MySQL Tutorial
The CHECK
constraint in MySQL is used to enforce rules on the values in a column. It ensures that the data entered into a table meets specific conditions.
ALTER TABLE
.>
, <
, =
, AND
, OR
, etc.).CREATE TABLE table_name ( column1 data_type, column2 data_type, CONSTRAINT constraint_name CHECK (condition) );
age
must be 18 or older.score
must be within the range of 0 to 100.To allow naming of a CHECK
constraint, and for defining a CHECK
constraint on multiple columns, use the following SQL syntax:
chk_age_score
→ Ensures that:
chk_age
ensures that the student's age is 18 or above.To drop a CHECK
constraint, use the following SQL: