MySQL 自动时间戳新条目到数据库 (phpMyAdmin)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3615471/
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 17:01:33  来源:igfitidea点击:

Auto TimeStamp new entry to DB (phpMyAdmin)

mysqltimestampphpmyadmin

提问by BigMike

If I want each new entry into my db to be automatically timestamped, would I set the Field Type to "timestamp" and have the Default value set to "CURRENT_TIMESTAMP"?

如果我希望数据库中的每个新条目都自动加上时间戳,是否将字段类型设置为“时间戳”并将默认值设置为“CURRENT_TIMESTAMP”?

Is this the correct method?

这是正确的方法吗?

回答by BoltClock

That is correct. In SQL code that would be:

那是正确的。在 SQL 代码中,这将是:

CREATE TABLE `table` (
    ...
    `date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, 
    ...
)

回答by Michael Pakhantsov

Yes, this method is correct:

是的,这个方法是正确的:

 create table t(Id int, ts timestamp default current_timestamp)

 insert into t(Id) values (1)

 select * from t

 1;2010-09-01 09:20:09:000