The CHECK constraint is used to limit the values that can be inserted into a column or set of columns. It ensures that the data meets a specified condition before it is allowed to be entered into the table. This can be used to enforce data integrity by restricting the type or range of values that are valid.
Key Characteristics of the CHECK Constraint:
- Column-Level or Table-Level: The
CHECKconstraint can be applied to a single column or to multiple columns in the table. - Enforces Data Integrity: It ensures that data entered into the table satisfies specific conditions.
- Range of Values: It is commonly used to ensure that the values in a column are within a valid range (e.g., ensuring an age column only accepts values greater than or equal to 18).
- Multiple Conditions: You can combine multiple conditions within a single
CHECKconstraint.
Using the CHECK Constraint
1. CHECK Constraint on Table Creation
You can define the CHECK constraint when you first create the table. It ensures that only values satisfying the condition are allowed for the column(s).
Example: CHECK Constraint on a Single Column
CREATE TABLE Persons (
ID INT NOT NULL,
LastName VARCHAR(255) NOT NULL,
FirstName VARCHAR(255),
Age INT,
CHECK (Age >= 18)
);
In this example:
- The
CHECKconstraint ensures that theAgecolumn only accepts values 18 or older.
Example: Named CHECK Constraint on Multiple Columns
CREATE TABLE Persons (
ID INT NOT NULL,
LastName VARCHAR(255) NOT NULL,
FirstName VARCHAR(255),
Age INT,
City VARCHAR(255),
CONSTRAINT CHK_Person CHECK (Age >= 18 AND City = 'Sandnes')
);
In this example:
- The
CHECKconstraint is namedCHK_Personand ensures thatAgemust be 18 or older, andCitymust be'Sandnes'.
2. CHECK Constraint on Existing Table
You can add a CHECK constraint to an existing table using the ALTER TABLE statement.
Example: Add CHECK Constraint on a Column
ALTER TABLE Persons
ADD CHECK (Age >= 18);
In this example:
- The
CHECKconstraint is added to theAgecolumn, ensuring that only values 18 or older can be inserted into theAgecolumn.
Example: Named CHECK Constraint on Multiple Columns
ALTER TABLE Persons
ADD CONSTRAINT CHK_PersonAge CHECK (Age >= 18 AND City = 'Sandnes');
In this example:
- The
CHECKconstraint is namedCHK_PersonAgeand ensures thatAgemust be 18 or older, andCitymust be'Sandnes'.
3. Dropping a CHECK Constraint
If you no longer need a CHECK constraint, you can remove it using the ALTER TABLE statement.
Example: Drop a CHECK Constraint
ALTER TABLE Persons
DROP CHECK CHK_PersonAge;
In this example:
- The
CHECKconstraint namedCHK_PersonAgeis dropped from thePersonstable.
Important Notes:
- Compatibility: While MySQL supports the
CHECKconstraint, it historically ignoredCHECKconstraints in earlier versions. Starting with MySQL 8.0.16, MySQL began enforcingCHECKconstraints. Make sure to check the version of MySQL you are using. - Performance: The
CHECKconstraint helps ensure data integrity, but excessive use of complex checks can impact performance, especially when the table grows large. - Multiple Conditions: The
CHECKconstraint allows you to define complex conditions, such as ensuring one column's value is greater than another or that a column's value falls within a specific range.
Summary of CHECK Constraint Usage:
- On Table Creation: The
CHECKconstraint can be defined when creating a table to enforce specific value limits on columns. - On Table Modification: You can add a
CHECKconstraint to an existing table using theALTER TABLEstatement. - Dropping a CHECK Constraint: If no longer needed, the
CHECKconstraint can be dropped with theALTER TABLEstatement.
The CHECK constraint is a powerful tool for maintaining data integrity by ensuring that the data entered into the table adheres to business rules or requirements.