PostgreSQL Tutorial
Data Types
Querying & Filtering Data
Managing Tables
Modifying Data
Conditionals
Control Flow
Transactions & Constraints
Working with JOINS & Schemas
Roles & Permissions
Working with Sets
Subquery & CTEs
User-defined Functions
Important In-Built Functions
PostgreSQL PL/pgSQL
Variables & Constants
Stored Procedures
Working with Triggers
Working with Views & Indexes
Errors & Exception Handling
In PostgreSQL, the CHECK
constraint is used to specify a condition that the data in a table must satisfy. Each row in the table must ensure that the condition specified by the CHECK
constraint evaluates to true
for the row to be accepted. If the condition evaluates to false
for a particular row, that row will be rejected.
You can specify the CHECK
constraint when you're creating a table.
CREATE TABLE employees ( id SERIAL PRIMARY KEY, age INTEGER, salary DECIMAL(10, 2) CHECK (salary > 0), CHECK (age > 18) );
In the example above:
CHECK
constraint to an existing table:You can add a CHECK
constraint to an existing table using the ALTER TABLE
command.
ALTER TABLE employees ADD CONSTRAINT chk_salary CHECK (salary > 1000 AND salary < 50000);
This ensures that the salary of an employee is always between 1000 and 50000.
CHECK
constraint:If you wish to remove a CHECK
constraint, you can use the ALTER TABLE
command.
ALTER TABLE employees DROP CONSTRAINT chk_salary;
Multiple CHECK
Constraints: A table can have multiple CHECK
constraints. Each constraint must have a unique name within the table.
NULL Values: If the condition of the CHECK
constraint evaluates to NULL
, the row will be allowed. For example, if a column has a CHECK
constraint that it should be greater than 10, and you insert a NULL
value, it will be accepted.
Performance: Be aware that adding a CHECK
constraint can impact the performance of data insertion, especially if the condition is complex. However, the impact is generally minimal for most scenarios.
Data Integrity: If you add a CHECK
constraint to an existing table, the current data must satisfy the new constraint, or the command will fail.
In summary, the CHECK
constraint in PostgreSQL is a powerful feature to ensure data integrity by making sure that the data in the table adheres to specified conditions.
Creating CHECK constraints in PostgreSQL:
CREATE TABLE my_table ( my_column INTEGER CHECK (my_column > 0) );
Enforcing data integrity with CHECK constraints in PostgreSQL:
ALTER TABLE my_table ADD CONSTRAINT positive_value CHECK (my_column > 0);
Multiple conditions in a CHECK constraint in PostgreSQL:
ALTER TABLE my_table ADD CONSTRAINT range_constraint CHECK (my_column BETWEEN 1 AND 100);
CHECK constraints vs. triggers in PostgreSQL:
CREATE TABLE my_table ( my_column INTEGER CHECK (my_column > 0) );
CREATE OR REPLACE FUNCTION check_my_column() RETURNS TRIGGER AS $$ BEGIN IF NEW.my_column <= 0 THEN RAISE EXCEPTION 'my_column must be greater than 0'; END IF; RETURN NEW; END; $$ LANGUAGE plpgsql; CREATE TRIGGER check_my_column_trigger BEFORE INSERT OR UPDATE ON my_table FOR EACH ROW EXECUTE FUNCTION check_my_column();
Disabling and enabling CHECK constraints in PostgreSQL:
ALTER TABLE my_table DISABLE CONSTRAINT positive_value;
ALTER TABLE my_table ENABLE CONSTRAINT positive_value;
Naming conventions for CHECK constraints in PostgreSQL:
ALTER TABLE my_table ADD CONSTRAINT positive_value CHECK (my_column > 0);
CHECK constraints on multiple columns in PostgreSQL:
CREATE TABLE my_table ( column1 INTEGER, column2 INTEGER, CHECK (column1 + column2 > 100) );