Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (55.6k points)

Can anyone explain the check constraint in MySQL?

1 Answer

0 votes
by (119k points)
edited by

In MySQL, CHECK constraint allows us to limit the value range that can be inserted in a column.

If you define a CHECK constraint on a single column, we can insert only certain values in this column.

If you define a CHECK constraint on a table it allows us to limit the values that can be inserted in certain columns based on values in other columns in the row.

Here is an example of how to limit the values to less than 18 in Age column:

CREATE TABLE Persons (
    ID int NOT NULL,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255),
    Age int,
    CHECK (Age>=18)
);

If you want to learn MySQL from top professionals, I recommend this SQL Course by Intellipaat.

Also, watch this video on MySQL:

Related questions

0 votes
1 answer
0 votes
1 answer
asked Jan 7, 2021 in SQL by Appu (6.1k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...