IKH

Referential Constraints

In the previous segment, you learnt about the first type of constraints, entity constraints, which pertain to the values in a single table. The second type of constraints is called referential constraints. These are used to restrict the values that are taken by a column in one table based on the values that exist in another table.

Consider the tables given below.

Customers

CustomerIDFirstNameLastNameAge
1JohnWick52
2SheldonCooper41
3CharlieHarper45

Orders

OrderIDOrderNumberCustomerID
1
12345
2
2
31343
2
3
12466
3
4
41234
1

CustomerID is a foreign key in the ‘Orders’ table because it is used to reference the ‘Customers’ table. You can easily determine which customer placed a particular order and find out all the details of that customer by looking up the corresponding customer id in the ‘Customers’ table. Watch the upcoming videos to understand how foreign keys make it easier to design and query databases.

So, as you learnt in the video, a referential constraint is a rule between two tables. According to this rule, the value that appears as a foreign key in a table is valid only if it also appears as a primary key in the table to which it refers.

To sum up, a given table has only one primary key but it can have multiple foreign keys. Before you assign a column as a foreign key, you need to ensure that the primary key column of the table that it refers to is present and it does not have null or duplicate values.

In the next segment, you will learn about the semantic constraints.

Report an error