Last Updated: 04 July, 2023
The CHECK keyword is used to create a check constraint in MySQL. A check constraint is a type of integrity constraint that specifies a search condition that must be met for all values inserted into a column or table. If a value does not meet the search condition, the insertion will be rejected.
The syntax for creating a CHECK constraint is as follows:
CREATE TABLE table_name ( column_name1 data_type CHECK (search_condition), column_name2 data_type CHECK (search_condition), ... );
For example, the following code creates a check constraint that ensures that the age column of the users table can only contain values between 18 and 65:
Let's see the example below:
CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, age INT CHECK (age >= 18 AND age <= 65), email VARCHAR(255) UNIQUE );
The CHECK keyword is a powerful tool that can be used to enforce data integrity in MySQL databases. By using check constraints, we can ensure that the data in the tables is always valid and consistent.
That's all, guys. I hope this MySQL article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com