CURRENT_DATE/CURDATE() in DEFAULT Value for DATE Columns: Functionality and Limitations
In MySQL, you may encounter a situation where you wish to set a default value for a DATE column using the CURRENT_DATE or CURDATE() function. However, this approach often fails, raising the question of why it is not supported.
The answer lies in MySQL's restriction on default values. According to the MySQL documentation:
The DEFAULT clause specifies a default value for a column. With one exception, the default value must be a constant; it cannot be a function or an expression.
Therefore, MySQL does not allow using CURRENT_DATE or CURDATE() as a default value for a DATE column because these functions return the current date dynamically. Instead, default values must be static constants.
However, in MySQL 8.0.13 and above, this limitation has been removed. You can now use CURRENT_DATE or CURDATE() as the default value for a DATE column.
CREATE TABLE INVOICE( INVOICEDATE DATE NOT NULL DEFAULT CURRENT_DATE )
This change allows for the possibility of assigning the current date as the default value for DATE columns, providing more flexibility in data management.
The above is the detailed content of Why Can't I Use CURRENT_DATE/CURDATE() as a Default Value for DATE Columns in MySQL?. For more information, please follow other related articles on the PHP Chinese website!