CREATE TABLE STUDENT (
column1 datatype(size),
column2 datatype(size),
column3 datatype(size),
CHECK (condition)
);
Example
CREATE TABLE STUDENT (
ID int ,
Name varchar(255) ,
Age int,
CHECK (Age>=18)
);
- As we have used a check constraint as (Age>=18) which means values entered by the user for this age column while inserting the data must be less than or equal to 18 otherwise an error is shown
- Simply, the only possible values that the age column will accept is [0 -17].
Check constraint at table level
Example
CREATE TABLE student {
ID int ,
Name varchar(255) ,
Program interface specificationAge int,
City varchar(255),
CONSTRAINT CHK_Person CHECK (Age>=18 AND City='hyderabad')
);
- You can apply check constraint to more than one column at a time
- In the above example we have used two conditions with the check constraint Age>=18 AND City=’ Hyderabad’ , as a result, both the columns age and city will be checked while data is entered i.e the only possible values that are accepted for the column age are 0 -17 and city must be only Hyderabad