English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Usage and examples of SQL CHECK keyword

SQL Keyword Reference

CHECK

CHECK constraints limit the values that can be placed in a column.

SQL CHECK on CREATE TABLE

The following SQL creates a CHECK constraint on the 'Age' column when creating the 'Persons' table. The CHECK constraint ensures that you cannot have18for anyone under the age of:

MySQL:

CREATE TABLE Persons
(
Age int,
CHECK (Age>=18)
);

SQL Server / Oracle / MS Access:

CREATE TABLE Persons (
    Age int CHECK (Age>=18)
);
If you want to name a CHECK constraint and define CHECK constraints on multiple columns, use the following SQL syntax:

MySQL / SQL Server / Oracle / MS Access:

CREATE TABLE Persons (
    Age int,
    City varchar(255)
    CONSTRAINT CHK_Person CHECK (Age>=18 AND City='Sandnes')
);

SQL CHECK for ALTER TABLE

Please use the following SQL to create a CHECK constraint on a column after the table has been created in the 'Age' column:

MySQL / SQL Server / Oracle / MS Access:

ALTER TABLE Persons
ADD CHECK (Age>=18);
If you want to name a CHECK constraint and define CHECK constraints on multiple columns, use the following SQL syntax:

MySQL / SQL Server / Oracle / MS Access:

ALTER TABLE Persons
ADD CONSTRAINT CHK_PersonAge CHECK (Age>=18 AND City='Sandnes');

Delete CHECK constraint

To delete a CHECK constraint, use the following SQL:

SQL Server / Oracle / MS Access:

ALTER TABLE Persons
DROP CONSTRAINT CHK_PersonAge;

MySQL:

ALTER TABLE Persons
DROP CHECK CHK_PersonAge;

SQL Keyword Reference