如何在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 18:57:47  来源:igfitidea点击:

How to set default value to sysdate in MySql?

mysqldefault-valueddl

提问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_datedatetime DEFAULT sysdate())

创建表tbl_tablecreated_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: NOWis different from sysdateand TIMESTAMPis different from datetime, keep this in mind.

但是:NOW不同于sysdate并且TIMESTAMP不同于datetime,请记住这一点。

Normaly you only can use constants for default-values. TIMESTAMPis the only column-type which supports a function like NOW(). See herefor further information on the MySQL Bugtracker.

通常,您只能将常量用于默认值。TIMESTAMP是唯一支持像NOW(). 有关MySQL Bugtracker 的更多信息,请参见此处