Thursday 12 July 2018

MySQL CHECK Constraint Emulation

MySQL CHECK Constraint Emulation?

Summary: in this tutorial, you will learn how to use triggers or views with check option to emulate MySQL CHECK constraint.
To follow this tutorial, you need to have a good understanding of triggers, views, and stored procedures.

Introduction to the SQL CHECK constraint

Standard SQL provides CHECK constraints that specify a value in a certain column must satisfy a Boolean expression. For example, you can add a CHECK constraint to enforce the cost of a part to be positive as follows:
SQL allows you to apply multiple CHECK constraints to a column or a CHECK constraint across multiple columns. For example, to make sure that the price is always greater or equal cost, you use the CHECK constraint as follows:
Once the CHECK constraints are in place, whenever you insert or update a value that causes the Boolean expression evaluates to false, the check constraint is violated and the database system rejects the change.
Unfortunately, MySQL does not support CHECK constraint. Actually, MySQL accepts the CHECK clause in the CREATE TABLE statement but it ignores it silently.

MySQL CHECK constraint using triggers

The first way to  simulate the CHECK constraint in MySQL, we use two triggers: BEFORE INSERT and BEFORE UPDATE.
First, create the parts tables for the demonstration.
Second, create a stored procedure to check the values in the cost and price columns.
Third, create BEFORE INSERT and BEFORE UPDATE triggers. Inside the triggers, call the check_parts()stored procedure.
Fourth, insert a new row that satisfies all the following conditions:
  • cost > 0
  • And price > 0
  • And price  >= cost
The INSERT statement invokes the BEFORE INSERT trigger and accepts the values.
The following INSERT statement fails because it violates the condition: cost > 0.
The following INSERT statement fails because it violates the condition: price > 0.
The following INSERT statement fails because it violates the condition: price > cost.
Let’s see what we are having now in the parts table.
MySQL CHECK Constraint Emulation Example
We try to update the cost to make it lower than the price:
It was rejected.
So by using two triggers: BEFORE INSERT and BEFORE UPDATE, we are able to emulate the CHECK constraint in MySQL.

MySQL CHECK constraint using an updatable view with check option

The idea is to create a view with check option against the base table. In the SELECT statement of the view, we select only valid rows that satisfy the CHECK conditions. Any insert or update against the view will be rejected if it would cause the new row to not appear in the view.
First, drop the parts table to remove all the associated triggers and create a new table like the partstable but have a different name parts_data:
Second, create a view named parts based on the parts_data table. By doing this, we can keep the code of the applications that use the parts table remain intact. In addition, all the privileges to the old parts table remain unchanged.
Third, insert a new row into the parts_data table through the parts view:
It is accepted because the new row is valid which can appear in the view.
However, the following statement fails because the new row would not appear in the view.
In this tutorial, we have introduced you to the standard SQL CHECK constraint and two ways to emulate the CHECK constraint in MySQL.

0 comments:

Post a Comment