CHECK constraint in MySQL is not working

MysqlCheck Constraints

Mysql Problem Overview


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.

Mysql Solutions


Solution 1 - Mysql

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 ;

Hope that helps.

Solution 2 - Mysql

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

Solution 3 - Mysql

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.

Solution 4 - Mysql

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

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

Solution 5 - Mysql

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/

Solution 6 - Mysql

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.

Solution 7 - Mysql

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

Solution 8 - Mysql

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohnRajaView Question on Stackoverflow
Solution 1 - MysqlDavid KerinsView Answer on Stackoverflow
Solution 2 - MysqlMichel FeldheimView Answer on Stackoverflow
Solution 3 - MysqlypercubeᵀᴹView Answer on Stackoverflow
Solution 4 - MysqlryanprayogoView Answer on Stackoverflow
Solution 5 - MysqlMarkus BarthlenView Answer on Stackoverflow
Solution 6 - MysqlJamesView Answer on Stackoverflow
Solution 7 - MysqlsdlinsView Answer on Stackoverflow
Solution 8 - MysqlKanaguView Answer on Stackoverflow