Computer >> Computer tutorials >  >> Programming >> MySQL

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


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.

check(Age>=45)

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)