The value range that can be entered into a column is restricted by the CHECK constraint.
A column that has a CHECK constraint defined on it will only accept specific values.
A table’s CHECK constraint might restrict values in specific columns according to values in other columns in the row.
Upon creating the “Persons” table, the following SQL places a CHECK constraint on the “Age” field. The CHECK constraint guarantees that an individual must be eighteen years of age or older:
Use the following SQL syntax to define a CHECK constraint on multiple columns and to allow naming of the constraint:
When the table has already been established, use the following SQL to create a CHECK constraint on the “Age” column:
ALTER TABLE Persons
ADD CHECK (Age>=18);
Use the following SQL syntax to define a CHECK constraint on multiple columns and to allow naming of the constraint:
ALTER TABLE Persons
ADD CONSTRAINT CHK_PersonAge CHECK (Age>=18 AND City='Sandnes');
To remove a CHECK constraint, use the SQL code below:
ALTER TABLE Persons
DROP CONSTRAINT CHK_PersonAge;
ALTER TABLE Persons
DROP CHECK CHK_PersonAge;
CodingAsk.com is designed for learning and practice. Examples may be made simpler to aid understanding. Tutorials, references, and examples are regularly checked for mistakes, but we cannot guarantee complete accuracy. By using CodingAsk.com, you agree to our terms of use, cookie, and privacy policy.
Copyright 2010-2024 by Refsnes Data. All Rights Reserved.