Skip to main content

SQL CHECK Constraint

How to validate table value using check constraints:
                        CHECK constraints will validate value by certain condition. The check constraint is used to limit the range of value in table column.

CREATE TABLE Employee (id int NOT NULL, AGE int, CHECK (AGE>20));
this statement allow employee should be greater than 20.

CHECK constraint on Multiple column

CREATE TABLE Employee (id int NOT NULL, AGE int,salary int, CHECK (AGE>20), CHECK(salary>15000));
this statement allow employee age should be greater than 20 and salary value should greater than 15000.

CHECK setting range value

CREATE TABLE Employee (id int NOT NULL, AGE int,salary int, CHECK (AGE>20), CHECK(salary between 10000 AND 100000));
this statement allow employee age should be greater than 20 and salary value between range 10000 and 100000.

CHECK by combining column

CHECK using ALTER  statement.
ALTER TABLE Employee ADD  CHECK(age >45 and dept_manager='manager') ;
This statement implies that manager age should greater than 45.



Thank You. Like, Share and Subscribe 👍 Followlect.com

Comments