MySQL 8.0 New Features - Introduction to Check Constraints

MySQL 8.0 New Features - Introduction to Check Constraints

Preface

In MySQL 8.0, a very useful new feature is introduced - check constraints, which can improve the control over illegal or unreasonable data writing. Let's take a closer look at it.

Check Constraints

Create, Delete and View

(1) You can create check constraints when creating a table.

mysql> CREATE TABLE t1
 -> (
 -> CHECK (c1 <> c2),
 -> c1 INT CHECK (c1 > 10),
 -> c2 INT CONSTRAINT c2_positive CHECK (c2 > 0),
 -> c3 INT CHECK (c3 < 100),
 -> CONSTRAINT c1_nonzero CHECK (c1 <> 0),
 -> CHECK (c1 > c3)
 -> );
Query OK, 0 rows affected (0.03 sec)

(2) You can also add check constraints using the following statements:

mysql> ALTER TABLE t1 ADD CONSTRAINT c3_nonzero CHECK ((c3<>0));
Query OK, 0 rows affected (0.16 sec)
Records: 0 Duplicates: 0 Warnings: 0

(3) You can delete the check constraint by using the following statement

mysql> ALTER TABLE t1 DROP CONSTRAINT c3_nonzero;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0

(4) You can view the check constraints by querying the table structure

mysql> SHOW CREATE TABLE t1_G
*************************** 1. row ***************************
 Table: t1
Create Table: CREATE TABLE `t1` (
 `c1` int DEFAULT NULL,
 `c2` int DEFAULT NULL,
 `c3` int DEFAULT NULL,
 CONSTRAINT `c1_nonzero` CHECK ((`c1` <> 0)),
 CONSTRAINT `c2_positive` CHECK ((`c2` > 0)),
 CONSTRAINT `t1_chk_1` CHECK ((`c1` <> `c2`)),
 CONSTRAINT `t1_chk_2` CHECK ((`c1` > 10)),
 CONSTRAINT `t1_chk_3` CHECK ((`c3` < 100)),
 CONSTRAINT `t1_chk_4` CHECK ((`c1` > `c3`))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)

(5) You can also view it through the following two views: table_constraints queries which constraints exist in the table, and check_constraints queries the specific definition of the check constraint.

mysql> SELECT * FROM information_schema.table_constraints WHERE table_name='t1';
+--------------------+-------------------+-----------------+--------------+------------+-----------------+----------+
| CONSTRAINT_CATALOG | CONSTRAINT_SCHEMA | CONSTRAINT_NAME | TABLE_SCHEMA | TABLE_NAME | CONSTRAINT_TYPE | ENFORCED |
+--------------------+-------------------+-----------------+--------------+------------+-----------------+----------+
| def | test | c1_nonzero | test | t1 | CHECK | YES |
| def | test | c2_positive | test | t1 | CHECK | YES |
| def | test | t1_chk_1 | test | t1 | CHECK | YES |
| def | test | t1_chk_2 | test | t1 | CHECK | YES |
| def | test | t1_chk_3 | test | t1 | CHECK | YES |
| def | test | t1_chk_4 | test | t1 | CHECK | YES |
+--------------------+-------------------+-----------------+--------------+------------+-----------------+----------+
6 rows in set (0.00 sec)

mysql> SELECT * FROM information_schema.check_constraints WHERE constraint_name='c1_nonzero';
+--------------------+-------------------+-----------------+--------------+
| CONSTRAINT_CATALOG | CONSTRAINT_SCHEMA | CONSTRAINT_NAME | CHECK_CLAUSE |
+--------------------+-------------------+-----------------+--------------+
| def | test | c1_nonzero | (`c1` <> 0) |
+--------------------+-------------------+-----------------+--------------+
1 row in set (0.00 sec)

(6) When inserting data that does not meet the check constraint, an error will be reported directly

mysql> insert into t1 values(0,0,0);
ERROR 3819 (HY000): Check constraint 'c1_nonzero' is violated.

limit

(1) Auto-increment columns and columns of other tables do not support check constraints

(2) Uncertain functions, such as CONNECTION_ID(), CURRENT_USER(), NOW(), etc., do not support check constraints.

(3) User-defined functions do not support check constraints

(4) Stored procedures do not support check constraints

(5) Variables do not support check constraints

(6) Subqueries do not support check constraints

Summarize

Check constraints are a very good feature that can realize a variety of data verification scenarios. You can try it.

The above is a brief introduction to the new feature of MySQL 8.0 - check constraints. For more information about the new feature of MySQL 8.0 - check constraints, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • MySQL not null constraint case explanation
  • MySQL foreign key constraint (FOREIGN KEY) case explanation
  • Summary of MySQL foreign key constraints and table relationships
  • MySQL integrity constraints definition and example tutorial
  • Creation, constraints and deletion of foreign keys in MySQL
  • Example statements for indexes and constraints in MySQL
  • Example explanation of MySQL foreign key constraints
  • Detailed explanation of MySQL foreign key constraints
  • Detailed explanation of mysql integrity constraints example
  • MySQL Constraints Super Detailed Explanation

<<:  docker run -v mounts data volumes abnormally, and the container status is always restarting

>>:  The rel attribute of the HTML link tag

Recommend

How to use file writing to debug a Linux application

In Linux, everything is a file, so the Android sy...

A brief discussion of 3 new features worth noting in TypeScript 3.7

Table of contents Preface Optional Chaining Nulli...

Detailed explanation of adding dotted lines to Vue element tree controls

Table of contents 1. Achieve results 2. Implement...

Briefly describe the difference between Redis and MySQL

We know that MySQL is a persistent storage, store...

Implementation of vscode custom vue template

Use the vscode editor to create a vue template, s...

Native JS to implement click number game

Native JS implements the click number game for yo...

How to generate a free certificate using openssl

1: What is openssl? What is its function? What is...

Practical explanation of editing files, saving and exiting in linux

How to save and exit after editing a file in Linu...

Nginx monitoring issues under Linux

nginx installation Ensure that the virtual machin...

JavaScript to achieve the effect of tab bar switching

Tab bar: Click different tabs to display differen...

Mini Program Recording Function Implementation

Preface In the process of developing a mini progr...

Screen command and usage in Linux

Screen Introduction Screen is a free software dev...