Constraints
CREATE TABLE employees (
id INT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(100) UNIQUE,
birth_date DATE CHECK (birth_date > '1900-01-01'),
hire_date DATE DEFAULT CURRENT_DATE,
department_id INT,
FOREIGN KEY (department_id) REFERENCES departments(id)
); When creating tables, you can apply various constraints to the columns to enforce rules.
- PRIMARY KEY: Uniquely identifies each record in a table.
- FOREIGN KEY: Uniquely identifies a record in another table.
- NOT NULL: Ensures that a column cannot have a NULL value.
- UNIQUE: Ensures that all values in a column are different.
- CHECK: Ensures that all values in a column satisfy a specific condition.
- DEFAULT: Sets a default value for a column when no value is specified.
Semantic portal