Foreign keys keep related data honest.
Direct answer
A foreign key is a column in one table that points to the primary key in another, linking the two. The database enforces that every value in the foreign key exists in the referenced table, which keeps data consistent. You choose what happens on delete: block, cascade or set to NULL.
Example
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL,
total REAL,
FOREIGN KEY (customer_id) REFERENCES customers(id)
ON DELETE RESTRICT
);
Relationships
| Type | Example |
|---|---|
| One to many | one customer, many orders |
| Many to many | students and courses, using a join table |
| One to one | user and profile |
Delete behaviour
| Option | Result |
|---|---|
RESTRICT | block the delete |
CASCADE | delete related rows |
SET NULL | clear the reference |
Where Teyro fits
Teyro's SQL and Databases for Beginners course turns this into short lessons you can play in a few minutes a day. It has 50 lessons, and you can start free.
The bottom line
Foreign keys make relationships real and protect you from orphan rows.


