Skip to content

Instantly share code, notes, and snippets.

@rgarner
Last active August 29, 2015 13:57
Show Gist options
  • Save rgarner/9740859 to your computer and use it in GitHub Desktop.
Save rgarner/9740859 to your computer and use it in GitHub Desktop.
MySQL go home you're drunk #12476424
mysql> INSERT INTO hosts (site_id, hostname, created_at, updated_at) VALUES (NULL, 'test.me', '2014-01-01', '2014-01-01');
ERROR 1048 (23000): Column 'site_id' cannot be null
mysql> INSERT INTO hosts (site_id, hostname, created_at, updated_at) VALUES (1, 'test.me', '2014-01-01', '2014-01-01');
Query OK, 1 row affected (0.00 sec)
mysql> UPDATE `hosts` SET `site_id` = NULL WHERE `hosts`.`site_id` = 1 AND `hosts`.`id` IN (1)
-> ;
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> select * from hosts;
+----+---------+----------+------+-------+------------+---------------------+---------------------+------------+---------+
| id | site_id | hostname | ttl | cname | live_cname | created_at | updated_at | ip_address | has_aka |
+----+---------+----------+------+-------+------------+---------------------+---------------------+------------+---------+
| 1 | 0 | test.me | NULL | NULL | NULL | 2014-01-01 00:00:00 | 2014-01-01 00:00:00 | NULL | NULL |
+----+---------+----------+------+-------+------------+---------------------+---------------------+------------+---------+
1 row in set (0.00 sec)
mysql>
mysql> INSERT INTO hosts (site_id, hostname, created_at, updated_at) VALUES (NULL, 'test.me', '2014-01-01', '2014-01-01');
ERROR 1048 (23000): Column 'site_id' cannot be null
mysql> INSERT INTO hosts (site_id, hostname, created_at, updated_at) VALUES (1, 'test.me', '2014-01-01', '2014-01-01');
Query OK, 1 row affected (0.00 sec)
mysql> select * from hosts;
+----+---------+----------+------+-------+------------+---------------------+---------------------+------------+---------+
| id | site_id | hostname | ttl | cname | live_cname | created_at | updated_at | ip_address | has_aka |
+----+---------+----------+------+-------+------------+---------------------+---------------------+------------+---------+
| 1 | 1 | test.me | NULL | NULL | NULL | 2014-01-01 00:00:00 | 2014-01-01 00:00:00 | NULL | NULL |
+----+---------+----------+------+-------+------------+---------------------+---------------------+------------+---------+
1 row in set (0.00 sec)
mysql> UPDATE `hosts` SET `site_id` = NULL WHERE `hosts`.`site_id` = 1 AND `hosts`.`id` IN (1);
ERROR 1048 (23000): Column 'site_id' cannot be null
mysql> describe hosts;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| site_id | int(11) | NO | MUL | NULL | |
| hostname | varchar(255) | NO | UNI | NULL | |
| ttl | int(11) | YES | | NULL | |
| cname | varchar(255) | YES | | NULL | |
| live_cname | varchar(255) | YES | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
| ip_address | varchar(255) | YES | | NULL | |
| has_aka | tinyint(1) | YES | | NULL | |
+------------+--------------+------+-----+---------+----------------+
@rgarner
Copy link
Author

rgarner commented Mar 24, 2014

mySQL 5.5 (top) will allow an UPDATE to NULL in a NOT NULL field. In fact, it will set it to 0.
mySQL 5.6 (bottom) doesn't allow this bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment