Home >Database >Mysql Tutorial >Why Doesn't `DEFAULT CURRENT_DATE` Work in MySQL (and When Does It?)

Why Doesn't `DEFAULT CURRENT_DATE` Work in MySQL (and When Does It?)

Barbara Streisand
Barbara StreisandOriginal
2024-12-10 06:19:14361browse

Why Doesn't `DEFAULT CURRENT_DATE` Work in MySQL (and When Does It?)

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn