Interview Question

What is a foreign key?

A foreign key keeps references aligned with a candidate key in another table.

💡 Concept ✅ Quick Revision 🗃️ SQL

Answer

A foreign key requires values to match a referenced unique or primary key. • It preserves referential integrity between related tables. • The referencing value may be NULL unless another constraint forbids it. • Update and delete actions define what happens when the referenced row changes.

💡 SQL Example

CREATE TABLE orders (order_id integer PRIMARY KEY, customer_id integer REFERENCES customers(customer_id));

Result

CREATE TABLE

⚡ Quick Revision

A foreign key keeps references aligned with a candidate key in another table.