How to Create a Table with a Date Type Column Defaulting to the Current Date

Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: 建表 Date 类型的列,默认当前日期怎样实现

| username: 不输土豆

Datetime columns can be set to the current time, how can the Date column have its default value set to the current date?

create table TimeTest (
    InsertDateTime Datetime default current_timestamp, 
    InsertDate Date default current_date
);
| username: tidb菜鸟一只 | Original post link

It can’t be implemented. MySQL only implemented it in version 8.0.13 and later. TiDB 6.5 is still compatible with MySQL 5.7. Try implementing it in the application or upgrade TiDB to 8.1 to see if it works.

| username: zhaokede | Original post link

MySQL 8.0.25-230102 is not supported.

| username: TiDBer_LM | Original post link

Starting from version 8.0.13, it is supported. You need to enclose CURRENT_DATE in parentheses:
CREATE TABLE t1 (d DATE DEFAULT (CURRENT_DATE));

| username: zhaokede | Original post link

Learned something new again.
Based on the SQL you provided, the table was created successfully, and the default values are correct.

| username: wfxxh | Original post link

So, you’re saying that the DateTime type works, but the Date type doesn’t, right?

| username: TiDBer_3Cusx9uk-0775 | Original post link

Compatibility issues, it is recommended to upgrade the TiDB version.

| username: 濱崎悟空 | Original post link

It seems that it requires a new version to support it.

| username: tidb狂热爱好者 | Original post link

Support for the new version

| username: TiDBer_G64jJ9u8 | Original post link

Senior, what did you use for deployment? Was it deployed with Ti-Operator? With this version, after my deployment, PD often fails to establish the cluster and then exits. Therefore, it can’t start.

| username: tidb狂热爱好者 | Original post link

I used tiup for deployment.