

{"id":25144,"date":"2018-08-17T06:02:46","date_gmt":"2018-08-17T06:02:46","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=25144"},"modified":"2021-03-11T21:33:02","modified_gmt":"2021-03-11T16:03:02","slug":"primary-and-foreign-key-in-sql","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/primary-and-foreign-key-in-sql\/","title":{"rendered":"Primary and Foreign Key in SQL With Examples"},"content":{"rendered":"<p>In this tutorial, we will look at the concept of Primary and foreign key. Both the Primary and the foreign key are SQL constraints.<\/p>\n<p>Constraints in SQL help us to manage the data and avoid any invalid transactions on it.<\/p>\n<p>The primary key is limited to a single table and is put to uniquely identify the corresponding rows of a table.<\/p>\n<p>When we talk about Foreign key, we can have as many Foreign keys as we want. A foreign key comes to use when we need to link tables to one another and have data spread over multiple tables.<\/p>\n<p>Let us now dive into the concept of Primary and Foreign keys by understanding the definition, syntax, and examples of both keys.<\/p>\n<h3>What is a Primary Key in SQL?<\/h3>\n<p>A Primary key is a unique column we set in a table to easily identify and locate data in queries. A table can have only one primary key.<\/p>\n<p>The primary key column has a unique value and doesn\u2019t store repeating values. A Primary key can never take NULL values.<\/p>\n<p><strong>For example,<\/strong> in the case of a student when identification needs to be done in the class, the roll number of the student plays the role of Primary key.<\/p>\n<p>Similarly, when we talk about employees in a company, the employee ID is functioning as the Primary key for identification.<\/p>\n<p>Let us now understand the Syntax of creating the table with the Primary key specified.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">CREATE TABLE tableName (\r\n    col1 int NOT NULL,\r\n    col2 varchar(50) NOT NULL,\r\n    col3 int,\r\n    \u2026\u2026\u2026\u2026\u2026.\r\n    PRIMARY KEY (col1)\r\n);\r\n<\/pre>\n<h3>What is a Foreign key in SQL?<\/h3>\n<p>A Foreign key is beneficial when we connect two or more tables so that data from both can be put to use parallelly.<\/p>\n<p>A foreign key is a field or collection of fields in a table that refers to the Primary key of the other table. It is responsible for managing the relationship between the tables.<\/p>\n<p>The table which contains the foreign key is often called the child table, and the table whose primary key is being referred by the foreign key is called the Parent Table.<\/p>\n<p><strong>For example:<\/strong> When we talk about students and the courses they have enrolled in, now if we try to store all the data in a single table, the problem of redundancy arises.<\/p>\n<p>To solve this table, we make two tables, one the student detail table and the other department table. In the student table, we store the details of students and the courses they have enrolled in.<\/p>\n<p>And in the department table, we store all the details of the department. Here the courseId acts as the Primary key for the department table whereas it acts as the Foreign key in the student table.<\/p>\n<p>Let us now look at the syntax of creating a table with a foreign key.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">CREATE TABLE childTable (\r\n    col1 int NOT NULL,\r\n    col2 int NOT NULL,\r\n    col3 int,\r\n    \u2026\u2026\u2026...\r\n    PRIMARY KEY (col1),\r\n    FOREIGN KEY (col3) REFERENCES parentTable(parent_Primary_key)\r\n);\r\n<\/pre>\n<h3>Understanding Primary and Foreign Key in detail with Examples<\/h3>\n<h4>1. Creation of Parent Table DataFlair<\/h4>\n<p><strong>Query:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">CREATE TABLE DataFlair(\r\nemp_id varchar(5) NOT NULL,\r\nname varchar(50),\r\nlocation varchar(50),\r\nexperience int,\r\nPRIMARY KEY(emp_id));\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/create-query-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-87038\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/create-query-1.png\" alt=\"Create query in SQL\" width=\"1920\" height=\"1032\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/create-query-1.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/create-query-1-300x161.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/create-query-1-1024x550.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/create-query-1-150x81.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/create-query-1-768x413.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/create-query-1-1536x826.png 1536w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/create-query-1-720x387.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/create-query-1-520x280.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/create-query-1-320x172.png 320w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<h4>2. Creation of Child Table Location<\/h4>\n<p><strong>Query:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">CREATE TABLE location(\r\nlocation_id varchar(5) NOT NULL,\r\nlocation varchar(50) NOT NULL,\r\noffice_size int,\r\nPRIMARY KEY(location_id),\r\nFOREIGN KEY(location) REFERENCES dataflair(location));\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/create-query-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-87040\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/create-query-2.png\" alt=\"Foreign Key in SQL\" width=\"1920\" height=\"1032\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/create-query-2.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/create-query-2-300x161.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/create-query-2-1024x550.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/create-query-2-150x81.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/create-query-2-768x413.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/create-query-2-1536x826.png 1536w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/create-query-2-720x387.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/create-query-2-520x280.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/create-query-2-320x172.png 320w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<h4>3. Viewing the Parent and the Child Table<\/h4>\n<p><strong>a. Parent Table or the DataFlair Table<\/strong><br \/>\n<strong>Query:\u00a0<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">SELECT * FROM DataFlair;\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/view-table-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-87041\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/view-table-1.png\" alt=\"View Table in SQL\" width=\"1920\" height=\"1032\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/view-table-1.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/view-table-1-300x161.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/view-table-1-1024x550.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/view-table-1-150x81.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/view-table-1-768x413.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/view-table-1-1536x826.png 1536w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/view-table-1-720x387.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/view-table-1-520x280.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/view-table-1-320x172.png 320w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p><strong>b. Child Table of the Location Table:<\/strong><br \/>\n<strong>Query:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">SELECT * FROM Location;\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/view-table-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-87042\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/view-table-2.png\" alt=\"view table in SQL\" width=\"1920\" height=\"1032\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/view-table-2.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/view-table-2-300x161.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/view-table-2-1024x550.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/view-table-2-150x81.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/view-table-2-768x413.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/view-table-2-1536x826.png 1536w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/view-table-2-720x387.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/view-table-2-520x280.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/view-table-2-320x172.png 320w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<h4>4. Running Queries on the Parent and Child Table<\/h4>\n<p><strong>Example 1:<\/strong> Let us now try to view the name of employee, location and the location id by using both the DataFlair and Location table.<br \/>\n<strong>Query:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">SELECT emp_id , name, \r\nlocation.location_id,DataFlair.location \r\nFROM DataFlair RIGHT JOIN Location \r\nON DataFlair.location=Location.location \r\nWHERE emp_id IS NOT NULL;\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/example-1-4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-87043\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/example-1-4.png\" alt=\"Running queries in SQL\" width=\"1920\" height=\"1024\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/example-1-4.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/example-1-4-300x160.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/example-1-4-1024x546.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/example-1-4-150x80.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/example-1-4-768x410.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/example-1-4-1536x819.png 1536w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/example-1-4-720x384.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/example-1-4-520x277.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/example-1-4-320x171.png 320w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p><strong>Example 2:<\/strong> Let us now view the locations where we have an office of DataFlair as of now.<br \/>\n<strong>Query:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">SELECT DISTINCT(location.location_id) AS Location_ID,\r\nDataFlair.location AS Office_Location\r\nFROM DataFlair RIGHT JOIN Location \r\nON DataFlair.location=Location.location \r\nWHERE emp_id IS NOT NULL;\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/example-2-4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-87044\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/example-2-4.png\" alt=\"SQL Queries Example\" width=\"1920\" height=\"1028\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/example-2-4.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/example-2-4-300x161.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/example-2-4-1024x548.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/example-2-4-150x80.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/example-2-4-768x411.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/example-2-4-1536x822.png 1536w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/example-2-4-720x386.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/example-2-4-520x278.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/example-2-4-320x171.png 320w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<h3>How to Drop Primary and Foreign key<\/h3>\n<p><strong>1. Drop Primary Key in SQL<\/strong><br \/>\n<strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">ALTER TABLE tableName\r\nDROP PRIMARY KEY;\r\n<\/pre>\n<p><strong>Query:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">ALTER TABLE dataflair\r\nDROP PRIMARY KEY;\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/drop-primary-key.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-87045\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/drop-primary-key.png\" alt=\"drop primary key in SQL\" width=\"1920\" height=\"1032\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/drop-primary-key.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/drop-primary-key-300x161.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/drop-primary-key-1024x550.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/drop-primary-key-150x81.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/drop-primary-key-768x413.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/drop-primary-key-1536x826.png 1536w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/drop-primary-key-720x387.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/drop-primary-key-520x280.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/drop-primary-key-320x172.png 320w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p><strong>2. Drop Foreign Key in SQL<\/strong><br \/>\n<strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">ALTER TABLE tableName\r\nDROP FOREIGN KEY foreignKeyName;\r\n<\/pre>\n<p><strong>Query:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">ALTER TABLE location\r\nDROP FOREIGN KEY location;\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/drop-foreign-key.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-87046\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/drop-foreign-key.png\" alt=\"drop foreign key in SQL\" width=\"1920\" height=\"1032\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/drop-foreign-key.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/drop-foreign-key-300x161.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/drop-foreign-key-1024x550.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/drop-foreign-key-150x81.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/drop-foreign-key-768x413.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/drop-foreign-key-1536x826.png 1536w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/drop-foreign-key-720x387.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/drop-foreign-key-520x280.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/drop-foreign-key-320x172.png 320w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<h3>Difference Between Primary and Foreign Key<\/h3>\n<table>\n<tbody>\n<tr>\n<td><b>Sr.No<\/b><\/td>\n<td><b>Primary Key<\/b><\/td>\n<td><b>Foreign Key<\/b><\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td><span style=\"font-weight: 400;\">Used to maintain the unique identification of data in the table.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Used to maintain the relationship between two or more relational tables.<\/span><\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td><span style=\"font-weight: 400;\">Helps us to identify data in a database table.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Helps to identify the data in another table using the connection with the foreign key.<\/span><\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td><span style=\"font-weight: 400;\">A table can have only one Primary Key.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">A table can have any number of Foreign Keys.<\/span><\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td><span style=\"font-weight: 400;\">The primary key is unique and Not Null.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">A foreign key can contain duplicate values also.<\/span><\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td><span style=\"font-weight: 400;\">Primary key can\u2019t take Null as a value.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">A foreign key can take NULL entries also.<\/span><\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td><span style=\"font-weight: 400;\">Primary Key can\u2019t be modified once entered.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">A foreign key can be modified at any instance of time.<\/span><\/td>\n<\/tr>\n<tr>\n<td>7<\/td>\n<td><span style=\"font-weight: 400;\">We can have Primary keys for temporary tables as well.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">We can&#8217;t have Foreign keys for the temporary tables.<\/span><\/td>\n<\/tr>\n<tr>\n<td>8<\/td>\n<td><span style=\"font-weight: 400;\">A Primary key can be defined on its own.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">For defining a Foreign key, we need a parent table with a Primary Key.<\/span><\/td>\n<\/tr>\n<tr>\n<td>9<\/td>\n<td><span style=\"font-weight: 400;\">Primary key creates clustered indexes on the table.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Foreign key does not create indexes on the table neither clustered nor unclustered.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Summary<\/h3>\n<p>In this tutorial, we have seen what a Primary and a Foreign Key is. We now know the definition of both and the syntax of Primary and foreign key.<\/p>\n<p>We have seen examples of how to use both the Primary and Foreign key to create tables. And understanding the relationship they maintain with each other.<\/p>\n<p>We have further enhanced our understanding by going through examples and understanding the working of queries on both the constraint keys.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will look at the concept of Primary and foreign key. Both the Primary and the foreign key are SQL constraints. Constraints in SQL help us to manage the data and&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":87037,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66],"tags":[3093,4858,8983,13402,13434,13435,13459,13512,13534,13550,13557],"class_list":["post-25144","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-sql","tag-create-table-primary-key","tag-foreign-key-in-database","tag-mysql-add-foreign-key","tag-sql-add-foreign-key","tag-sql-create-table-foreign-key","tag-sql-create-table-primary-key","tag-sql-foreign-key","tag-sql-primary-key","tag-sql-server-add-primary-key","tag-sql-server-foreign-key","tag-sql-server-primary-key"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Primary and Foreign Key in SQL With Examples - DataFlair<\/title>\n<meta name=\"description\" content=\"primary and foreign key in sql,sql foreign key,sql primary key,sql create table primary key,foreign key database,add foreign key,foreign key vs primary key\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/primary-and-foreign-key-in-sql\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Primary and Foreign Key in SQL With Examples - DataFlair\" \/>\n<meta property=\"og:description\" content=\"primary and foreign key in sql,sql foreign key,sql primary key,sql create table primary key,foreign key database,add foreign key,foreign key vs primary key\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/primary-and-foreign-key-in-sql\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-08-17T06:02:46+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-03-11T16:03:02+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Primary-and-foreign-key-in-SQL.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Primary and Foreign Key in SQL With Examples - DataFlair","description":"primary and foreign key in sql,sql foreign key,sql primary key,sql create table primary key,foreign key database,add foreign key,foreign key vs primary key","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/primary-and-foreign-key-in-sql\/","og_locale":"en_US","og_type":"article","og_title":"Primary and Foreign Key in SQL With Examples - DataFlair","og_description":"primary and foreign key in sql,sql foreign key,sql primary key,sql create table primary key,foreign key database,add foreign key,foreign key vs primary key","og_url":"https:\/\/data-flair.training\/blogs\/primary-and-foreign-key-in-sql\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-08-17T06:02:46+00:00","article_modified_time":"2021-03-11T16:03:02+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Primary-and-foreign-key-in-SQL.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/primary-and-foreign-key-in-sql\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/primary-and-foreign-key-in-sql\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89"},"headline":"Primary and Foreign Key in SQL With Examples","datePublished":"2018-08-17T06:02:46+00:00","dateModified":"2021-03-11T16:03:02+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/primary-and-foreign-key-in-sql\/"},"wordCount":857,"commentCount":6,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/primary-and-foreign-key-in-sql\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Primary-and-foreign-key-in-SQL.jpg","keywords":["create table primary key","foreign key in database","mysql add foreign key","SQL add Foreign Key","sql create table foreign key","sql create table primary key","SQL Foreign Key","SQL Primary Key","sql server add primary key","SQL server Foreign Key","SQL server primary key"],"articleSection":["SQL Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/primary-and-foreign-key-in-sql\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/primary-and-foreign-key-in-sql\/","url":"https:\/\/data-flair.training\/blogs\/primary-and-foreign-key-in-sql\/","name":"Primary and Foreign Key in SQL With Examples - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/primary-and-foreign-key-in-sql\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/primary-and-foreign-key-in-sql\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Primary-and-foreign-key-in-SQL.jpg","datePublished":"2018-08-17T06:02:46+00:00","dateModified":"2021-03-11T16:03:02+00:00","description":"primary and foreign key in sql,sql foreign key,sql primary key,sql create table primary key,foreign key database,add foreign key,foreign key vs primary key","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/primary-and-foreign-key-in-sql\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/primary-and-foreign-key-in-sql\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/primary-and-foreign-key-in-sql\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Primary-and-foreign-key-in-SQL.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Primary-and-foreign-key-in-SQL.jpg","width":1200,"height":628,"caption":"Primary and foreign key in SQL"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/primary-and-foreign-key-in-sql\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Searching for Groups of Scala Regex","item":"https:\/\/data-flair.training\/blogs\/tag\/searching-for-groups-of-scala-regex\/"},{"@type":"ListItem","position":3,"name":"Primary and Foreign Key in SQL With Examples"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"The DataFlair Team provides industry-driven content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our expert educators focus on delivering value-packed, easy-to-follow resources for tech enthusiasts and professionals.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam2\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/25144","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=25144"}],"version-history":[{"count":5,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/25144\/revisions"}],"predecessor-version":[{"id":87047,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/25144\/revisions\/87047"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/87037"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=25144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=25144"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=25144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}