Lesson 4 of the SQL Optimization Course: Column - level CHECK Constraint


For relational databases, the design of tables and SQL is written are particularly crucial. It wouldn’t be an exaggeration to say that they account for 90% of performance. So this time, specifically targeting these two major knowledge areas, we’ll conduct a detailed analysis for you, peeling back the layers.
This Series uses plain and understandable language and selects a large number of examples to elaborate on the subtleties for you.
π§βπ» Target audience:
We will use MySQL as the demonstration database.
The CHECK constraint is used to filter one or more columns according to pre - set rules. If the condition is true, the filtering is successful; if false, the filtering fails, and a failure code is returned to the client.
Why is this discussed separately? It’s common to encounter non - standard SQL. In many cases, since the code - side doesn’t perform filtering, the filtering has to be done at the database level.
Suppose we have a table f1 with a column r1 whose value should be a multiple of 3; otherwise, the write operation is rejected. Also, assume that the input for r1 is not standardized and can only be filtered by the database. What can we do? There are mainly the following methods:
Example 1
| |
| |
Normal Insertion
| |
Handle input constraints within the stored procedure. This is similar to handling input constraint logic on the application side, but the same processing logic is placed at the database side. And in the future, all data entry can only rely on the single - entry point of the stored procedure.
This will lead to a large amount of entered data, including many useless non - standard data. Usually, non - standard data is processed regularly during non - peak business hours. Examples for these two methods are not provided as they are similar to the first one.
Now, let’s talk about the CHECK constraint that filters at the column level and is defined before the table definition (MySQL version >= 8.0.16).
| |
The relevant restrictions of the CHECK constraint are as follows:
constraint name must be unique in each database. That is, there are no two identical constraints in a single database. If not defined, the system will automatically generate a unique constraint name.insert, update, replace, load data, load xml; it fails for the corresponding ignore statements.NOW(), CONNECTION_ID(), CURRENT_USER().ON UPDATE, ON DELETE) are not applicable.enforced is enabled. If not enforced is added separately, the CHECK constraint fails.Example 2
Based on the above, let’s look at the actual examples of the two tables. The CHECK constraint only takes effect for table f1.
| |
Next, let’s look at a more detailed example of the CHECK constraint.
Example 3
| |
Regarding the above example:
tb_f1_r1_nonzero, tb_f1_r1r2_chk1, tb_f1_r1r3_chk are not tied to a specific column and are globally valid. In other words, they are table - based CHECK constraints.tb_f1_r1_chk1 includes constraint tb_f1_r1_nonezero, so tb_f1_r1_nonezero can never detect an exception. After checking, this constraint can be removed.The definition after removing the redundant constraint:
| |
| |
Next, we can transform the trigger we started with. By adding the relevant conditions, we can achieve the same column - level CHECK constraint.
| |
Test the Effect
| |
This article introduced the usage and some examples related to the database CHECK constraint. Personally, I suggest that if the CHECK constraint can be separated from the database side and implemented on the application side, it would be better. The simpler the database side, the better the performance. However, there are exceptions. If it is difficult to implement on the application side due to historical reasons or other factors, it has to be implemented on the database side.
π See you in the next lession.
SQLFlash is your AI-powered SQL Optimization Partner.
Based on AI models, we accurately identify SQL performance bottlenecks and optimize query performance, freeing you from the cumbersome SQL tuning process so you can fully focus on developing and implementing business logic.
Join us and experience the power of SQLFlash today!.