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
| id | seq | table | from | to | on_update | on_delete | match |
|---|---|---|---|---|---|---|---|
| 0 | 0 | customers | customer_id | customer_id | NO ACTION | NO ACTION | NONE |
1 row
Related
Learn it step by step in the free lesson Creating Tables, with exercises that check your queries.