• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
ExamTurf

Exam Turf

  • Categories
    • Exam Preparations
      • All Exams
      • Popular
        • UPSC-PSC
        • SSC Exams
        • Bank Exams
        • Railway Exams
        • Defence Exams
        • IIT JEE/Engineering
        • NEET/Medical
        • State PSC
        • NET Exams
        • GATE & ESC
        • IIT-JAM
        • NEET PG
        • TET Exams
        • CAT/MBA
        • CA & CS
        • State CET
        • CLAT and Law
        • State Exams
      • Finance Exam Prep
        • ACCA
        • CFP®
        • CMA®
        • CPA
        • CAIA®
        • CFA®
        • ChFC®
        • CGMA®
        • Financial Modeling
        • FRM®
        • CCP
        • CISA
        • CIA
      • Project Management
        • PMP
        • CAPM
        • CSM
        • CompTIA Project+
        • ACP
        • PRINCE2
        • CPD
        • Project Manager IAPM
        • PPM
        • MPM
        • APM
        • CPMP
        • CSSBB
        • CSSGB
        • PMC
        • BVOP
        • GPM-IPMA
        • PgMP
        • PMI-RMP
        • PMI-PBA
        • PMI-ACP
        • Others
      • Quality Management
        • CQI
        • CQPA
        • CQT
        • CRE
        • CSSBB
        • CSSGB
        • CSSYB
        • CSQE
        • Quality Management Exam Prep
      • IT and Software
        • AWS Exams
        • CCSP
        • CDPSE
        • CDP
        • CEH
        • CISM
        • CISSP
        • CCIE
        • CCNA
        • CCNP
        • Microsoft Azure
        • MCSA
        • CMDBA
        • Salesforce
        • CompTIA A+
        • MTA
        • PMP
        • Oracle Certification
        • GIAC
        • ITIL
        • Others
      • Data Science Exam Prep
        • CAP
        • CCA
        • CCP
        • DASCA Certifications
        • EMCDS
        • Google Certifications
        • IBM Certifications
        • Azure
        • Open CDS
        • SAS
        • Tensorflow
        • Data Science Exam Prep
      • Digital Marketing
        • Facebook
        • Google AdWords
        • Hootsuite
        • HubSpot
        • PCM Digital
        • AMA Certifications
        • Others
    • K-12
      • Class 12
      • Class 11
      • Class 10
      • Class 9
      • Class 8
      • Class 7
      • Class 6
      • Graduation
    • Data Science
      • Big Data
        • Hadoop
        • Splunk
        • Apache Pig
        • Apache Spark
        • Hive
        • Mahout
        • Apache Storm
        • Pyspark
        • Sqoop
        • Kafka
        • Others
      • Deep Learning
        • Tensorflow
        • Octave
        • OpenCV
        • NLP
        • Scikit-Learn
        • Python
      • Machine Learning
        • Tensorflow
        • Octave
        • OpenCV
        • NLP
        • Scikit-Learn
        • Python
      • Artificial Intelligence
        • Python
        • Tensorflow
        • Deeplearning
        • Pytorch
        • IoT
      • Data Analysis
        • R
        • SAS
        • Devops
        • Docker
        • Kubernetes
        • Keras
      • Neural Networks
        • Keras
        • R
        • Python
        • Pytorch
        • AI
        • BI
      • Python
        • Seaborn
        • Pandas
        • Numpy
        • Kubernetes
        • Keras
        • Pytorch
        • Pyspark
      • Business Intelligence
        • SAS
        • Power BI
        • Qliksense
      • Business Analytics
        • SEO
        • Google Analytics
        • Marketing Analytics
        • Sales Analytics
        • Fraud Analytics
        • Customer Analytics
        • Talend
      • Cloud Computing
        • AWS
        • Cloud
        • Azure
      • Data Visualization
        • SSIS
        • Qlikview
        • Matplotlib
        • Seaborn
        • Pandas
        • Numpy
        • Kibana
      • Statistics
        • Minitab
        • Tableau
        • SPSS
        • Eviews
        • Predictive Modeling
        • Time Series
        • Forecasting
      • Database
        • Informatica
        • SQL
        • CloverETL
        • CouchDB
        • Solr
        • Cassandra
    • Development
      • All Development
      • Web Development
        • All Web Development
        • JavaScript
        • React
        • CSS
        • Angular
        • PHP
        • Node.Js
        • WordPress
      • Data Science
        • All Data Science
        • Python
        • Deep Learning
        • Machine Learning
        • Data Analysis
        • Artificial Intelligence
        • R
        • Tensorflow
        • Neural Networks
      • Mobile Development
        • All Mobile Development
        • Google Flutter
        • Android Development
        • iOS Development
        • Swift
        • React Native
        • Dart Programming
        • Kotlin
        • Redux Framework
      • Programming Languages
        • All Programming
        • Python
        • Java
        • RPA Robotics
        • C++ and C#
        • React
        • JavaScript
        • C
        • Go Programming
        • Spring Framework
      • Game Development
        • All Game Development
        • 2D Game Development
        • 3D Game Development
        • Unity
        • Game Fundamentals
        • Unreal Engine
        • RPA Robotics
        • C++ and C#
        • 3D Max
        • Blender
      • Database Design
        • All Database Design
        • SQL
        • MySQL
        • Oracle SQL
        • MongoDB
        • Oracle Certification
        • SQL Server
        • Apache Kafka
        • PostgreSQL
        • Database Programming
      • Software Testing
        • All Software Testing
        • Selenium WebDriver
        • Java
        • Automation Testing
        • Selenium Testing Framework
        • API Testing
        • Cypress.io
        • REST Assured
        • Python
      • Software Engineering
        • All Software Engineering
        • Coding Interview
        • Kubernetes
        • Certified Kubernetes Application Developer
        • Data Structures
        • Software Practices
        • Microservices
        • Python
        • Spring Boot
        • Elasticsearch
      • Development Tools
        • All Development Tools
        • Docker
        • Kubernetes Python
        • Git
        • DevOps
        • Jenkins
        • JIRA
        • AWS Certified Solutions Architect - Associate
        • Continuous Integration
        • Confluence
      • No Code Development
        • All No-Code Development
        • Elementor
        • WordPress
        • Microsoft
        • Software Development
        • Web Design
        • Others
    • Business
      • All Business
      • Entrepreneurship
        • All Entrepreneurship
        • Business Plan
        • Business Fundamentals
        • Entrepreneurship Fundamentals
        • Business Strategy
        • Social Media Strategy
        • Branding Strategy
        • Startup
        • Home Business
      • Communications
        • All Communications
        • Communication Skills
        • Presentation Skills
        • Public Speaking
        • Business Writing
        • Persuasion
        • Email Etiquette
        • Business Communication
      • Sales
        • All Sales
        • Sales Skills
        • B2B Sales
        • Presentation Skills
        • Business Development
        • Lead Generation
        • Cold Email
        • Customer Service
      • Business Strategy
        • All Business Strategy
        • Digital Marketing
        • Google Ads (Adwords)
        • TOGAF 9 Foundation
        • Strategic Planning
        • Management Consulting
        • Agile
      • Operations
        • All Operations
        • Six Sigma
        • Six Sigma Green Belt
        • Supply Chain
        • Quality Management
        • Process and Automation
        • Six Sigma Black Belt
        • Lean
        • Six Sigma Yellow Belt
      • Project Management
        • All Project Management
        • PMP
        • PMBOK
        • Agile
        • Scrum
        • PMI-ACP
        • CAPM
        • Risk Management
        • Professional Scrum Master
      • Business Law
        • All Business Law
        • Product Management
        • Leadership
        • Management Skills
        • Business Process Management
        • Business Strategy
        • ISO 9001
        • Risk Management
        • Agile
        • Quality Management
      • Business Analytics and Intelligence
        • All BA and BI
        • Microsoft Power BI
        • SQL
        • Tableau
        • Business Analysis
        • Business Intelligence
        • Data Analysis
        • MySQL
        • Data Modeling
        • Big Data
      • Human Resources
        • All Human Resources
        • Recruiting
        • Instructional Design
        • Communication
        • Hiring
        • Leaderships Management
        • Conflict Management
        • HR Analytics
        • Talent Management
      • Industry
        • All Industry
        • Communication Skills
        • Presentation Skills
        • Public Speaking
        • Business Writing
        • Persuasion
        • Email Etiquette
        • Managing Leadership
      • E Commerce
        • All E-Commerce
        • Dropshipping
        • Search Engine
        • WordPress for Ecommerce
      • Media
        • All Media
        • Communication Skills
        • Presentation Skills
        • Public Speaking
        • Business Writing
        • Persuasion
        • Email Etiquette
        • Managing Leadership
      • Real Estate
        • All Real Estate
        • Investing
        • Risk Profiling
        • Construction
        • Financial Modeling
        • Management
        • Real Estate Finance
        • Marketing
      • Other Business
        • Product Management
        • Leadership
        • Management Skills
        • Business Process Management
        • Business Strategy
        • ISO 9001
        • Risk Management
        • Agile
        • Quality Management
    • Finance & Accounting
      • All Fin and Accounting
      • Taxes
        • All Taxes
        • Tax Preparation
        • Goods and Services Tax
        • History
        • Home Business
        • Value Added Tax (VAT)
        • QuickBooks Online
        • Financial Accounting
        • Entrepreneurship Fundamentals
        • Personal Finance
      • Money Management Tools
        • All Money Management Tools
        • QuickBooks Online
        • QuickBooks
        • Excel
        • SAP FICO
        • Xero
        • Financial Modeling
        • Excel Analytics
        • Financial Analysis
      • Investing and Trading
        • All Investing & Trading
        • Stock Trading
        • Technical Analysis
        • Forex
        • Investing
        • Options Trading
        • Day Trading
        • Algorithmic Trading
        • Financial Analysis
        • Financial Trading
      • Financial Modelings
        • All Financial Modeling & Analysis
        • Financial Analysis
        • Financial Modeling
        • Finance Fundamentals
        • Excel
        • History
        • Investing
        • Python
        • Investment Banking
        • Financial Management
      • Finance Exam Prep
        • All Finance Cert & Exam Prep
        • CFA
        • Certified Management Accountant (CMA)
        • Financial Markets
        • Quantitative Finance
        • ACCA
        • Financial Management
        • ANBIMA Certification
        • Corporate Finance
        • Fixed Income Securities
      • Finance
        • All Finance
        • Personal Finance
        • Investment Banking
        • CFA
        • Finance Fundamentals
        • Financial Management
        • Financial Analysis
        • Corporate Finance
        • Company Valuation
        • Excel
      • Economics
        • All Economics
        • Microeconomics
        • Macroeconomics
        • Stata
        • Entrepreneurship Fundamentals
        • Econometrics
        • Finance Fundamentals
        • Political Science
        • Regression Analysis
      • Crypto and Blockchain
        • All Cryptocurrency & Blockchain
        • Cryptocurrency
        • Bitcoin
        • Blockchain
        • Personal Finance
        • Day Trading
        • Bitcoin Trading
        • Algorithmic Trading
        • Ethereum
        • Technical Analysis
      • Compliance
        • All Compliance
        • Anti-Money Laundering
        • Risk Management
        • Sarbanes-Oxley (SOX)
        • Internal Auditing
        • CAMS Certification
        • CIA
        • IFRS
        • Financial Risk Manager (FRM)
        • Certified Fraud Examiner (CFE)
      • Acc and Bookkeeping
        • All Accounting & Bookkeeping
        • History
        • Finance Fundamentals
        • Financial Accounting
        • Bookkeeping
        • Financial Statement
        • Xero
        • Cost Accounting
        • Tally.ERP
        • IFRS
    • IT & Software
      • Robotics RPA
      • IT Certification
        • All IT Certification
        • AWS Certification
        • Microsoft Certification
        • AWS Certified Solutions Architect - Associate
        • AWS Certified Cloud Practitioner
        • CompTIA - A
        • Cisco CCNA
        • AWS Certified Developer - Associate
        • CompTIA Security+
        • Amazon AWS
      • Network and Security
        • All Network & Security
        • Ethical Hacking
        • Cyber Security
        • Network Security
        • CompTIA Security+
        • Penetration Testing
        • IT Networking Fundamentals
        • CompTIA Network+
        • Cisco CCNA
        • Terraform
      • Hardware
        • All Hardware
        • Arduino
        • PLC
        • Electronics
        • Microcontroller
        • Raspberry Pi
        • Embedded Systems
        • FPGA
        • HMI
        • RTOS
      • Operating Systems
        • All Operating Systems
        • Linux
        • Linux Administration
        • Windows Server
        • Shell Scripting
        • Active Directory
        • LPIC-1: Linux Administrator
        • PowerShell
        • VMware Vsphere
        • Operating System Creation
      • All IT and Software
        • All Other IT & Software
        • Docker
        • Python
        • Kubernetes
        • Devops
        • Algorithms
        • Ansible
        • AWS Certified Solutions Architect - Professional
        • Java
        • AWS Certification
    • Office Productivity
      • All Office Productivity
      • Microsoft
        • All Microsoft
        • Words
        • PowerPoint
        • SQL
        • SharePoint
        • Excel
        • OutLook
        • OneDrive
        • MS Project
        • Azure
      • Apple
        • All Apple
        • iOS
        • Mac Basics
        • macOS
        • excel
        • MS Project
        • Mac Pages
        • Office Productivity
        • Video Editing
        • Microsoft Word
      • Google
        • All Google
        • Google Analytics
        • Google Tag
        • Google Drive
        • Google Apps
        • Excel
        • Gmail Productivity
        • Google Data Studio
        • Google Docs
        • Google Office
      • SAP
        • All SAP
        • SAP ABAP
        • SAP FICO
        • SAP FICO
        • SAP Financial Accounting
        • SAP HCM
        • Supply Chain
        • SAP MM
        • SAP S/4HANA
        • SAP SD
      • Oracle
        • All Oracle
        • Primavera
        • SQL
        • Business Intelligence
        • Database
        • PLSQL
        • Data Integrator
        • Exam Prep
      • Other Office Productivity
        • All Others
        • ServiceNow
        • Time Management
        • Personal Productivity
        • Salesforce
        • QuickBooks
    • Personal Development
      • All Personal Development
      • Personal Transformation
        • All Personal Transformation
        • Life Coach Training
        • Neuro-Linguistic Programming
        • Mindfulness
        • Personal Development
        • Life Purpose
        • Meditation
        • CBT
        • Neuroscience
      • Personal Productivity
        • All Personal Productivity
        • Time Management
        • Focus Mastery
        • Speed Reading
        • Goal Setting
        • Organization
        • PowerShell
        • Procrastination
        • Habits
      • Leadership
        • All Leadership
        • Management Skills
        • Manager Training
        • Communication Skills
        • Public Speaking
        • Conflict Management
        • Listening Skills
        • Relationship Building
        • Spirituality
      • Career Development
        • All Career Development
        • Resume and CV Writing
        • Interviewing Skills
        • Job Search
        • Personal Networking
        • Life Coach Training
        • Soft Skills
        • Business Communication
      • Personal Brand Building
      • Time Management
      • Communication
    • Design
      • All Design
      • Web Design
        • All Web Design
        • Photoshop
        • After Effects
        • HTML CSS Web
        • Adobe Premiere
        • Video Editing
        • Digital Art
        • AutoCAD
        • Graphic Design
      • Graphic Design Illustration
        • All Graphic Illustration
        • Graphic Design
        • Photoshop
        • HTML CSS Web
        • Drawing
        • Digital Painting
        • InDesign
        • Figure Drawing
      • Design Tools
        • All Design Tools
        • Photoshop
        • After Effects
        • HTML CSS Web
        • Adobe Premiere
        • Video Editing
        • Digital Art
        • AutoCAD
        • Graphic Design
      • Game Design
      • User Experience Design
        • All User Experience
        • User Interface
        • Adobe XD
        • Figma
        • Web Design
        • Product Design
        • Sketch
        • Axure
        • InVision Studio
      • Design Thinking
        • All Design Thinking
        • WordPress
        • CSS
        • Sketch
        • HTML
        • Photoshop
        • User Interface
        • HTML5
        • Adobe XD
      • 3D and Animation
        • All 3D & Animation
        • Blender
        • 3D Modeling
        • After Effects
        • Motion Graphics
        • 3D Animation
        • 3D Texturing
        • zBrush
        • 3ds Max
        • Fusion 360
      • Fashion Design
        • All Fashion Design
        • Fashion
        • HTML CSS Web
        • Sewing
        • Jewelry Design
        • Photoshop
        • T-Shirt Design
        • Anime
        • Marvelous Designer
      • Architectural Design
      • Interior Design
      • Other Design
    • Marketing
      • All Marketing
      • Digital Marketing
        • All Digital Marketing
        • Google Ads (Adwords)
        • Social Media
        • Internet Marketing
        • Email Marketing
        • Google Analytics
      • SEO Optimization
        • All SEO Optimization
        • SEO
        • WordPress
        • Keyword Research
        • Local SEO
        • Link Building
        • Google my Business
        • SEO Audit
        • Google Ads (Adwords)
      • Social Media
        • All Social Media
        • Instagram Marketing
        • Facebook Ads
        • Facebook Marketing
        • PPC Advertising
        • Social Media Management
        • Instagram Photography
        • TikTok Marketing
      • Branding and Strategy
        • All Branding
        • Business Branding
        • Personal Branding
        • Brand Management
        • Graphic Design
        • Social Media
      • Marketing Fundamentals
        • All Fundamentals
        • Google Ads (Adwords)
        • SMM
        • Marketing Strategy
        • Internet Marketing
        • Sales Marketing
        • Email Marketing
        • Google Analytics
      • Analytics and Automation
        • All Marketing Analytics
        • Google Analytics
        • Google Analytics IQ
        • Data Analysis
        • Marketing Analytics
        • SQL
        • Google Tag Manager
        • Marketing Automation
      • Public Relations
        • All Public Relations
        • Leadership
        • Business Communication
        • Public Speaking
        • Brand Building
        • Startup
        • Planning
        • Time Management
      • Advertising
        • All Advertising
        • Instagram Marketing
        • Facebook Ads
        • Facebook Marketing
        • PPC Advertising
        • Social Media Management
        • Instagram Photography
        • TikTok Marketing
      • Sales Management
      • Content Marketing
        • All Content Marketing
        • Time Management
        • Branding Strategy
        • Content Writing
        • Content Creation
        • WordPress
      • Growth Hacking
        • All Growth Hacking
        • Digital Marketing
        • App Marketing
        • Lead Generation
        • SEO
        • Instagram Marketing
        • Excel
        • B2B Sales
      • Affiliate Marketing
        • All Affiliate Marketing
        • ClickBank
        • SEO
        • CPA Marketing
        • Teespring
        • Internet Marketing
        • Home Business
      • Product Marketing
        • All Product Marketing
        • Marketing Plan
        • Self-Publishing
        • Product Management
        • Voice-Over
        • Marketing Management
        • Amazon PPC Advertising
        • Facebook Marketing
    • Teaching and Academics
      • Medical
      • Law
      • Humanities
      • Math
        • All Math
        • Calculus
        • Statistics
        • Linear Algebra
        • Probability
        • Algebra
        • Trigonometry
        • Discrete Math
        • Geometry
      • Science
        • All Science
        • Physics
        • Solar Energy
        • Anatomy
        • Chemistry
        • Biology
        • Neuroscience
        • Physiology
        • Research Paper Writing
        • AP Physics
      • Online Education
      • Social Science
        • All Social Science
        • Counseling
        • History
        • Economics
        • Geography
        • Social Psychology
        • Others SS
      • Teacher Training
      • Test Prep
        • All Test Prep
        • CFA
        • PMP
        • Devops Test Prep
        • FRM
        • CEH Ethical Hacker
        • GMAT
        • Azure
        • AWS
        • Others
      • Other Teaching & Academics
  • Teach at EXAMTURF
  • Login
  • Sign Up

MySQL Constraints

Home » Data Science » Database » MySQL Constraints

April 27, 2021 by Ajinkya Radhakrishnan

TwitterLinkedInFacebookWhatsApp

MySQL Constraints

Introduction To MySQL Constraints

The following article provides an outline for MySQL Constraints. MySQL constraint is mainly used to restrict or allow, the value or type of data, which is to be stored in the table of a database. It is the best way to maintain a database’s uprightness. One can apply constraints in two levels, one is ‘table label’ and another is ‘column level’. Column level can be applied to only one column, unlike table level which can be applied to the entire table. Once the constraints applied in the table, while executing, if there are any mismatched data entered, it will not allow executing the operation.

Different Constraints Used In MySQL

Given below are the six constraints mostly used in MySQL:

  • ‘NOT NULL’
  • ‘UNIQUE’
  • ‘CHECK’
  • ‘DEFAULT’
  • ‘PRIMARY KEY’
  • ‘FOREIGN KEY’

1. ‘NOT NULL’ Constraint

If we use this constraint in a column, then it will not accept any valueless entry.

For example, we have created a table called ‘Employee’, wherein we are supposed to enter the employee’s ‘first name’ and the name of the ‘hometown’ of that employee.

Code:

MySQL>CREATE TABLE Employee(Id INTEGER, Name TEXT NOT NULL, Hometown VARCHAR(40));

Now we will add some data to the table in the following way:

Code:

Mysql>INSERT INTO Employee VALUES(1,'John','Mumbai');

Mysql>INSERT INTO Employee VALUES(2,NULL,'Delhi');

The first command is correct, and it will run successfully, where we have inserted all the values. But while running the second command, it will show an error. The ‘Error 1038’ error would appear, stating column ‘Name’ cannot be null.

2. ‘UNIQUE’ Constraint

The ‘unique’ name comes from the uniqueness, which this constraint maintains in a column. The function of this constraint is, not to allow any duplicate value in a column of a table. In the table called ‘Employee’, we will apply a ‘unique’ constraint to the employee’s hometown.

Code:

MySQL> CREATE TABLE Employee(Id INTEGER, Name UNIQUE, Hometown VARCHAR(40));

Now we will add some similar data to the table in the following way:

Code:

Mysql>INSERT INTO Employee VALUES(1,'John','Mumbai');

Mysql>INSERT INTO Employee VALUES(2,'Jack','Mumbai');

The first command will run successfully where we have inserted all the values for the first time, with no repetition. But while running the second command it will show an error, as the ‘hometown’ name is the same. As per the condition, it should be different. The ‘Error 1062’ error would appear stating that it is a duplicate entry as ‘Mumbai’ for ‘Employee.Hometown’.

3. ‘CHECK’ Constraint

Here we can apply conditions or place controls to have the desired value in that column. It tries to validate the value with the given logical expression to that particular column. In the table named ‘Employee’, we have added one more column called ‘age’. Now we don’t want, that somebody enters numeric figures below ’21’.

Code:

MySQL>CREATE TABLE Employee(Id INTEGER, Name UNIQUE, HomeTown VARCHAR(40), Age int CHECK (Age>=21));

Now we will insert some value to the table:

Code:

Mysql>INSERT INTO Employee VALUES(1,'John','Mumbai',25);

Mysql>INSERT INTO Employee VALUES(2,'Jack','Delhi',15);

The second command is incorrect. It will show an error, as the age of the employee is less than 21. ‘Error 3819’ error should appear stating that ‘Employee_age_1’ has been violated.

4. ‘DEFAULT’ Constraint

In a column of a table, if we require a value, even including a NULL, then we use this constraint. In a column, if no value is inserted then it takes the default value assigned for that column. In the table named ‘Employee’, we don’t want, that somebody keeps the hometown column blank.

Code:

MySQL>CREATE TABLE Employee(Id INTEGER, Name UNIQUE, Hometown VARCHAR(40) DEFAULT'Mumbai');

Now we will add some similar data to the table in the following way:

Code:

mysql>INSERT INTO Employee VALUES(1,'John','Mumbai');

mysql>INSERT INTO Employee VALUES(2,'Jack','70');

The second command is incorrect, while running, it will not accept ’70’ as hometown instead it will take ‘Mumbai’ as a default value for that column.

5. PRIMARY KEY Constraint

To access the table faster, some unique indexing is required for that value. This constraint solves this problem very efficiently. There can be many columns in a table, containing various sets of values, but it should contain one primary key only. In the table named ‘Employee’, if someone enters the same ID repeatedly it can be a mess in the table. That is why a primary key is required.

Code:

MySQL>CREATE TABLE Employee(Id INTEGER, Name UNIQUE, Hometown VARCHAR(40) DEFAULT ‘Mumbai');

Now we will add some similar data to the table in the following way:

Code:

MySQL>INSERT INTO Employee VALUES (1,'John','Mumbai');

MySQL>INSERT INTO Employee VALUES (2,'Jack','Delhi');

MySQL>INSERT INTO Employee VALUES (2,'Jemmy', 'Kolkata');

The first and second command is correct but the third is not. While running the third command, it will not accept ‘2’ as ID. The ‘Error 1062’ error would reflect stating that it has a duplicate entry ‘2’ for the key ‘Employee.PRIMARY’.

6. FOREIGN KEY Constraint

This key is also known as the referencing key. Suppose two tables are containing different values in their respective columns. Now if we want to link these two tables, there should be something common in between, which is the primary key of both the table. Now if we try to connect or link the primary key of the first table with the primary key of the second table, then we will call the second table’s primary key a foreign key.

For example, we have created two tables, one is ‘Employee’ and another is ‘Organization’.

Code:

MySQL>CREATE TABLE Employee (Person Id PRIMARY KEY, Name UNIQUE, HomeTown VARCHAR(40));

Let’s add some data to the table:

Code:

Mysql>INSERT INTO Employee VALUES (1,'John','Mumbai');

Mysql>INSERT INTO Employee VALUES (2,'Jack','Delhi');

Mysql>INSERT INTO Employee VALUES (3,'Jimmy', 'Kolkata');

Let’s create an ‘Organization’ table:

Code:

MySQL>CREATE TABLE Organization (PersonId, Department UNIQUE, Specializations VARCHAR (40) FOREIGN KEY (Employee_ID) REFERENCES Employee(Person_ID));

We will add some data to this table too:

MySQL>INSERT INTO Employee VALUES (3,'Operations','Management');

MySQL>INSERT INTO Employee VALUES (2,'Software Development’,‘Data Science');

MySQL>INSERT INTO Employee VALUES (1,'Human Resource’,‘Hiring');

After running all these six commands this is how the details would appear:

Code:

PersonID1, Name 'John', Hometown 'Mumbai' EmployeeID1, Department 'Human Resource', Specializations 'Hiring'

PersonID2, Name 'Jack', Hometown 'Delhi'); EmployeeID2, Department 'Software Development', Specializations 'Data Science'

PersonID3, Name 'Jimmy', Hometown 'Kolkata' EmployeeID3, Department 'Operations, Specializations 'Management'

Recommended Articles

This is a guide to MySQL Constraints. Here we discuss the introduction and different constraints used in MySQL respectively. You can also go through our other suggested articles to learn more –

  1. What is MySQL?
  2. MySQL Operators
  3. MySQL Server
  4. MySQL Commands

Are you preparing for the entrance exam ?

Join our Data Science test series to get more practice in your preparation

View More
TwitterLinkedInFacebookWhatsApp

Data Science,  Database

Primary Sidebar

Recent Posts

  • Voice Over Internet Protocol (VoIP)
  • Data Modeling Interview Questions
  • Resorts in Indore
  • Big Data and Artificial Intelligence
  • Shore Temple

Categories

  • Artificial Intelligence
  • Bank Exams
  • Best Resorts
  • C# Tutorial
  • CAT Exam
  • Celebrity
  • Cloud Computing
  • Data Analysis
  • Data Science
  • Database
  • Defence Exams
  • Exam Preparations
  • Finance & Accounting
  • Finance Cert and Exam Prep
  • Full Form
  • IIT JEE Preparation
  • Interview Questions
  • Interview Questions
  • IT & Software
  • Java Tutorial
  • K-12
  • Machine Learning
  • Management & Foreign Studies
  • Miscellaneous
  • Mobile Development
  • NEET Preparation
  • Operating Systems
  • Others
  • PostgreSQL
  • Programming Languages
  • Python
  • Railway Exams
  • Software Development
  • SSC Exams
  • State PSC
  • Temple
  • Top Difference Between
  • Tourist Places
  • Uncategorized
  • UPSC
  • Web Development
  • World Best Hotels
Footer

Company

  • About Us
  • Contact Us
  • Terms
  • Privacy Policy & Cookie Policy
  • Cancellation/Refund Policy

Teachers

  • Teach on Examturf
  • Instructor FAQs
  • Instructor Revenue Share
  • Marketing Tips for Instructor
  • Instructor Success Stories

Learners

  • Learners FAQs

Affiliate

  • Become an Affiliate
  • Affiliate Success Stories

Resources

  • Blog
  • Exam Preparation
  • UPSC Exams
  • SSC Exams
  • IIT JEE
  • NEET Preparations
  • Data Science
  • Development

Featured Tests

  • All Featured Tests
  • Mini Quizzes
  • Comprehensive Series
  • Regular Tests
  • Exam Preparations
  • Data Science
  • Development
  • Business
  • Finance & Accounting
  • IT & Software
  • Office Productivity
  • Personal Development
  • Design
  • Marketing
  • Teaching & Academics

Copyright © 2025

Special Offer: 2000+ Selected Test Series, Mini Quizzes and Mock Tests are FREE. Offer valid for :

x