Home > Database > Mysql Tutorial > Why Doesn't MySQL Always Accept CURRENT_DATE/CURDATE() as a Default Value?

Why Doesn't MySQL Always Accept CURRENT_DATE/CURDATE() as a Default Value?

Barbara Streisand
Release: 2024-12-07 11:49:12
Original
804 people have browsed it

Why Doesn't MySQL Always Accept CURRENT_DATE/CURDATE() as a Default Value?

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
)
Copy after login

[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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template