Search code examples
mysqlsqldefaultcreate-table

in mysql8 why does DATE DEFAULT CURRENT_DATE need parentheses around CURRENT_DATE?


as of mysql8, this is legal:

CREATE TABLE t (d DATETIME DEFAULT CURRENT_TIMESTAMP);

and this is legal:

CREATE TABLE t (d DATE DEFAULT (CURRENT_DATE));

so why is this illegal?

CREATE TABLE t (d DATE DEFAULT CURRENT_DATE);

resulting in a

Error in query (1064): Syntax error near 'CURRENT_DATE)' at line 1


Solution

  • Refer to this for more information.

    This is most likely, what you were referring to in your question:

    The default value specified in a DEFAULT clause can be a literal constant or an expression. With one exception, enclose expression default values within parentheses to distinguish them from literal constant default values.

    [...]

    The exception is that, for TIMESTAMP and DATETIME columns, you can specify the CURRENT_TIMESTAMP function as the default, without enclosing parentheses.