mysql中timestamp类型的列默认值为current_timestamp的只能有一个

时间:2022-01-23 01:51:35

mysql中,类型为timestamp的列,默认为current_timestamp类型,并且在更新的时候会自动更新为当前值。因此一个表中设置多个timestamp类是没有意义的,一般设置为datatime类型。
TIMESTAMP列创建后的格式是:

a timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

一般,创建表的时候,创建时间字段用datatime,更新时间字段用timestamp

timestamp与datatime区别

(1)显示

格式相同,都是19个字符长:YYYY-MM-DD HH:MM:SS

(2)范围

datetime :支持的范围为”1000-01-01 00:00:00”到”9999-12-31 23:59:59”
TIMESTAMP:值不能早于1970或晚于2037

(3)存储

TIMESTAMP
1.4个字节储存(Time stamp value is stored in 4 bytes)
2.值以UTC格式保存( it stores the number of milliseconds)
3.时区转化 ,存储时对当前的时区进行转换,检索时再转换回当前的时区。
datetime
1.8个字节储存(8 bytes storage)
2.实际格式储存(Just stores what you have stored and retrieves the same thing which you have stored.)
3.与时区无关(It has nothing to deal with the TIMEZONE and Conversion.)