SQL Check Constraints not supported in MySQL

I previously wrote about using SQL check constraints in MySQL. It seems however that the MySQL relational database management system (RDBMS) does not actually support the fundamental verification used by check constraints in other RDBMSs. This means that although you can add check constraints to tables without issue, they are not validated and data is inserted or updated.

The latest MySQL documentation (5.5) discretly mentions the fact these constraints are unfortunately not supported.

  • The CONSTRAINT_TYPE value can be UNIQUE, PRIMARY KEY, or FOREIGN KEY.
  • The UNIQUE and PRIMARY KEY information is about the same as what you get from the Key_name field in the output from SHOW INDEX when the Non_unique field is 0.
  • The CONSTRAINT_TYPE column can contain one of these values: UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK. This is a CHAR (not ENUM) column. The CHECK value is not available until we support CHECK.

Take special note of the final line I’ve emboldened. This is quite a shame that this constraint type is not fully supported yet.

Apologies to readers for the implication that this was functional in my previous post, and thanks to the poster of this comment for noticing the discrepancy.

Check constraints, of course, function fine in many other DBMSs, and to that end, the original post on SQL check constraints is still relevant, but sadly not if you are worked with MySQL.

If you liked this post, you may be interested in these:

  1. SQL Check Constraints on MySQL Databases
  1. carlos says:

    Ya, I’m so upset today.
    I just tried to make check constraint in mysql db… its not working… :( poor mysql

  2. There are workarounds. For example you can simulate CHECK with view. Check this out:

    http://www.cphpvb.net/db/5671-%D1%81%D0%B8%D0%BC%D1%83%D0%BB%D0%B8%D1%80%D0%B0%D0%BD%D0%B5-%D0%BD%D0%B0-check-%D1%81-view/

    Sorry, it’s in Bulgarian language, but the code is code in any language, right? :)

    • Dave Robertson says:

      Good one, Philip. I can’t believe how many ‘Tutorials’ for MySQL publish all these examples of how to use check constraints when they don’t really work! I was going crazy looking for the reason, because the same code worked fine on IBM’s DB2 for the [AS/400, System i, iSeries, IBM i - pick a name] which is what I normally work on.

    • Morg. says:

      Well if you like workarounds, I’d suggest a set of before triggers as these are more likely to emulate a check efficiently ;)

  3. Nicola says:

    It’s incredible that they don’t mention it even at w3schools:

    http://www.w3schools.com/Sql/sql_check.asp

    they just list mysql as one of the options there…

  4. Jed says:

    Hmm, but you don’t need check constraints in MySQL, you have the enum data type, just make sure you have sql_mode = ‘STRICT_ALL_TABLES’ when you create your table and it works just fine.

    mysql> set sql_mode=’STRICT_ALL_TABLES’;
    Query OK, 0 rows affected (0.00 sec)

    mysql> create table test (col1 enum (‘good’,'bad’));
    Query OK, 0 rows affected (0.03 sec)

    mysql> insert into test values (‘good’);
    Query OK, 1 row affected (0.00 sec)

    mysql> insert into test values (‘bad’);
    Query OK, 1 row affected (0.00 sec)

    mysql> insert into test values (‘not allowed’);
    ERROR 1265 (01000): Data truncated for column ‘col1′ at row 1
    mysql> select * from test;
    +——+
    | col1 |
    +——+
    | good |
    | bad |
    +——+
    2 rows in set (0.00 sec)

Howdy! My name is Jordan Hall and you’ve reached my website for some reason. Hope you are enjoying it. If you want, I have a few more details about this website. If you are looking for a more professional overview and less general prose, take a look at my biography or my skill and experiences section.

Latest Project

A portfolio of sorts will appear here shortly listing some of the projects I've developed or those I'm currently working on.

For now, take a look at my projects page directly.

Latest Photographs

Some photograph previews will appear here at some point.

Please feel free to take a look at my photographs page.

Latest Comments