如何在 MySql 中为 sysdate 设置默认值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5178684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set default value to sysdate in MySql?
提问by Antoops
I am going to create a table with a column storing Created_date which is of datetime datatype. And my aim is to set its default value as sysdate().
我将创建一个表,其中包含一个存储日期时间数据类型的 Created_date 的列。我的目标是将其默认值设置为 sysdate()。
I tried
我试过
CREATE TABLE
tbl_table
(created_date
datetime DEFAULT sysdate())
创建表
tbl_table
(created_date
日期时间默认系统日期())
This gives me error saying not a valid default statement. I used similar logic in Oracle. Please help me to resolve this.
这给我错误说不是有效的默认语句。我在 Oracle 中使用了类似的逻辑。请帮我解决这个问题。
Thanks in advance.
提前致谢。
回答by theomega
Try
CREATE TABLE tbl_table ( created_date TIMESTAMP DEFAULT NOW())
尝试
CREATE TABLE tbl_table ( created_date TIMESTAMP DEFAULT NOW())
But: NOW
is different from sysdate
and TIMESTAMP
is different from datetime
, keep this in mind.
但是:NOW
不同于sysdate
并且TIMESTAMP
不同于datetime
,请记住这一点。
Normaly you only can use constants for default-values. TIMESTAMP
is the only column-type which supports a function like NOW()
. See herefor further information on the MySQL Bugtracker.
通常,您只能将常量用于默认值。TIMESTAMP
是唯一支持像NOW()
. 有关MySQL Bugtracker 的更多信息,请参见此处。