What is Foreign Key?

Turkish: Foreign Key

A foreign key links a value in one table to a key in another table, preserving referential integrity in relational databases.

What is a Foreign Key?

A foreign key is a database constraint that makes a row in one table refer to a valid row in another table. For example, orders.customer_id may reference customers.id, meaning each order should belong to an existing customer.

How Does It Work?

In SQL databases, a foreign key definition describes the relationship between a child table and a parent table. The database can prevent an order from being created for a non-existent customer, or prevent a customer from being deleted while related orders still exist.

Delete and update behavior depends on the rule. RESTRICT blocks the operation, CASCADE updates or deletes related rows too, and SET NULL clears the reference. The right choice depends on the business rule.

Why It Matters

Foreign keys prevent orphaned records in reporting and operational screens. Orders without customers, invoice lines without invoices, or comments without users can damage data quality and make business reports unreliable.

In relational models shaped through normalization, foreign keys are a core tool. Related columns should also be indexed, and bulk imports or deletion strategies need to account for these constraints.