Check Constraint:
It is used to restrict the range of values that can be inserted in a column. If a column contain check constraint then only a particular range of values can be inserted in that column.
Example:
CREATE TABLE Employee(
Emp_ID INT NOT NULL,
Emp_NAME VARCHAR (20) NOT NULL,
Emp_AGE INT NOT NULL CHECK (Emp_AGE >= 18),
Emp_ADDRESS CHAR (25) ,
Emp_SALARY DECIMAL (18, 2),
PRIMARY KEY (Emp_ID)
);
In this example we have put check constraint on Emp_Age column and we cannot insert Employee whose age is less than 18.
Adding Check Constraint using Alter Command:
We can easily add Check constraint using Alter command.
ALTER TABLE Employee
ADD CONSTRAINT myCheck CHECK (Emp_AGE >= 18 );
Drop a Check Constraint:
ALTER TABLE Employee
DROP CONSTRAINT myCheck;
0 Comment(s)