Last Updated: 23-02-2021
Primary Key:
A primary key is used to ensure data in the specific column is unique. It is a
column that cannot have NULL values. It is either an existing table column or a
column that is specifically generated by the database according to a defined
sequence.
Example: Refer to the figure –
STUD_NO, as well as STUD_PHONE both, are candidate keys for relation
STUDENT but STUD_NO can be chosen as the primary key (only one out of
many candidate keys).
column that cannot have NULL values. It is either an existing table column or a
column that is specifically generated by the database according to a defined
sequence.
Example: Refer to the figure –
STUD_NO, as well as STUD_PHONE both, are candidate keys for relation
STUDENT but STUD_NO can be chosen as the primary key (only one out of
many candidate keys).
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. It is a column (or columns)
that references a column (most often the primary key) of another table.
Example: Refer to the figure –
STUD_NO in STUDENT_COURSE is a foreign key to STUD_NO in
STUDENT relation.
A foreign key is a column or group of columns in a relational database table
that provides a link between data in two tables. It is a column (or columns)
that references a column (most often the primary key) of another table.
Example: Refer to the figure –
STUD_NO in STUDENT_COURSE is a foreign key to STUD_NO in
STUDENT relation.
Figure:
Let’s see the difference between Primary Key and Foreign Key:
PRIMARY KEY
- A primary key is used to ensure data in the specific column is unique.
- It uniquely identifies a record in the relational database table.
- Only one primary key is allowed in a table.
- It is a combination of UNIQUE and Not Null constraints.
- It does not allow NULL values.
- Its value cannot be deleted from the parent table. It constraint can be implicitly defined on the temporary tables.
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.
- It refers to the field in a table which is the primary key of another table.
- Whereas more than one foreign key is allowed in a table.
- It can contain duplicate values and a table in a relational database.
- It can also contain NULL values.
- Its value can be deleted from the child table.
Difference between Primary and Candidate Key
Both Primary Key and Candidate Key are the attributes that are used to
access tuples from a table. These(Primary key and Candidate key) are also
can be used to create a relationship between two tables.
Primary Key :
Primary Key is a set of attributes (or attributes) that uniquely identify the
tuples in relation to or table. The primary key is a minimal super key, so there
is one and only one primary key in any relationship. For example,
Student{ID, F_name, M_name, L_name, Age}
Here only ID can be the primary key because the name, age, and address can
be the same but ID can’t be the same.
Candidate Key:
A candidate key is a set of attributes (or attributes) that uniquely identify the
tuples in relation or table. As we know the Primary key is a minimal super
key, so there is one and only one primary key in any relationship but there is
more than one candidate key that can take place. Candidate key’s attributes can
contain a NULL value that opposes the primary key. For example,
Student{ID, First_name, Last_name, Age}
Here we can see the two candidate keys ID and {First_name, Last_name,
DOB}. So here, there are present more than one candidate keys, which can
uniquely identify a tuple in a relation.

Comments
Post a Comment
Please, do not enter any spam link in the comment box.