MySQL Wildcards

MySQL Wildcards

MySQL Aliases

MySQL Aliases

MySQL Data Types

MySQL Data Types

MySQL Interview Questions

MySQL Interview Questions and Answers


MySQL CHECK Constraint

In MySQL, CHECK constraint controls the values in the associated column. In other words, CHECK constraints ensures that the value in a certain column satisfy a Boolean expression or not.

MySQL CHECK Constraint example

The following MySQL, creates a CHECK constraint on the "Age" column when the "tblCustomer" table is created. The CHECK constraint ensures that the customer's age greater or equal to 18 years:

Example

CREATE TABLE tblCustomer (
    custId int NOT NULL,
    firstName varchar(255) NOT NULL,
    lastName varchar(255) NOT NULL,
    Age int,
    CHECK (Age>=18)
);

MySQL CHECK Constraint on ALTER table example

The following MySQL, creates a CHECK constraint on the "Age" column when table already exists into the database:

Example

ALTER TABLE tblCustomer
ADD CHECK (Age>=18);

OR

ALTER TABLE tblCustomer
ADD CONSTRAINT CHK_CustomerAge CHECK (Age>=18);

MySQL DROP a CHECK Constraint example

The following MySQL, dropping CHECK constraint "UC_Customer" from "tblCustomer" table:

Example

ALTER TABLE tblCustomer
DROP CHECK CHK_CustomerAge;