- Why is there a default keyword if i am allowing datetime to be null
You mistake the NULL
after DEFAULT
with the NULL
that allows a column to contain NULL
values. Read below about the difference.
- Is it about the Datetime syntax, so that it should be in default format.
The DEFAULT
keyword in a CREATE TABLE
statement doesn't tell anything about any format. It specifies the default value to be used for the column when an INSERT
statement doesn't provide a value for it.
The complete definition of a table column in the CREATE TABLE
statement contain the following pieces, in this order:
- field name;
- field type;
NULL
or NOT NULL
- are NULL
values allowed to be stored in the field? If neither is specified, NULL
is assumed;
DEFAULT
default value - the value to be used for the field when an INSERT
statement doesn't provide a value for it; NULL
can be used as default value if the column is nullable; if it's not specified, MySQL uses some rules to compute the default value based on the field type;
AUTO_INCREMENT
- when a value is not provided for the column, MySQL uses the biggest value existing in the column plus one; can be used only with integer and float columns;
- one of
UNIQUE
, UNIQUE KEY
, KEY
, PRIMARY KEY
- the first two are equivalent and they specify that the column is an unique index of the table (it cannot contain the same value for two or more rows); the last two specify the column is the identifier of the row (it is an UNIQUE INDEX
and it cannot contain NULL
values); these attributes can be specified here for compatibility with other database systems; on MySQL one usually specifies the table indexes after the columns using a different syntax;
COMMENT
string - a comment for the column; it is not used by MySQL but it can be useful for the developers (to specify what represents the column);
- other, less used, options.
Only the first two pieces from the list above (the name and the type) are required; all the others are optional. If present, they must be provided in the order of the list.
Your statement:
CREATE TABLE User
(
id INT(11) NOT NULL AUTO_INCREMENT,
created_at DATETIME DEFAULT NULL
)
The column id
specifies NOT NULL
, skips DEFAULT
and specifies AUTO_INCREMENT
. Without AUTO_INCREMENT
, MySQL uses 0
as the default value (this is the default value for integer types). Because of AUTO_INCREMENT
, when a value is not provided in the INSERT
statement, MySQL finds the largest value already stored in the column, increases it with 1
and uses this computed value for the column.
The column created_at
doesn't specify if it allows NULL
values (it allows them, this is the default) and specifies that the default value for the column is NULL
.
Examples
Let's see how it works:
INSERT INTO User(id, created_at) VALUES (5, '2016-06-01 11:22:33')
inserts a new row having the values provided in the INSERT
statement; no surprise here.
INSERT INTO User(created_at) VALUES ('2016-06-02 12:34:56')
inserts a new row with id = 6
, created_at = '2016-06-02 12:34:56'
. Because a value was not provided for column id
, the AUTO_INCREMENT
option generated 6
for it (the successor of the larger value already in the column).
The same happens when NULL
is provided for the AUTO_INCREMENT
column:
INSERT INTO User(id, created_at) VALUES (NULL, '2016-06-03')
inserts id = 7
, created_at = '2016-06-03 00:00:00'
. Please also note that, because the time components were not specified in the value provided for created_at
, MySQL defaulted them to 00:00:00
.
A new statement:
INSERT INTO User(id) VALUES (10)
creates a row having id = 10
, created_at = NULL
.
The statement
INSERT INTO User() VALUES()
looks strange but it's perfectly legal and inserts a row that uses the default values for all its columns. In this case, the new row have the values id = 11
, created_at = NULL
.
Finally, the statement
INSERT INTO User(id) VALUES(5)
fails because there already exists a row having id = 5
in the table (it was inserted by the first statement at the start of the examples.