CURRENT_DATE/CURDATE() as Default DATE Value Fix
Despite its straightforward appearance, setting CURRENT_DATE or CURDATE() as the default value for a DATE column using the syntax below may not function:
CREATE TABLE INVOICE( INVOICEDATE DATE NOT NULL DEFAULT CURRENT_DATE )
Reason:
Although this code may seem intuitive, it fails due to a MySQL limitation. According to the documentation, the DEFAULT clause mandates a constant value for a column. Functions or expressions, such as CURRENT_DATE, are not permitted.
Solution:
Previously, this issue persisted for MySQL versions prior to 8.0.13. However, in MySQL 8.0.13 and later, you can now use CURRENT_DATE() or CURDATE() as the default value for DATE columns, eliminating the need for workarounds.
The above is the detailed content of Why Doesn't `DEFAULT CURRENT_DATE` Work in MySQL (and When Does It?). For more information, please follow other related articles on the PHP Chinese website!