CHECK Constraints
Lesson 22 of 33 in the SQL / MySQL learning path.
How do I reject a seat count outside 1 to 40 in the table itself?
A form can reject 41 seats and a second client can still insert them. A CHECK constraint lives on the table, so every INSERT and UPDATE has to satisfy it. MySQL 8.0.16 and newer enforces the expression. This lesson adds lessons.seats and a named constraint, chk_lessons_seats, that requires the value to be from 1 through 40.
BETWEEN is inclusive, so 1 and 40 are valid and 0 and 41 are not. The column is also NOT NULL, because a CHECK treats NULL as unknown and would accept it. The script stores three legal lessons, shows the constraint in information_schema, and ends with an INSERT of 41 seats that MySQL rejects.
Syntax & Parameters
CONSTRAINT chk_lessons_seats CHECK (seats BETWEEN 1 AND 40)
Example
CREATE DATABASE IF NOT EXISTS school
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
USE school;
SET NAMES utf8mb4;
DROP VIEW IF EXISTS roster;
DROP TABLE IF EXISTS enrollments;
DROP TABLE IF EXISTS lessons;
DROP TABLE IF EXISTS users;
CREATE TABLE users (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(120) NOT NULL,
email VARCHAR(254) NOT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
UNIQUE KEY uq_users_email (email)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE lessons (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
title VARCHAR(160) NOT NULL,
track VARCHAR(80) NOT NULL,
published_at DATETIME NULL,
seats TINYINT UNSIGNED NOT NULL,
PRIMARY KEY (id),
CONSTRAINT chk_lessons_seats CHECK (seats BETWEEN 1 AND 40)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE enrollments (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
lesson_id INT UNSIGNED NOT NULL,
user_id INT UNSIGNED NOT NULL,
status VARCHAR(20) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY uq_enrollments_lesson_user (lesson_id, user_id),
CONSTRAINT fk_enrollments_lesson FOREIGN KEY (lesson_id) REFERENCES lessons (id),
CONSTRAINT fk_enrollments_user FOREIGN KEY (user_id) REFERENCES users (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
INSERT INTO users (name, email, created_at) VALUES
('Ada Lovelace', 'ada@school.test', '2026-03-01 09:00:00');
INSERT INTO lessons (title, track, published_at, seats) VALUES
('Algebra I', 'math', '2026-03-02 09:00:00', 24),
('SQL Basics', 'data', '2026-03-10 09:00:00', 1),
('Studio Draft', 'art', NULL, 40);
INSERT INTO enrollments (lesson_id, user_id, status) VALUES
(1, 1, 'confirmed');
SELECT id, title, track, seats
FROM lessons
ORDER BY id;
SELECT tc.CONSTRAINT_NAME, cc.CHECK_CLAUSE
FROM information_schema.TABLE_CONSTRAINTS AS tc
INNER JOIN information_schema.CHECK_CONSTRAINTS AS cc
ON cc.CONSTRAINT_SCHEMA = tc.CONSTRAINT_SCHEMA
AND cc.CONSTRAINT_NAME = tc.CONSTRAINT_NAME
WHERE tc.CONSTRAINT_SCHEMA = 'school'
AND tc.TABLE_NAME = 'lessons'
AND tc.CONSTRAINT_TYPE = 'CHECK';
INSERT INTO lessons (title, track, published_at, seats) VALUES
('Overflow Lab', 'math', NULL, 41);
Expected Output
id | title | track | seats 1 | Algebra I | math | 24 2 | SQL Basics | data | 1 3 | Studio Draft | art | 40 3 rows in set CONSTRAINT_NAME | CHECK_CLAUSE chk_lessons_seats | (`seats` between 1 and 40) 1 row in set ERROR 3819 (HY000): Check constraint 'chk_lessons_seats' is violated. Overflow Lab is not inserted.
Line-by-Line Walkthrough
- lessons.seats is a TINYINT UNSIGNED, which could hold 0 through 255. The type alone does not express the classroom rule.
- CONSTRAINT chk_lessons_seats CHECK (seats BETWEEN 1 AND 40) names the rule and stores it with the table. MySQL enforces it on 8.0.16 and newer.
- BETWEEN is inclusive. The inserts of 24, 1, and 40 all succeed. 1 is the smallest legal class and 40 is the largest.
- seats is NOT NULL. A nullable column would let NULL through, because a CHECK expression that is unknown for NULL counts as a pass.
- The enrollment insert shows the rest of the school schema. It does not decide whether seats is legal. The lessons row has already passed the CHECK.
- The information_schema query prints chk_lessons_seats and the stored clause so you can see the rule without reading the CREATE TABLE again.
- The last INSERT asks for 41 seats. MySQL rejects it with error 3819 and does not store Overflow Lab.
- A seat count of 0 fails the same constraint. A negative number fails earlier, because TINYINT UNSIGNED cannot store it under strict mode.
- Application validation should still explain the rule in the form. The CHECK is the backstop for every client, import, and hand-written statement.
Common Pitfalls & Security
A CHECK is enforced only by a server that implements it. MySQL versions before 8.0.16 accepted the syntax and then ignored it, so a dump from an old server can look protected when it is not. The constraint also does not, by itself, tell the form which message to show.
- NULL passes a CHECK when the expression is unknown. Pair the rule with NOT NULL when a missing seat count is not a real class size.
- BETWEEN 1 AND 40 allows both ends. Writing seats > 1 AND seats < 40 would reject a legal class of 1 or 40.
- TINYINT UNSIGNED already rejects negatives and values above 255. Do not expect the CHECK error for those. 41 is inside the type and outside the rule, which is why it returns error 3819.
- The constraint runs on UPDATE too. Shrinking a stored class to 0 fails even if the original INSERT was legal.
- A CHECK does not grant or hide rows. It only rejects writes that break the expression. Reads of existing legal rows stay open.
Best Practices & Modern Standards
Name the constraint, and keep the expression simple enough to read in an error log. Use CHECK for rules that are properties of one row, such as a seat count from 1 to 40. Use a foreign key when the rule is "this lesson_id must exist," and use a unique key when the rule is "this student is in this lesson once."
Validate in the form so the instructor sees a clear message, and keep the same bounds in the table so an import cannot bypass the form. After you add a CHECK to a table that already has rows, update or remove illegal rows first, or the ALTER stops.
Real-World Use
A department caps every lesson at 40 seats and never opens a class with zero seats. The lessons table stores that rule as chk_lessons_seats. The course form checks the number too, and a bulk import that sends 41 still fails inside MySQL with the constraint name in the error.
Frequently Asked Questions
Are 1 and 40 accepted?
Yes. BETWEEN 1 AND 40 includes both ends. This script stores SQL Basics with 1 seat and Studio Draft with 40.
Why does 41 fail while the column is a TINYINT?
TINYINT UNSIGNED can store 41. The CHECK is what rejects it. MySQL returns error 3819 and names chk_lessons_seats.
Would a NULL seat count pass the CHECK?
A CHECK allows NULL because the comparison is unknown. This column is also NOT NULL, so a missing seat count fails that rule instead of being stored.
Which MySQL versions actually enforce CHECK?
MySQL 8.0.16 and newer enforce CHECK constraints. Older MySQL accepted the clause and did not enforce it. This lesson targets MySQL 8.