I have a strange situation with a field that doesn't allow NULL values. If I insert a row, the field defaults to the empty string, even when mysql claims that the default is NULL.
mysql> describe user;
+---------------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------------+------------------+------+-----+---------+----------------+
| id | int(30) | NO | PRI | NULL | auto_increment |
| username | varchar(45) | NO | UNI | NULL | |
| city | varchar(45) | NO | | NULL | |
+---------------------------+------------------+------+-----+---------+----------------+
mysql> show triggers;
Empty set (0.00 sec)
mysql> insert into user (username) values ('just_testing');
Query OK, 1 row affected, 17 warnings (0.01 sec)
This is the point where I go WHAT?! - city should default to NULL which isn't allowed, but look here:
mysql> select username, city from user where username = 'just_testing' and city is null;
Empty set (0.00 sec)
mysql> select username, city from user where username = 'just_testing' and city='';
+--------------+------+
| username | city |
+--------------+------+
| just_testing | |
+--------------+------+
1 row in set (0.00 sec)
Mysql has decided to use the empty string as default even though it isn't so and there isn't any triggers.
And further:
mysql> insert into user (username, city) values ('just_testing3', NULL);
ERROR 1048 (23000): Column 'city' cannot be null
What am I overlooking? How does the city column default to ''?
just_testing2and reading backjust_testing, is it a typo?SHOW CREATE TABLE user?... not null default null ...is a contradiction and a flaw in the creation of the table from my point of view. The expected behavior is one out of two possible ones and mysql decided to choose the other one ...not null default nullin MySQL, it's just the wayDESCRIBEshows things.