SQL: Creating Databases and Tables - CHECK Constraint

CHECK Constraint

The CHECK constraint specifies if a value in a column must meet a specific requirement (Boolean expression).

Example: Check Constraint

-- Create Table with CHECK Constraints
CREATE TABLE new_users(
id serial PRIMARY KEY,
first_name VARCHAR(50), 
birth_date DATE CHECK(birth_date > '1900-01-01'),
join_date DATE CHECK (join_date > birth_date),
salary integer CHECK(salary > 0)
);
## data frame with 0 columns and 0 rows
-- Select
SELECT * FROM new_users;
## data frame with 0 columns and 0 rows
-- INSERT, Should result in an error
INSERT INTO new_users(first_name,birth_date,join_date,salary)
VALUES ('Joe', '1980-02-02', '1990-04-04', -10);
## NULL