CURRENT_DATE/CURDATE() Default Value Issues in MySQL
One common dilemma faced by developers involves using the CURRENT_DATE or CURDATE() functions as default values for DATE columns in MySQL. Despite expectations, these functions do not function as intended.
The reason lies in MySQL's restrictions on default values. Typically, default values must be constants, not functions or expressions. Therefore, setting the default value of a DATE column to NOW() or CURRENT_DATE will fail.
CREATE TABLE INVOICE( INVOICEDATE DATE NOT NULL DEFAULT CURRENT_DATE )
[Edit: Support for CURRENT_DATE/CURDATE() as Default Values]
It is worth noting that MySQL version 8.0.13 introduced support for CURRENT_DATE and CURDATE() as default values for DATE columns. However, prior versions do not support this functionality.
Exception for TIMESTAMP Columns
A notable exception to the rule is the possibility of using CURRENT_TIMESTAMP as the default value for TIMESTAMP columns, which is supported by MySQL. Please refer to MySQL documentation for more details on this aspect.
The above is the detailed content of Why Doesn't MySQL Always Accept CURRENT_DATE/CURDATE() as a Default Value?. For more information, please follow other related articles on the PHP Chinese website!