SQL Constraints
Constraints are rules declared on table columns that the database enforces on every insert and update, rejecting data that breaks them. They keep data valid without relying on application code.
The common constraints are NOT NULL (a value is required), UNIQUE (no duplicates), PRIMARY KEY (unique and not NULL, identifies the row), FOREIGN KEY (must match a row in another table), CHECK (must satisfy a condition, such as price > 0) and DEFAULT (the value used when none is given).
CREATE TABLE products (
id INT PRIMARY KEY,
sku VARCHAR(20) NOT NULL UNIQUE,
price DECIMAL(10, 2) CHECK (price > 0),
in_stock BOOLEAN DEFAULT TRUE,
cat_id INT REFERENCES categories(id)
);
