How is FOREIGN KEY command different from PRIMARY KEY command?
Answers
Answer:
A primary key is a special key used to uniquely identify records in a table.
Whereas a foreign key is used to establish relationship between two tables.
Hope it helps uhh......
Answer:
Basics of Primary Key vs Foreign Key:
A primary key is a special key in a relational database that acts as a unique identifier for each record meaning it uniquely identifies each row/record in a table and its value should be unique for each row of the table. A foreign key, on the other hand, is a field in one table that link two tables together. It refers to a column or a group of columns that uniquely identifies a row of another table or same table.
Relation of Primary Key vs Foreign Key:
A primary key uniquely identifies a record in the relational database table, whereas a foreign key refers to the field in a table which is the primary key of another table. A primary key must be unique and only one primary key is allowed in a table which must be defined, whereas more than one foreign key are allowed in a table.
Duplicate Values of Primary Key vs Foreign Key:
A primary key is a combination of UNIQUE and Not Null constraints so no duplicate values can be allowed to have in a primary key field in a relational database table. No two rows are allowed to carry duplicate values for a primary key attribute. Unlike a primary key, foreign key can contain duplicate values and a table in a relational database can contain more than foreign key.
NULL of Primary Key vs Foreign Key:
One of the main differences between the two is that unlike primary keys, foreign keys can also contain NULL values. A table in a relational database can have only one primary key which does not allow NULL values.
Temporary Table of Primary Key vs Foreign Key:
A primary key constraint can be defined implicitly on temporary tables and their variables, whereas a foreign key constraint cannot be enforced on local or global temporary tables.
Deletion of Primary Key vs Foreign Key:
A primary key value cannot be deleted from the parent table which is referred to as a foreign key in the child table. You have to delete the child table first before removing the parent table. On the contrary, a foreign key value can be deleted from the child table even if the value is referred to the primary key of the parent table.