SQL | CHECK Constraint

21 Mar, 2018
SQL ConstraintsCheck Constraint is used to specify a predicate that every tuple must satisfy in a given relation. It limits the values that a column can hold in a relation.
The predicate in check constraint can hold a sub query.
Check constraint defined on an attribute restricts the range of values for that attribute.
If the value being added to an attribute of a tuple violates the check constraint, the check constraint evaluates to false and the corresponding update is aborted.
Check constraint is generally specified with the CREATE TABLE command in SQL.
Syntax:
CREATE TABLE pets(
        ID INT NOT NULL,
        Name VARCHAR(30) NOT NULL,
        Breed VARCHAR(20) NOT NULL,
        Age INT,
        GENDER VARCHAR(9),
        PRIMARY KEY(ID),
        check(GENDER in ('Male', 'Female', 'Unknown'))
        );
Note: The check constraint in the above SQL command restricts the GENDER to belong to only the categories specified. If a new tuple is added or an existing tuple in the relation is updated with a GENDER that doesn’t belong to any of the three categories mentioned, then the corresponding database update is aborted.
Query
Constraint: Only students with age >= 17 are can enroll themselves in a university.Schema for student database in university:
CREATE TABLE student(
        StudentID INT NOT NULL,
        Name VARCHAR(30) NOT NULL,
        Age INT NOT NULL,
        GENDER VARCHAR(9),
        PRIMARY KEY(ID),
        check(Age >= 17)
        );
Student relation:
Explanation: In the above relation, the age of all students is greater than equal to 17 years, according to the constraint mentioned in the check statement in the schema of the relation. If, however following SQL statement is executed:
INSERT INTO student(STUDENTID, NAME, AGE, GENDER) 
VALUES (1006, 'Emma', 16, 'Female');
There won’t be any database update and as the age < 17 years.
Different options to use Check constraint:
With alter: Check constraint can also be added to an already created relation using the syntax:alter table TABLE_NAME modify COLUMN_NAME check(Predicate);
alter table TABLE_NAME modify COLUMN_NAME check(Predicate);
Giving variable name to check constraint:Check constraints can be given a variable name using the syntax:alter table TABLE_NAME add constraint CHECK_CONST check (Predicate);
alter table TABLE_NAME add constraint CHECK_CONST check (Predicate);
Remove check constraint: Check constraint can be removed from the relation in the database from SQL server using the syntax:alter table TABLE_NAME drop constraint CHECK_CONSTRAINT_NAME;
alter table TABLE_NAME drop constraint CHECK_CONSTRAINT_NAME;
Drop check constraint: Check constraint can be dropped from the relation in the database in MySQL using the syntax:alter table TABLE_NAME drop check CHECK_CONSTRAINT_NAME;
alter table TABLE_NAME drop check CHECK_CONSTRAINT_NAME;
This article is contributed by Mayank Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
SQL-Clauses-Operators
SQL
SQL
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.