CHECK constraint in MySQL is not working
Asked Answered
D

8

142

First I created a table like

CREATE TABLE Customer (
  SD integer CHECK (SD > 0),
  Last_Name varchar (30),
  First_Name varchar(30)
);

and then inserted values in that table

INSERT INTO Customer values ('-2','abc','zz');

MySQL doesn't show an error, it accepted the values.

Docent answered 22/1, 2010 at 6:33 Comment(5)
Partially agree. Given that you tried to use it, it can be assumed that you were asking both questions. In fact, the answer you have accepted is mainly explaining why it does not work.Wyne
You can vote on this feature request: bugs.mysql.com/bug.php?id=3464 but it hasn't received any attention in a decade.Pessimism
You can use CHECK constraints in MariaDB from version 10.2.1.Maculate
Now that MySQL 8.0.16 has finally addressed this one it would be time to recognise silently (!) ignored inline REFERENCES specifications too (the other big SQL standard incompatibility): bugs.mysql.com/bug.php?id=102904.Autoroute
In MySQL 8.x CHECK CONSTRAINT support is presentNorby
U
160

MySQL 8.0.16 is the first version that supports CHECK constraints.

Read https://dev.mysql.com/doc/refman/8.0/en/create-table-check-constraints.html

If you use MySQL 8.0.15 or earlier, the MySQL Reference Manual says:

The CHECK clause is parsed but ignored by all storage engines.

Try a trigger...

mysql> delimiter //
mysql> CREATE TRIGGER trig_sd_check BEFORE INSERT ON Customer 
    -> FOR EACH ROW 
    -> BEGIN 
    -> IF NEW.SD<0 THEN 
    -> SET NEW.SD=0; 
    -> END IF; 
    -> END
    -> //
mysql> delimiter ;
Unify answered 22/1, 2010 at 7:11 Comment(3)
Here you will find how to trigger an error instead: https://mcmap.net/q/128077/-throw-an-error-preventing-a-table-update-in-a-mysql-triggerKinetics
This is among the vast sparkly rainbow of reasons that I will always use PostgreSQL instead of MySQL given a choice whatsoever.Dorseydorsiferous
I wonder if it would be 10 minute or 15 minute development in MySQL to throw a warning if the parser encounters a CHECK constraint defined. Ahhh, that would be too straightforward...Ptah
I
76

Unfortunately MySQL does not support SQL check constraints. You can define them in your DDL query for compatibility reasons but they are just ignored.

There is a simple alternative

You can create BEFORE INSERT and BEFORE UPDATE triggers which either cause an error or set the field to its default value when the requirements of the data are not met.

Example for BEFORE INSERT working after MySQL 5.5

DELIMITER $$
CREATE TRIGGER `test_before_insert` BEFORE INSERT ON `Test`
FOR EACH ROW
BEGIN
    IF CHAR_LENGTH( NEW.ID ) < 4 THEN
        SIGNAL SQLSTATE '12345'
            SET MESSAGE_TEXT := 'check constraint on Test.ID failed';
    END IF;
END$$   
DELIMITER ;  

Prior to MySQL 5.5 you had to cause an error, e.g. call a undefined procedure.

In both cases this causes an implicit transaction rollback. MySQL does not allow the ROLLBACK statement itself within procedures and triggers.

If you don't want to rollback the transaction ( INSERT / UPDATE should pass even with a failed "check constraint" you can overwrite the value using SET NEW.ID = NULL which will set the id to the fields default value, doesn't really make sense for an id tho

Edit: Removed the stray quote.

Concerning the := operator:

Unlike =, the := operator is never interpreted as a comparison operator. This means you can use := in any valid SQL statement (not just in SET statements) to assign a value to a variable.

https://dev.mysql.com/doc/refman/5.6/en/assignment-operators.html

Concerning backtick identifier quotes:

The identifier quote character is the backtick (“`”)

If the ANSI_QUOTES SQL mode is enabled, it is also permissible to quote identifiers within double quotation marks

http://dev.mysql.com/doc/refman/5.6/en/identifiers.html

Irredentist answered 9/1, 2013 at 23:0 Comment(5)
...not very simple, at least compared to CHECK :(. Coupla tutes: net.tutsplus.com/tutorials/databases/…, sitepoint.com/how-to-create-mysql-triggersAsiaasian
ugh this looks very bulky. I think I rather create a tuple in python and check values there instead of putting this in.Jolenejolenta
Quick question: why this doesn't work without setting the DELIMITER?Trimeter
But why? I mean, they could have just removed the command. What was the point of keeping the command and removing its functionality?Conduce
The command is defined in the ANSI Standard for SQL which Mysql is following. dev.mysql.com/doc/refman/8.0/en/compatibility.htmlIrredentist
F
53

CHECK constraints are ignored by MySQL as explained in a miniscule comment in the docs: CREATE TABLE

The CHECK clause is parsed but ignored by all storage engines.

Finkle answered 9/1, 2013 at 22:31 Comment(4)
@thefiloe: Correct, in other DBMS with correct implementation of CHECK constraints, if the CHECK evaluates to FALSE then the insert (or update) is not done and an error is caused.Swart
Fixed in MariaDB (see this answer stackoverflow.com/a/44333349).Mapping
@Jérôme I know, I have some (more recent) answers that include improvements in this area (there had been other ways to workaround this issue, both in MariaDB and MySQL, before MariaDB properly implemented CHECK constraints). What I'm not sure is if I should go and edit all my old answers!Swart
I suppose my comment with a link to a more recent answer is fine. Or better than nothing. Perhaps I should have edited. I didn't mean to put pressure on you to do anything.Mapping
V
16

The CHECK constraint doesn't seem to be implemented in MySQL.

See this bug report: https://bugs.mysql.com/bug.php?id=3464

Valdivia answered 22/1, 2010 at 6:37 Comment(1)
Fixed in MariaDB (see this answer stackoverflow.com/a/44333349).Mapping
A
9

As mentioned by joanq MariaDB now seems to support CHECK constraints among other goodies:

"Support for CHECK CONSTRAINT (MDEV-7563)."

https://mariadb.com/kb/en/mariadb/mariadb-1021-release-notes/

Ashtonashtonunderlyne answered 22/1, 2010 at 6:33 Comment(0)
L
2

Check constraints are supported as of version 8.0.15 (yet to be released)

https://bugs.mysql.com/bug.php?id=3464

[23 Jan 16:24] Paul Dubois

Posted by developer: Fixed in 8.0.15.

Previously, MySQL permitted a limited form of CHECK constraint syntax, but parsed and ignored it. MySQL now implements the core features of table and column CHECK constraints, for all storage engines. Constraints are defined using CREATE TABLE and ALTER TABLE statements.

Loupgarou answered 22/1, 2010 at 6:33 Comment(0)
C
2

Update to MySQL 8.0.16 to use checks:

As of MySQL 8.0.16, CREATE TABLE permits the core features of table and column CHECK constraints, for all storage engines. CREATE TABLE permits the following CHECK constraint syntax, for both table constraints and column constraints

MySQL Checks Documentation

Cleghorn answered 4/2, 2019 at 19:50 Comment(0)
J
-2

try with set sql_mode = 'STRICT_TRANS_TABLES' OR SET sql_mode='STRICT_ALL_TABLES'

Jabot answered 29/1, 2012 at 5:2 Comment(1)
that actuall does not help (MySQL 5.6) it prevents entering data of false type but not of entering data that does not meet the CHECK constraintKinetics

© 2022 - 2024 — McMap. All rights reserved.