What is difference between primary and foreign key? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 8

What is difference between primary and foreign key?

23rd Jan 2017, 6:04 AM
Anand
13 Answers
+ 15
A primary key is a column or a set of columns that uniquely identify a row in a table. A primary key should be short, stable and simple. A foreign key is a field (or collection of fields) in a table whose value is required to match the value of the primary key for a second table.
23rd Jan 2017, 6:52 AM
Muhamad Iqbal (bhalboel)
Muhamad Iqbal (bhalboel) - avatar
+ 13
I have a name. That is how people refer to me, so it is my primary key. I also have a dog with a name. His name is also his primary key as I use it to call for him. To let everyone know that it is my dog, I store a copy of his primary key as a part of myself, but since it refers to something else than me, it's a foreign key. In other words, the primary key is unique and defines the object, while foreign keys display a relation to another object.
23rd Jan 2017, 6:05 PM
Wind2dk
Wind2dk - avatar
+ 3
A primary key is a column/s that can uniquely identify tuples in a relation ,primary key must not be null.Where as a foreign key is a column/s that has the same set or subset of values as a primary key in another relation.
23rd Jan 2017, 6:17 PM
Avinash Singh
Avinash Singh - avatar
+ 3
The primary key the unique column of a table that uniquely identifies rows of table.It does not contain any duplicate item and also no value is empty. Whereas the foreign key is the column which may be more than one columns and its value is matched with the value of primary key to identify the record in another table.
24th Jan 2017, 2:52 PM
Siddharth Kumar Gupta
Siddharth Kumar Gupta - avatar
+ 2
primary keys are unique identifiers for data in a table while foreign keys simply reference data in another table. A(tableA_id, name,dataFromB) B(tableB_id,dataStoredInB) Take for instance the two tables above, the primary key(pk) are the tableA_id and tableB_id fields while the foreign key(fk ) is the dataFromB field that references the dataStoredInB field. Also foreign keys do not have to be primary keys from another table they can be field from another table.
23rd Jan 2017, 1:32 PM
Liam Tang
Liam Tang - avatar
+ 2
we use primary key to identify a record of a table uniquely.And we use foreign key to identify field of a one table uniquely to row of another table.
24th Jan 2017, 1:52 PM
Lasidu Lashan
Lasidu Lashan - avatar
+ 2
Tô sum up: Primary Key is an unique value set for each row in a table, for identifying the row. Foreign Key is a value used to reference a Primary Key from OTHER table.
24th Jan 2017, 2:57 PM
AlanPereira
AlanPereira - avatar
+ 1
primary key contain the unique value but foreign key have contain duplicate values.
25th Jan 2017, 1:27 AM
Ganesh YDG
Ganesh YDG - avatar
+ 1
primary key and foreign key both are unique and null value but primary key of one table will be known as the foreign key of another table.
7th Feb 2017, 10:21 AM
Shubham Shekhar Mahapatra
Shubham Shekhar Mahapatra - avatar
0
primary key is a unique which is not null .it is a key which uniquely identifys the table.forgien key is the referntial integrity.suppose take an real life example.student table contain rollnumber,name,branch.here rollnumber acts as primary key.In another table named library which contain same rollnumber a column here the rollnumber acts as a forgein key for the student table
1st Feb 2017, 5:09 PM
Hruday kumar sahu
Hruday kumar sahu - avatar
0
when joining 2 tables there needs to be a common field in both. E.G a customer table and sales record table. the sales record table will have the details of the sale and a [customer_ID] field. the customer table will also have a [ID] field. in the customer table the [ID] field will be unique for each customer, whilst the sales record table will have potentially many of the [customer_ID]s for different sales to the same customer. In the customer table the ID field is unique and is the primary key. the matching field in the sale record table is not unique in that table and is the foreign key. so unique in a table = primary, not unique in a table = foreign
8th Feb 2017, 12:30 PM
Mark Perry
Mark Perry - avatar
0
primary key mainly used for identifying any field value uniquely and it has also removed the use of two constraint NOT NULL and UNIQUE while foreign key relates the two tables one table's primary key may be foreign in another table
15th Feb 2017, 5:58 PM
ravi kumar sinha
0
primary key provide two conditions. 1 not null 2 data should be unique. foreign key provide references of other table.
27th Feb 2017, 8:42 AM
Shah Darshil N.