Databases 6 min read

How MySQL Finally Supports CHECK Constraints and Why It Matters for Data Integrity

This article explains why MySQL lacked CHECK constraints for years, demonstrates the data quality issues caused by missing checks, shows how to work around them with unsigned types, and provides step‑by‑step examples of using the new CHECK constraint in MySQL 8.0.16+ while highlighting its absence in earlier versions.

Aikesheng Open Source Community
Aikesheng Open Source Community
Aikesheng Open Source Community
How MySQL Finally Supports CHECK Constraints and Why It Matters for Data Integrity

Impact of Missing CHECK Constraints

MySQL historically supported only entity, domain, referential and unique constraints. Without a CHECK constraint, rows with invalid data (e.g., a negative age) can be stored, degrading data quality.

CREATE TABLE person (
  name VARCHAR(16),
  age INT
);

INSERT INTO person (name, age) VALUES ('张三岁', -32);
SELECT * FROM person;

The query returns a row with -32 as the age, illustrating the problem.

One workaround is to use an unsigned integer type, which prevents negative values but cannot enforce a minimum age such as > 18.

DROP TABLE IF EXISTS person;
CREATE TABLE person (
  name VARCHAR(16),
  age INT UNSIGNED
);

INSERT INTO person (name, age) VALUES ('张三岁', -32);
-- error 1264 (22003): Out of range value for column 'age'

Enforcing Age > 18 with CHECK (MySQL 8.0.16+)

Starting with MySQL 8.0.16 the engine evaluates CHECK constraints, allowing you to declare that age > 18 directly in the table definition.

SELECT @@version;  -- confirm you are on 8.0.16 or later

CREATE TABLE person (
  name VARCHAR(16),
  age INT,
  CONSTRAINT ck_person_001 CHECK (age > 18)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO person (name, age) VALUES ('张三岁', 17);
-- error 3819 (HY000): Check constraint 'ck_person_001' is violated

INSERT INTO person (name, age) VALUES ('张三岁', 19);
-- succeeds

Behavior in MySQL 8.0.15 and Earlier

In MySQL 8.0.15 and earlier the same CHECK clause is silently ignored. The constraint does not appear in SHOW CREATE TABLE, and invalid data can be inserted without error.

CREATE TABLE person (
  name VARCHAR(16),
  age INT,
  CONSTRAINT ck_person_001 CHECK (age > 18)
);

SHOW CREATE TABLE person;  -- the CHECK clause is omitted

INSERT INTO person (name, age) VALUES ('张三岁', 17);
-- row inserted successfully

Practical Steps

Verify the MySQL version with SELECT @@version; (must be 8.0.16 or later to enforce CHECK).

Define CHECK constraints in the CREATE TABLE statement when supported.

Confirm the constraint is stored using SHOW CREATE TABLE person;.

Test boundary values (e.g., age 17 and 19) to ensure the constraint behaves as expected.

For full syntax and additional considerations, refer to the official MySQL documentation: https://dev.mysql.com/doc/refman/8.0/en/create-table-check-constraints.html
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

SQLmysqldata integrityCHECK constraintDomain integrityVersion 8.0.16
Aikesheng Open Source Community
Written by

Aikesheng Open Source Community

The Aikesheng Open Source Community provides stable, enterprise‑grade MySQL open‑source tools and services, releases a premium open‑source component each year (1024), and continuously operates and maintains them.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.