Foreign Key
PronunciationFAWR-uh-n KEE
Definition
A field or group of fields in one table that uniquely identifies a row of another table, used to link two tables together and enforce referential integrity.
Where you hear it
During database design, writing SQL constraints, or discussing table relationships.
Examples
The
orderstable includes a foreign key that references theuserstable.A foreign key prevents the database from deleting a customer who still has active purchases.
The payments table uses a foreign key to point at the order it pays for.
Common mistake
Assuming a foreign key automatically creates an index for fast lookups, which is not true for all database systems and often needs to be created manually.
Don't confuse with
Foreign Key vs. Primary Key: A primary key uniquely identifies a record within its own table, whereas a foreign key is used to establish a link between data in two different tables.
Say it at work
- In a meeting or chat
I think we need to add a foreign key to the logs table so we can track which user triggered each event.
- In an email or ticket
Please ensure that the foreign key constraint is properly defined in the migration file to maintain referential integrity between these two tables.