DEV Community

loading...
Cover image for Primary Keys & Foreign Keys

Primary Keys & Foreign Keys

Mahmoud EL-kariouny
Full Stack Web Developer
・2 min read

What Is Primary Key?

⦁ The primary key is the unique identifier for the entire row, referring to one or more columns.

⦁ if there are more multiple columns for the primary key, then the set of primary key columns is known as a composite key.

What Is Foreign Key?

⦁ A primary key in another (foreign) table.

⦁ Foreign keys are used to map relationships between tables.

Primary Key vs Foreign Key

1 -

Primary Key

⦁ A primary key is used to ensure data in the specific column is unique.

Foreign Key

⦁ A foreign key is a column or group of columns in a relational database table that provides a link between data in two tables

2 -

Primary Key

⦁ It uniquely identifies a record in the relational database table.

Foreign Key

⦁ It refers to the field in a table which is the primary key of another table.

3 -

Primary Key

⦁ Only one primary key is allowed in a table.

Foreign Key

⦁ Whereas more than one foreign key is allowed in a table.

4 -

Primary Key

⦁ It is a combination of UNIQUE and Not Null constraints.

Foreign Key

⦁ It can contain duplicate values and a table in a relational database.

5 -

Primary Key

⦁ It does not allow NULL values.

Foreign Key

⦁ It can also contain NULL values.

6 -

Primary Key

⦁ Its value cannot be deleted from the parent table.

Foreign Key

⦁ Its value can be deleted from the child table.

7 -

Primary Key

⦁ Its constraint can be implicitly defined on the temporary tables.

Foreign Key

⦁ Its constraint cannot be defined on the local or global temporary tables.

Note :

  • The relational databases support many keys on It.

  • Like (Super Key - Candidate Key - Composite Key - Surrogate Key - Alternate Key)

Discussion (0)