11

Why does the first INSERT go through for table2. Note that table2.col_1 is NOT NULL. It doesn't insert NULL for col_1, but mysteriously converts the NULL value to an empty string. I am using MySQL Version 5.5.28. Thanks

mysql> DROP TABLE IF EXISTS table1, table2;

Query OK, 0 rows affected (0.01 sec)   

mysql> CREATE  TABLE IF NOT EXISTS table1 (
    -> id INT UNSIGNED NOT NULL AUTO_INCREMENT ,
    -> col_1 VARCHAR(45) NOT NULL ,
    -> col_2 VARCHAR(45) NOT NULL ,
    -> PRIMARY KEY (`id`))
    -> ENGINE = InnoDB;

Query OK, 0 rows affected (0.01 sec)

mysql> CREATE TABLE table2 LIKE table1;
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO table1 (id, col_1, col_2) VALUES (NULL, "xxx","yyy");
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO table2 (id, col_1, col_2) SELECT NULL, NULL, col_2 FROM table1 WHERE id=1;
Query OK, 1 row affected, 1 warning (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 1

mysql> SHOW WARNINGS;
+---------+------+-------------------------------+
| Level   | Code | Message                       |
+---------+------+-------------------------------+
| Warning | 1048 | Column 'col_1' cannot be null |
+---------+------+-------------------------------+
1 row in set (0.00 sec)

mysql> SELECT * FROM table2;
+----+-------+-------+
| id | col_1 | col_2 |
+----+-------+-------+
|  1 |       | yyy   |
+----+-------+-------+
1 row in set (0.00 sec)

mysql> INSERT INTO table2 (id, col_1, col_2) VALUES( NULL, NULL, "zzz");
ERROR 1048 (23000): Column 'col_1' cannot be null

mysql> SELECT * FROM table2;
+----+-------+-------+
| id | col_1 | col_2 |
+----+-------+-------+
|  1 |       | yyy   |
+----+-------+-------+
1 row in set (0.00 sec)
9
  • maybe it inserts NULL string and not the null (nothing) Commented Apr 9, 2013 at 15:00
  • Strange, I'm doing the same and I do get the nulls inserted... Maybe you could specify PDO::PARAM_INT when you bind the value? Commented Apr 9, 2013 at 15:05
  • @JW Please explain. I just edited the original post to show how it only inserts a row if the INSERT is fed by a SELECT. Commented Apr 9, 2013 at 15:06
  • @Sebas. Does it insert NULL, or an empty string? Commented Apr 9, 2013 at 15:06
  • null, but see my edition of my previous comment Commented Apr 9, 2013 at 15:07

3 Answers 3

12

You have MySQL's STRICT mode OFF. Turn it on and you'll get an error.

Otherwise you can test for those warnings with PDO via: http://php.net/manual/en/pdo.errorinfo.php

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks David, My bad, turns out to have nothing to do with PDO or PHP. I edited the original post to show this. Sorry for the confusion.
@user1032531 Updated this answer to reflect my further comprehension.
I bet you are right! I never knew of MySQL STRICT mode, and will test it out. Thanks
8

This behavior is well documented in MySQL docs . MySQL doc

If you are not using strict mode, then whenever you insert an “incorrect” value into a column, such as a NULL into a NOT NULL column or a too-large numeric value into a numeric column, MySQL sets the column to the “best possible value” instead of producing an error:,but the warning count is incremented

Comments

-1

I have tried setting MySQL's STRICT mode OFF and didn't work for me (I even changed it to "my.ini" ).

What worked for me was a BEFORE INSERT TRIGGER

Basically you do:

CREATE TRIGGER triggerName BEFORE INSERT ON customer
FOR EACH ROW
BEGIN
    if new.`customerName` = '' then
    signal sqlstate '45000'
    SET MESSAGE_TEXT = 'Customer Name Cannot be Empty!';
    end if;
END

In the MESSAGE_TEXT you can add whatever text you want the error to show.

Hope it helps!

Most of it found here

2 Comments

I am clearly saying in the start of my post "I have tried setting MySQL's STRICT mode OFF and didn't work". I am not saying this is the solution.
What did you mean by "didn't work"? The actual problem of the asker is because his STRICT mode is OFF. So why would you say setting it OFF didn't work when what you need to do is to actually turn it ON?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.