Glossary · Database

Foreign Key

A column in one table that references the primary key of another table, establishing a link between them.

A foreign key is a column or set of columns in a table that references the primary key of another table, creating a relationship between the two tables. Foreign keys enforce referential integrity–ensuring that a value in the foreign key column must exist in the referenced primary key column. This prevents orphaned records and maintains data consistency. Foreign key constraints can specify cascading actions: CASCADE (automatically delete/update related records), SET NULL (set foreign key to NULL), RESTRICT (prevent the operation), or NO ACTION. Understanding foreign key relationships is crucial for text-to-SQL systems to generate correct joins.

In practice

How AI for Database applies it

AI for Database maps all foreign key relationships in your schema, enabling it to automatically join the right tables when answering multi-table questions.
Database · aifordatabase glossaryRead-only ✓

Fig — every answer ships with the tables, rows and SQL behind it.

See it on your own database

Connect read-only in minutes. Free models included.

Start free