Strict mode can still throw warnings

MySQL by default is vary lax with data validation. Silent conversions is a concept that is not a common practice in other databases. In MySQL, instead of throwing an error, a warning was thrown and many applications simply did not handle warnings. With the introduction of sql_mode=STRICT_ALL_TABLES (or TRADITIONAL), in MySQL 5, a better level of validation now exists.

My understanding was that Warnings are now thrown as Errors, therefore eliminating the need to do a SHOW WARNINGS to confirm any problems after every query (this is a performance overhead on a high volume system due to the round trip latency).

However I found an instance where MySQL in STRICT Mode still throws warnings, leading to the question, are there any other areas, and does the earlier statement “Warnings are now thrown as Errors” hold true.

Here is my seeding process to showing the problem.

mysql> create table i(i tinyint, unique key( i));
Query OK, 0 rows affected (0.01 sec)
mysql> insert into i values(999);
Query OK, 1 rows affected (0.00 sec)

Using default settings, attempting to INSERT a duplicate row throws an error, using INSERT IGNORE does not.

mysql> insert into i values(999);
ERROR 1062 (23000): Duplicate entry '127' for key 'i'
mysql> insert ignore into i values(999);
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> show warnings;
+---------+------+--------------------------------------------+
| Level   | Code | Message                                    |
+---------+------+--------------------------------------------+
| Warning | 1264 | Out of range value for column 'i' at row 1 |
+---------+------+--------------------------------------------+
1 row in set (0.00 sec)

When using a Strict Mode, a recommendation for all new systems, it is generally accepted that warnings are translated into errors, which implies your could should never have to consider checking for warnings.

mysql> truncate table i;
mysql> set sql_mode=strict_all_tables;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into i values(999);
ERROR 1264 (22003): Out of range value for column 'i' at row 1
mysql> insert ignore into i values(999);
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> show warnings;
+---------+------+--------------------------------------------+
| Level   | Code | Message                                    |
+---------+------+--------------------------------------------+
| Warning | 1264 | Out of range value for column 'i' at row 1 |
+---------+------+--------------------------------------------+
1 row in set (0.00 sec)
mysql> set sql_mode=traditional;
Query OK, 0 rows affected (0.00 sec)

mysql> insert ignore into i values(9990);
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> show warnings;
+---------+------+--------------------------------------------+
| Level   | Code | Message                                    |
+---------+------+--------------------------------------------+
| Warning | 1264 | Out of range value for column 'i' at row 1 |
+---------+------+--------------------------------------------+
1 row in set (0.00 sec)

I should caveat this post also with using caution with INSERT IGNORE. This should only be used if you never care about errors which I would never consider as a best practice design approach.

4 Responses to “Strict mode can still throw warnings”

  1. Shlomi Noach says:

    Hi,

    Very interesting. However, INSERT IGNORE has good use: to solve in DB level issues of duplication.
    What would otherwise be an application level check: “Is there a row with id XYZ? If not – let’s insert one” -
    and I’ve seen so many of these on MyISAM, or on InnoDB outside transaction scope -
    is easily solved by MySQL when simply instructed to add or ignore.
    I find it useful and in fact having better performance at times.

    Regards

  2. Maarten says:

    On the ‘insert syntax’ manual page: “If you use the IGNORE keyword, errors that occur while executing the INSERT statement are treated as warnings instead.”

    If you combine this rule with the rule of the strict mode, you’ll get a contradiction. It’s very well possible that the current behaviour is the intended behaviour. And I actually think this makes sense, because otherwise the whole insert ignore feature would have no purpose. ;)

  3. Ryan Lowe says:

    Isn’t that the whole point of INSERT IGNORE, though? The IGNORE keyword explicitly tells MySQL to treat any errors in the INSERT statement as warnings.

  4. Bob says:

    See this bug :

    http://bugs.mysql.com/bug.php?id=50757

    CREATE TABLE test1 ( x INT ); INSERT INTO test1 VALUES (0),(1);
    SELECT * FROM test1 WHERE x=’a';
    +——+
    | x |
    +——+
    | 0 |
    +——+
    1 row in set, 2 warnings (0.00 sec)