MySQL 日期时间的 SQL 自动递增
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21450914/
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
SQL Auto-Increment by DateTime
提问by RixsonL
Is there a way in my sql to auto increment by the date and time?
我的 sql 中有没有办法按日期和时间自动递增?
so say I have a table
所以说我有一张桌子
mytable
=====================================================
= Comment_ID = First_Name = Comment = DateTime =
=====================================================
= 1 = Bob = FooBar = 11-01-14 11:00 =
= 2 = Hyman = Blah = 11-01-14 12:29 =
= 3 = John = jonny = 12-01-14 07:09 =
=====================================================
Is there away to make the date auto-increment?
有没有让日期自动增加?
采纳答案by Tzar
Run this in your MySQL:
在你的 MySQL 中运行这个:
ALTER TABLE `mytable`
CHANGE COLUMN `DateTime` `DateTime` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ;
This sets your default value to CURRENT_TIMESTAMP
.
这会将您的默认值设置为CURRENT_TIMESTAMP
。
回答by Kuzgun
You can set a default value for your datetime column, now()
. This way whenever you add a row, it will automatically get current datetime.
您可以为日期时间列设置默认值now()
。这样,无论何时添加一行,它都会自动获取当前日期时间。