MySQL Check Constraint

Profile picture for user arilio666

When the CHECK constraint is applied to a column, it is responsible for not accepting data values outside the specified range. It can also be specified based on the conditions like less than, equal to, not, etc.

Add Check Constraint in MySQL

CREATE TABLE office (
    emp_ID int NOT NULL,
    emp_fName varchar(255) NOT NULL,
    emp_lName varchar(255) NOT NULL,
    Age int
    
    CHECK(Age > 50)
);

INSERT INTO office (emp_ID,emp_fName,emp_lName,Age)
VALUES(13,'Steven','Grant',51);

check constraint in MySQL

  • Here, we can see that the table was just created with a constraint CHECK of age not more than 50.
  • So when we try to input the age of 51, which is beyond 50 and is outside the specified range.
  • It won't allow us to input this age.
Tags