mysql CURRENT_TIMESTAMP给我全部0

时间:2012-08-23 05:05:01

标签: mysql sql timestamp utc

如果我运行以下查询:

ALTER TABLE `price_s` ADD COLUMN `ts` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP  AFTER `price_s_id` ;

新列将填充0000-00-00 00:00:00。 在修改列之前,此列中的值不会更新为当前时间戳,即ON UPDATE CURRENT_TIMESTAMP似乎工作正常。

如果我跑:

SELECT version( ) , @@time_zone , @@system_time_zone , NOW( ) , UTC_TIMESTAMP( )

我得到了:

'5.5.15', '+10:00', 'EST', '2012-08-23 14:56:59', '2012-08-23 04:56:59'

我想将UTC时间存储在ts列中,我该怎么做?

不确定这是否相关,我试图生成mysql.time_zone*表:

Miranda-Macbook: ./mysql_tzinfo_to_sql /usr/share/zoneinfo | ./mysql -p -u root mysql
Enter password: Warning: Unable to load '/usr/share/zoneinfo/+VERSION' as time zone. Skipping it.

Warning: Unable to load '/usr/share/zoneinfo/Asia/Riyadh87' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/Asia/Riyadh88' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/Asia/Riyadh89' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/Mideast/Riyadh87' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/Mideast/Riyadh88' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/Mideast/Riyadh89' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.

感谢@Sameer和@Marc B. 现在我更了解时间戳了。(在article的帮助下) 它已经存储在UTC中,所以我需要的是:

SET time_zone = 'SYSTEM';
UPDATE tablename SET ts=now()

1 个答案:

答案 0 :(得分:2)

INSERT INTO yourtable (ts) VALUES (now())
UPDATE yourtable SET ts=now()

只要你填入ts字段的是一个有效的mysql时间戳字符串(yyyy-mm-dd hh:mm:ss),时区就不重要了。这只在转换或检索时变得相关 - mysql datefields本身没有时区概念,它们只是某些日期/时间数据的存储库。