gpt4 book ai didi

mysql - 如何在mysql中创建只有日期的时间戳?

转载 作者:行者123 更新时间:2023-11-29 07:01:41 25 4
gpt4 key购买 nike

我正在尝试创建一个只有日期的时间戳。这就是我开始工作的原因。

create table verses 
(
id INT AUTO_INCREMENT NOT NULL PRIMARY KEY,
username VARCHAR(255),
content VARCHAR(1023),
time DATE
);

insert into verses (username, content, time)
values ('test', 'blbabblabalbla', CURDATE());

这行得通,但我每次都需要插入时间。有没有办法自动执行此操作?就像我们使用 TIMESTAMP 时一样?我试过这个但没用

create table verses 
(
id INT AUTO_INCREMENT NOT NULL PRIMARY KEY,
username VARCHAR(255),
content VARCHAR(1023),
time CURDATE(TIMESTAMP)
);

最佳答案

您必须将字段设置为 datetime 而不是 date

还有文档说:

As of MySQL 5.6.5, TIMESTAMP and DATETIME columns can be automatically initializated and updated to the current date and time [...]

还有:

For any TIMESTAMP or DATETIME column in a table, you can assign the current timestamp as the default value, the auto-update value, or both. An auto-initialized column is set to the current timestamp for inserted rows that specify no value for the column. [...]

如果您想了解更多信息,请参阅 official documentation .

关于mysql - 如何在mysql中创建只有日期的时间戳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9866897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com