MySQL
CHECK Constraint
The CHECK constraint in MySQL is used to enforce rules on the data values in a table. It ensures that the values in a column meet specific conditions, helping maintain data integrity and consistency. For example, you can use CHECK constraints to ensure that age values are positive, or that a column value falls within a certain range. Below is a guide on using the CHECK constraint in MySQL, with code samples for tables related to a company database, including company, employees, departments, and branches.
Understanding CHECK Constraints
- Purpose
- The
CHECKconstraint ensures that all values in a column satisfy a specified condition. - Helps enforce rules at the database level to prevent invalid data entries.
- The
- Syntax
- The
CHECKconstraint is defined during table creation or alteration. - The syntax for a CHECK constraint is CHECK (condition).
- The
Code Samples
- Creating the
companyTable with a CHECK ConstraintCREATE TABLE company ( company_id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL, established YEAR NOT NULL, revenue DECIMAL(15, 2) CHECK (revenue >= 0) );company_id: Unique identifier for each company (auto-incremented).name: Name of the company (cannot be NULL).established: Year when the company was established (cannot be NULL).revenue: Annual revenue (must be non-negative due to the CHECK constraint).
- Creating the
employeesTable with CHECK ConstraintsCREATE TABLE employees ( employee_id INT AUTO_INCREMENT PRIMARY KEY, first_name VARCHAR(50) NOT NULL, last_name VARCHAR(50) NOT NULL, salary DECIMAL(10, 2) CHECK (salary >= 0), hire_date DATE NOT NULL, department_id INT NULL, age INT CHECK (age >= 18) );employee_id: Unique identifier for each employee (auto-incremented).first_nameandlast_name: Required fields (cannot be NULL).salary: Salary of the employee (must be non-negative due to the CHECK constraint).hire_date: Date the employee was hired (cannot be NULL).department_id: Reference to the department (can be NULL).age: Age of the employee (must be at least 18 due to the CHECK constraint).
- Creating the
departmentsTable with a CHECK ConstraintCREATE TABLE departments ( department_id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL, budget DECIMAL(12, 2) CHECK (budget >= 0), num_employees INT CHECK (num_employees >= 0) );department_id: Unique identifier for each department (auto-incremented).name: Name of the department (cannot be NULL).budget: Budget allocated to the department (must be non-negative due to the CHECK constraint).num_employees: Number of employees in the department (must be non-negative).
- Creating the
branchesTable with a CHECK ConstraintCREATE TABLE branches ( branch_id INT AUTO_INCREMENT PRIMARY KEY, address VARCHAR(255) NOT NULL, city VARCHAR(100) NOT NULL, state VARCHAR(100) NOT NULL, postal_code VARCHAR(20), opening_hours VARCHAR(50) CHECK (LENGTH(opening_hours) <= 50) );branch_id: Unique identifier for each branch (auto-incremented).address,city, andstate: Required fields (cannot be NULL).postal_code: Optional field (can beNULL).opening_hours: Description of the branch's opening hours (length must not exceed 50 characters due to the CHECK constraint).
Important Considerations
- Enforcement:
CHECKconstraints are enforced whenever data is inserted or updated in the table. - Support:
CHECKconstraints are supported in MySQL 8.0.16 and later versions. For earlier versions, consider alternative methods for validation. - Performance: Be mindful of the potential performance impact when using complex conditions.
By using CHECK constraints as demonstrated in these examples, beginners can ensure that their data adheres to specific rules and maintains consistency throughout their MySQL database.
To gain complete access, login with gmail or outlook, no need of signup, click here


Comments Not Found