alamin
alamin

Reputation: 2497

What is the Meaning of "DEFAULT" in Mysql Datetime?

In this following mysql command i am seeing the Keyword DEFAULT

CREATE Table User
(
    id int(11) NOT NULL AUTO_INCREMENT,
    created_at datetime DEFAULT NULL
)

My Question Is :

  1. Why is there a default keyword if i am allowing datetime to be null
  2. Is it about the Datetime syntax, so that it should be in default format.

please explain it to me. I also found the documentation page for DEFAULT in mysql. but i am not understanding it.

[N.B.: Pardon Me, If this a beginner question, or already asked. But I did not find exactly what i was looking for.]

Upvotes: 1

Views: 2058

Answers (5)

axiac
axiac

Reputation: 72306

  1. 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.

  1. 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.

Upvotes: 2

Andrew
Andrew

Reputation: 1866

1. Why is there a default keyword if i am allowing datetime to be null

If a column definition includes no explicit DEFAULT value, MySQL determines the default value as described in Section 12.7, “Data Type Default Values”.

MySQL will add default value to column definition by it's own rules if you don't set it manually. So, DEFAULT is always presented, except special datatypes

DEFAULT does not apply to the BLOB, TEXT, GEOMETRY, and JSON types

2. Is it about the Datetime syntax, so that it should be in default format.

No. If you allow null, then column can take any valid datetime value OR null. Null is valid too. And from manual:

If the column can take NULL as a value, the column is defined with an explicit DEFAULT NULL clause.

As result, if you create table with column created_at datetime it will be converted according these rules to created_at datetime DEFAULT NULL.

Update

Some examples. Next CREATE TABLE statements are equivalent:

CREATE Table User
(
    created_at datetime NULL
);

CREATE Table User
(
    created_at datetime DEFAULT NULL
);

You said, that you "allow null" for this column. Probably, you was using first query with created_at datetime NULL syntax. But MySQL really translates it into second query, because of rules above.

Upvotes: 3

Hamza Zafeer
Hamza Zafeer

Reputation: 2436

The DEFAULT constraint is used to insert a default value into a column.

The default value will be added to all new records, if no other value is specified.

The DEFAULT value clause 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.

For More Information See This Question. MySQL columns with default null - stylistic choice, or is it?

Upvotes: 2

S.I.
S.I.

Reputation: 3375

From MySQL Data Type Default Values

If the column can take NULL as a value, the column is defined with an explicit DEFAULT NULL clause.

For MySQL those definitions are all same and identical

column type
column type NULL
column type DEFAULT NULL
column type NULL DEFAULT NULL

Upvotes: 0

Tudor Constantin
Tudor Constantin

Reputation: 26861

Think of NULL as unknown. This has interesting implications, like:

mysql> select null = true;
+-------------+
| null = true |
+-------------+
|        NULL |
+-------------+

and

mysql> select null != true;
+--------------+
| null != true |
+--------------+
|         NULL |
+--------------+

Upvotes: 1

Related Questions