Junior — Middle
What is the role of foreign key constraints in a relational database?
sobes.tech AI
Answer from AI
A foreign key constraint in a relational database ensures data integrity between related tables. It guarantees that the value in the foreign key column corresponds to an existing value in the related table (usually the primary key).
The role of the constraint:
- Prevents insertion or update of records with non-existent references.
- Ensures data consistency between tables.
- Allows automatic cascading of operations (deletion or update) for related records.
For example, if there is an Orders table with a foreign key to Customers, then it is not possible to create an order for a non-existent customer.