What Is Primary Key
What Is Primary Key
2 The table allows only one primary key. Tables can allow more than
one foreign key.
3 The primary key doesn’t allow null The foreign key accepts
values. multiple null values.
4 It can identify the record uniquely in the A foreign key is a field in the
database table. table that is the primary key
in another table.
5 In the primary key, the value cannot be In this, the value can be
removed from the parent table. deleted from the child table.
6 Its restriction can be completely defined Its restriction cannot be
on the temporary tables. defined on the global or local
temporary tables.
The primary key column has a unique value and doesn’t store repeating
values. A Primary key can never take NULL values.
Let us now understand the Syntax of creating the table with the Primary key
specified.
Syntax:
A foreign key is a field or collection of fields in a table that refers to the Primary
key of the other table. It is responsible for managing the relationship between
the tables.
The table which contains the foreign key is often called the child table, and the
table whose primary key is being referred by the foreign key is called the Parent
Table.
For example: When we talk about students and the courses they have enrolled
in, now if we try to store all the data in a single table, the problem of redundancy
arises.
To solve this table, we make two tables, one the student detail table and the
other department table. In the student table, we store the details of students
and the courses they have enrolled in.
And in the department table, we store all the details of the department. Here
the courseId acts as the Primary key for the department table whereas it acts as
the Foreign key in the student table.
Let us now look at the syntax of creating a table with a foreign key.