Skip to content

Instantly share code, notes, and snippets.

@nrocy
Created January 25, 2011 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nrocy/795129 to your computer and use it in GitHub Desktop.
Save nrocy/795129 to your computer and use it in GitHub Desktop.
is this 'normal' mysql behaviour? how didn't i notice this before?
mysql> create table wtf ( id int not null auto_increment primary key, expletive varchar(64) not null, incredulity_level int not null, nullable int null );
Query OK, 0 rows affected (0.01 sec)
mysql> show create table wtf;
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| wtf | CREATE TABLE `wtf` (
`id` int(11) NOT NULL auto_increment,
`expletive` varchar(64) NOT NULL,
`incredulity_level` int(11) NOT NULL,
`nullable` int(11) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1 |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> insert into wtf ( nullable ) values ( '42' );
Query OK, 1 row affected, 2 warnings (0.00 sec)
mysql> select * from wtf;
+----+-----------+-------------------+----------+
| id | expletive | incredulity_level | nullable |
+----+-----------+-------------------+----------+
| 1 | | 0 | 42 |
+----+-----------+-------------------+----------+
1 row in set (0.00 sec)
Really?
@nrocy
Copy link
Author

nrocy commented Jan 25, 2011

Update, this is because MySQL isn't running in strict mode:

Strict mode controls how MySQL handles input values that are invalid or missing. A value can be invalid for several reasons. For example, it might have the wrong data type for the column, or it might be out of range. A value is missing when a new row to be inserted does not contain a value for a non-NULL column that has no explicit DEFAULT clause in its definition. (For a NULL column, NULL is inserted if the value is missing.)

See: http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html#sqlmode_strict_all_tables

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