about 9 years ago
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)
);
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.
We can easily add Check constraint using Alter command.
0 Comment(s)