-
NOT NULL
Ensures that a column cannot have a NULL (empty) value.
CREATE TABLE users (
id INT,
name VARCHAR(100) NOT NULL
);
-
UNIQUE
Ensures all values in a column are unique (no duplicates).
CREATE TABLE employees (
email VARCHAR(100) UNIQUE
);
-
PRIMARY KEY
A combination of NOT NULL and UNIQUE. Uniquely identifies each row in a table.
CREATE TABLE students (
student_id INT PRIMARY KEY,
name VARCHAR(100)
);
-
FOREIGN KEY
Ensures referential integrity between two tables. It links a column in one table to the PRIMARY KEY of another.
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT,
FOREIGN KEY (customer_id) REFERENCES customers(id)
);
-
CHECK
Ensures that all values in a column meet a specific condition.
CREATE TABLE accounts (
balance DECIMAL(10,2),
CHECK (balance >= 0)
);
-
DEFAULT
Sets a default value for a column when no value is specified.
CREATE TABLE products (
id INT,
status VARCHAR(10) DEFAULT 'active'
);