In this post, I will explain What is Primary Key, Unique Key and Foreign Key?
1. Primary Key
1. Primary Key
Primary Key provides uniqueness for a column or set of columns in a table and used to identify a row (a record) in a table. A database table can have only one Primary Key and a column with a Primary Key doesn't allow NULL value (Not nullable). Primary Key generates a Unique Clustered Index.
2. Unique Key
Unique Key provides uniqueness for a column or set of columns in a table like Primary Key. It is used to prevent duplicate values in a column. A database table can have more than one Unique Key and it allows at least one NULL value in a column( Nullable). Unique Key generates a Unique Non-Clustered Index.
3. Foreign Key
A Foreign Key is a Column (or Set of Columns) that references a column (most often the Primary Key) of another table. A table containing the Foreign Key is called a child table, and a table containing Primary Key is called a parent table.
Foreign Key is used to ensure referential integrity of the data in two tables and to prevent actions that would destroy links between tables. A Foreign Key also prevents invalid data from being inserted into a Foreign Key column, because it has to be one of the values contained in a parent table referenced column.
2. Unique Key
Unique Key provides uniqueness for a column or set of columns in a table like Primary Key. It is used to prevent duplicate values in a column. A database table can have more than one Unique Key and it allows at least one NULL value in a column( Nullable). Unique Key generates a Unique Non-Clustered Index.
3. Foreign Key
A Foreign Key is a Column (or Set of Columns) that references a column (most often the Primary Key) of another table. A table containing the Foreign Key is called a child table, and a table containing Primary Key is called a parent table.
Foreign Key is used to ensure referential integrity of the data in two tables and to prevent actions that would destroy links between tables. A Foreign Key also prevents invalid data from being inserted into a Foreign Key column, because it has to be one of the values contained in a parent table referenced column.