CHECK Constraint in SQL - sql - sql tutorial - learn sql

- The CHECK constraint is used to limit the value range that can be placed in a column.
- If you define a CHECK constraint on a single column it allows only certain values for this column.
- If you define a CHECK constraint on a table it can limit the values in certain columns based on values in other columns in the row.
- The CHECK constraint ensures that all values in a column satisfy certain conditions.
- The database will only insert a new row or update an existing row if the new value satisfies the CHECK constraint.
- The CHECK constraint is used to ensure data quality.
- For example, in the following CREATE TABLE statement,
CREATE TABLE Student
(SID integer CHECK (SID > 0),
Last_Name varchar (30),
First_Name varchar(30));
- Column "SID" has a constraint - its value must only include integers greater than 0. So, attempting to execute the following statement,
INSERT INTO Student VALUES (-3, 'wikitechy', 'Tutorials');
- will result in an error because the values for SID must be greater than 0.
- Please note that the CHECK constraint does not get enforced by MySQL at this time.
