HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlModerate

check constraint does not work?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
workdoesconstraintchecknot

Problem

I have the following table.

create table test (
   id smallint unsigned AUTO_INCREMENT,
   age tinyint not null,
   primary key(id),
   check (age<20)
);


The problem is that the CHECK constraint does not work on the age column. For example, when I insert 222 for the age field MySQL accepts it.

Solution

CHECK constraints are not implemented in MySQL. From CREATE TABLE


The CHECK clause is parsed but ignored by all storage engines. See Section 12.1.17, “CREATE TABLE Syntax”. The reason for accepting but ignoring syntax clauses is for compatibility, to make it easier to port code from other SQL servers, and to run applications that create tables with references. See Section 1.8.5, “MySQL Differences from Standard SQL”.

It's also been a reported bug for almost 8 years...

Context

StackExchange Database Administrators Q#9662, answer score: 19

Revisions (0)

No revisions yet.