Referential Integrity constraints and Foreign Keys

For Complete YouTube Video: Click Here

In this class, we will understand Referential Integrity constraints and Foreign Keys.

We have already discussed the concepts of domain constraints, key constraints, and NULL constraints.

Referential Integrity constraints and Foreign Keys

So far, all the constraints are on a single table, but the referential integrity is made on two tables.

What is a referential integrity constraint?

A Referential Integrity Constraint is specified between two relations and is used to maintain the consistency among the tuples in the two relations.

To understand this, we will consider the two relations with a referential integrity constraint, as shown below.

Referential Integrity constraints and Foreign Keys
Referential Integrity constraints and Foreign Keys

In the above image, the arrow mark represents that the values of the Dno of the employee table are referencing the values in the department table.

For better understanding, we will insert a new employee table as shown below.

Referential Integrity constraints and Foreign Keys 2
Referential Integrity constraints and Foreign Keys 2

Is the new row insertion consistent?

No, the Dno 2 does not exist, but we are trying to insert an employee working for department 2.

By establishing a relation, we can maintain consistency and integrity among the tables of the database.

The referential integrity constraint is established by using a foreign key keyword.

In the next class, we will implement a referential integrity constraint.