MySQL Data Type Default Values When Creating Tables
TheDEFAULTvalueclause in a data type specification indicates a default value for a column. With one exception, the default value must be a constant; it cannot be a function or an expression. This means, for example, that you cannot set the default for a date column to be the value of a function such asNOW()orCURRENT_DATE. The exception is that you can specifyCURRENT_TIMESTAMPas the default for aTIMESTAMPcolumn.
As of MySQL 5.0.2, if a column definition includes no explicitDEFAULTvalue, MySQL determines the default value as follows:
If the column can takeNULLas a value, the column is defined with an explicitDEFAULT NULLclause. This is the same as before 5.0.2.
If the column cannot takeNULLas the value, MySQL defines the column with no explicitDEFAULTclause. Exception: If the column is defined as part of aPRIMARY KEYbut not explicitly asNOT NULL, MySQL creates it as aNOT NULLcolumn (becausePRIMARY KEYcolumns must beNOT NULL), but also assigns it aDEFAULTclause using the implicit default value. To prevent this, include an explicitNOT NULLin the definition of anyPRIMARY KEYcolumn.
For data entry for aNOT NULLcolumn that has no explicitDEFAULTclause, if anINSERTorREPLACEstatement includes no value for the column, or anUPDATEstatement sets the column toNULL, MySQL handles the column according to the SQL mode in effect at the time:
If strict SQL mode is not enabled, MySQL sets the column to the implicit default value for the column data type.
If strict mode is enabled, an error occurs for transactional tables and the statement is rolled back. For nontransactional tables, an error occurs, but if this happens for the second or subsequent row of a multiple-row statement, the preceding rows will have been inserted.
Suppose that a tabletis defined as follows:
CREATE TABLE t (i INT NOT NULL);
In this case,ihas no explicit default, so in strict mode each of the following statements produce an error and no row is inserted. When not using strict mode, only the third statement produces an error; the implicit default is inserted for the first two statements, but the third fails becauseDEFAULT(i)cannot produce a value:
INSERT INTO t VALUES();
INSERT INTO t VALUES(DEFAULT);
INSERT INTO t VALUES(DEFAULT(i));