timestamp存储
⑴ datetime和timestamp的区别
datetime和timestamp是出现在MySQL和SqlServer数据库中的。
MySQL中datetime和timestamp的区别:
(一)范围不同。
“datetime” 以'YYYY-MM-DD HH:MM:SS'格式检索和显示DATETIME值。支持的范围为'1000-01-01 00:00:00'到'9999-12-31 23:59:59'TIMESTAMP值不能早于1970或晚于2037
(二)储存不同。
1,TIMESTAMP
①4个字节储存(Time stamp value is stored in 4 bytes)
②值以UTC格式保存( it stores the number of milliseconds)
③时区转化 ,存储时对当前的时区进行转换,检索时再转换回当前的时区。
2,datetime
①8个字节储存(8 bytes storage)
②实际格式储存(Just stores what you have stored and retrieves the same thing which you have stored.)
③与时区无关(It has nothing to deal with the TIMEZONE and Conversion.)
注:TIMESTAMP列的显示格式与datetime列相同。
SqlServer中datetime和timestamp在中用法区别基本都差不多。
datetime :
SqlServer中用两个 4 字节的整数内部存储 datetime 数据类型的值。第一个 4 字节存储 base date(即 1900 年 1 月 1 日)之前或之后的天数。基础日期是系统参考日期。不允许早于 1753 年 1 月 1 日的 datetime 值。另外一个 4 字节存储以午夜后毫秒数所代表的每天的时间。
注:smalldatetime 数据类型存储日期和每天的时间,但精确度低于 datetime。SQL Server 将 smalldatetime 的值存储为两个 2 字节的整数。第一个 2 字节存储 1900 年 1 月 1 日后的天数。另外一个 2 字节存储午夜后的分钟数。日期范围从1900 年 1 月 1 日到 2079 年 6 月 6 日,精确到分钟。
timestamp:
timestamp 这种数据类型表现自动生成的二进制数,确保这些数在数据库中是唯一的。timestamp 一般用作给表行加版本戳的机制。存储大小为 8 字节。