How do I add a check constraint to a table in MySQL?

MySQLMySQLi Database

To add a check constraint to a table, let us first create a table.

CREATE table yourTableName (
   Column_name1 datatyep,
   Column_name N datatype,
   check( condition)

The following is how you can check the age by creating a check constraint.


Let us now create a table and while creating a table, we will add the above shown check constraint for age.

mysql>CREATE table addCheckConstraintDemo
-> (
-> Age int,
-> check(Age>=45)
-> );
Query OK, 0 rows affected (0.55 sec)
Published on 20-Nov-2018 06:40:58