FOREIGN KEY
FOREIGN KEY is a SQL statement in the Constraints category. Ensures referential integrity: values in this column must exist in the referenced parent table. The syntax is FOREIGN KEY (column) REFERENCES parent_table(parent_column) [ON DELETE CASCADE]. It returns constraint. A typical example: CREATE TABLE orders ( id INT PRIMARY KEY, user_id INT, FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE ); -- CASCADE: delete orders when user is deleted A close relative is PRIMARY KEY, which a column or set of columns that uniquely identifies each row. Only one PK per table. Automatically creates a unique index. A close relative is UNIQUE, which ensures all values in a column (or combination) are unique across the table. Allows NULLs (multiple NULLs allowed). A close relative is NOT NULL, which ensures a column cannot contain NULL values. More about this category: Table constraints — PRIMARY KEY, FOREIGN KEY, UNIQUE, NOT NULL, CHECK, DEFAULT.