Sobes.tech
Junior — Middle

How do database management systems support referential integrity between tables?

sobes.tech AI

Answer from AI

DBMSs support referential integrity through foreign key constraints. A foreign key is a field or set of fields in one table that references the primary key of another table. The DBMS guarantees that the foreign key values exist in the referenced table, preventing referential integrity violations.

For example, if there is an Orders table with a foreign key CustomerID referencing the Customers table, you cannot insert an order with a non-existent customer, nor delete a customer if there are orders referencing them (unless cascade delete is configured).

An example of creating a foreign key in SQL:

ALTER TABLE Orders
ADD CONSTRAINT fk_customer
FOREIGN KEY (CustomerID) REFERENCES Customers(ID)
ON DELETE CASCADE;

Here, ON DELETE CASCADE means that when a customer is deleted, all related orders will be automatically deleted.

How do database management systems support… - sobes.tech