Home / SQL / Reference / Tables and design

SQL FOREIGN KEY

Make a column refer to a row in another table.

Syntax

col TYPE REFERENCES other(key)

The database rejects values that do not exist in the other table, and (by default) deleting rows that are still referred to.

Example

Query (on the shop database used in the SQL course)

PRAGMA foreign_key_list(orders);

Result

idseqtablefromtoon_updateon_deletematch
00customerscustomer_idcustomer_idNO ACTIONNO ACTIONNONE

1 row

Related

Learn it step by step in the free lesson Creating Tables, with exercises that check your queries.