是否可以在 MySQL 中创建具有 UNIX_TIMESTAMP 默认值的列?
我正在尝试这样做,但 MySQL 似乎不允许我这样做。这个问题有解决方案吗?或者我是否希望始终在 INSERT 查询中包含该函数?
CREATE TABLE foo(
created INT NOT NULL DEFAULT UNIX_TIMESTAMP()
)
我知道 TIMESTAMP 类型接受 CURRENT_TIMESTAMP 默认值,但我的客户坚持在数据库中使用纪元时间。
I'm trying to do this, but it seems like MySQL isn't allowing me. Is there a solution to this issue or am I expected to always include the function in my INSERT queries?
CREATE TABLE foo(
created INT NOT NULL DEFAULT UNIX_TIMESTAMP()
)
I'm aware of the TIMESTAMP type that accepts a CURRENT_TIMESTAMP default, but my client insisted on using epoch time in the database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
MySQL 实现
TIMESTAMP
数据类型的方式,实际上是将纪元时间存储在数据库中。因此,如果您想将其显示为CURRENT_TIMESTAMP
,则只需使用默认值为CURRENT_TIMESTAMP
的TIMESTAMP
列,然后对其应用UNIX_TIMESTAMP()
即可。 int:但是,如果您确实希望列的数据类型为
INT
,您可以使用 R. Bemrose 的建议并通过触发器设置它:The way MySQL implements the
TIMESTAMP
data type, it is actually storing the epoch time in the database. So you could just use aTIMESTAMP
column with a default ofCURRENT_TIMESTAMP
and apply theUNIX_TIMESTAMP()
to it if you want to display it as an int:However, if you really want the datatype of the column to be
INT
, you can use R. Bemrose's suggestion and set it via trigger:来自文档:
From the documentation:
您可以为此创建触发器。
在 {table_name} FOR EACH ROW SET INSERT 之前创建触发器 {trigger_name} new.{field_name} = UNIX_TIMESTAMP(NOW());
在插入 foo 之前为每个行集创建触发器 my_trigger_name_1 new.created = UNIX_TIMESTAMP(NOW());
在更新 {table_name} 之前创建触发器 {trigger_name} FOR EACH ROW SET new.{field_name} = UNIX_TIMESTAMP(NOW());
在更新 foo 之前为每个行集创建触发器 my_trigger_name_2 new.created = UNIX_TIMESTAMP(NOW());
You can create triggers for this.
CREATE TRIGGER {trigger_name} BEFORE INSERT ON {table_name} FOR EACH ROW SET new.{field_name} = UNIX_TIMESTAMP(NOW());
CREATE TRIGGER my_trigger_name_1 BEFORE INSERT ON foo FOR EACH ROW SET new.created = UNIX_TIMESTAMP(NOW());
CREATE TRIGGER {trigger_name} BEFORE UPDATE ON {table_name} FOR EACH ROW SET new.{field_name} = UNIX_TIMESTAMP(NOW());
CREATE TRIGGER my_trigger_name_2 BEFORE UPDATE ON foo FOR EACH ROW SET new.created = UNIX_TIMESTAMP(NOW());
正如 ktretyak 的回答,使用 MySQL v8+,您可以使用括号来做到这一点。
一些插入后,您可以看到它工作正常。
(在 MySQL 8.0.26 上测试 + 仅修补文本颜色)
或者,您也可以使用表达式。
检查ktretyak的回答。
我写这个答案而不是评论或编辑答案(尝试并拒绝)
由于缺乏声誉,现在使用括号是一个很好的解决方案。
As ktretyak's answer, with MySQL v8+, you can do that using a parentheses.
After some inserts, you can see it works correctly.
(Tested on MySQL 8.0.26 + only text color patched)
Alternatively you can use an expression, too.
Check ktretyak's answer.
I write this answer instead of comment or edit answer(tried and rejected)
because of lack of REPUTATION and using a parentheses is good solution now.
好吧,如果 MySQL 不允许您直接执行此操作,您始终可以使用
BEFORE INSERT...FOR EACH ROW
触发器。Well, if MySQL won't let you do it directly, you can always do it using a
BEFORE INSERT... FOR EACH ROW
trigger.现在,使用 MySQL v8+,您只需在括号中即可执行此操作:
请参阅 文档
Now, with MySQL v8+, you can to do this just in parentheses:
See docs