Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Inserts record with NULL value in NOT NULL column

Tags:

mysql

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)
like image 329
user1032531 Avatar asked Apr 09 '13 14:04

user1032531


People also ask

How do I insert a NULL value in a NOT NULL column?

Code Inspection: Insert NULL into NOT NULL column You cannot insert NULL values in col1 and col2 because they are defined as NOT NULL. If you run the script as is, you will receive an error. To fix this code, replace NULL in the VALUES part with some values (for example, 42 and 'bird' ).

How can you insert NULL values in a column while inserting the data?

You also can specify the NULL keyword in the VALUES clause to indicate that a column should be assigned a NULL value. The following example inserts values into three columns of the orders table: INSERT INTO orders (orders_num, order_date, customer_num) VALUES (0, NULL, 123);

Can we insert NULL in MySQL?

You can insert NULL value into an int column with a condition i.e. the column must not have NOT NULL constraints. The syntax is as follows. INSERT INTO yourTableName(yourColumnName) values(NULL); To understand the above syntax, let us first create a table.

What will happen if you insert NULL in an auto increment column that is a NOT NULL primary key column?

When we insert NULL value to AUTO_INCREMENT column, MySQL will return sequence number.


2 Answers

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

like image 113
Arun Killu Avatar answered Oct 18 '22 05:10

Arun Killu


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

like image 25
DavidScherer Avatar answered Oct 18 '22 04:10

DavidScherer