MySQL

Chapter 5 - DDL (Data Definition Language)

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

  1. Purpose
    • The CHECK constraint ensures that all values in a column satisfy a specified condition.
    • Helps enforce rules at the database level to prevent invalid data entries.
  2. Syntax
    • The CHECK constraint is defined during table creation or alteration.
    • The syntax for a CHECK constraint is CHECK (condition).

Code Samples

  1. Creating the company Table with a CHECK Constraint
    CREATE 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).
  2. Creating the employees Table with CHECK Constraints
    CREATE 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_name and last_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).
  3. Creating the departments Table with a CHECK Constraint
    CREATE 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).
  4. Creating the branches Table with a CHECK Constraint
    CREATE 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, and state: Required fields (cannot be NULL).
    • postal_code: Optional field (can be NULL).
    • opening_hours: Description of the branch's opening hours (length must not exceed 50 characters due to the CHECK constraint).

Important Considerations

  • Enforcement:CHECK constraints are enforced whenever data is inserted or updated in the table.
  • Support:CHECK constraints 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.

Tansy SQL Course | CHECK Constraint | Chapter 5 | Lesson 7 - Video Thumbnail
Comments(0 comments)

Comments Not Found