Why are use the SQL CHECK Constraint in the database at table creation?
Why are use the SQL CHECK Constraint in the database at table creation?
43922-Sep-2021
Updated on 22-Sep-2021
Home / DeveloperSection / Forums / Why are use the SQL CHECK Constraint in the database at table creation?
Ashutosh Kumar Verma
27-Sep-2021SQL Check Constraint:
CHECK constraint is used to give a limit for the values to be passed on the column.
Data is inserted on the column according to the condition on which the CHECK constraint is given. A check constraint is used in SQL server allow you to specify condition on each row in a table.
CHECK constraints are column-level (single column) and table-level (multiple columns).
Check constraint is create two times, first when database table is created and other is alter table in exists table in database
1- SQL Check Constraint on table create:
The following SQL creates a Check constraint on the 'Age' column when the 'Student' table is created. The Check constraint ensures that the age of a Student must be 18, or older:
Check constraint on multiple columns in a table
2- SQL Check Constraint on created table:
The following SQL creates a Check Constraint on student Age in “Student” table which is created in database. Here the Check Constraint is used with SQL ALTER command. Ex- for single column in table,
For multiple columns of a table,
In the above Student table the records entered in which stuAge must be 18 or above and stuID is more than 3.
Drop Check Constraint:
To Drop a created Check Constraint in table, use the following SQL statement,
Other one is-