当前位置:Gxlcms > 数据库问题 > mysql中datetime和timestamp类型的区别

mysql中datetime和timestamp类型的区别

时间:2021-07-01 10:21:17 帮助过:7人阅读

相同

显示

TIMESTAMP列的显示格式与DATETIME列相同。换句话说,显示宽度固定在19字符,并且格式为YYYY-MM-DD HH:MM:SS。

不同

范围

datetime 以‘YYYY-MM-DD HH:MM:SS‘格式检索和显示DATETIME值。支持的范围为‘1000-01-01 00:00:00‘到‘9999-12-31 23:59:59‘TIMESTAMP值不能早于1970或晚于2037

储存

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.)

实例对比

现在我来做个时区对他们的影响。

1.先插入一个数据insert into `t8` values(now(), now());

2.改变客户端时区(东9区,日本时区)。

3.再次显示插入的数据,变化了,timestamp类型的数据 增加了 1个小时

mysql> desc t2;

+-------+-----------+------+-----+-------------------+-----------------------------+

| Field | Type      | Null | Key | Default           | Extra                       |

+-------+-----------+------+-----+-------------------+-----------------------------+

| date1 | datetime  | YES  |     | NULL              |                             |

| date2 | timestamp | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |

+-------+-----------+------+-----+-------------------+-----------------------------+

2 rows in set (0.00 sec)
mysql> insert into t2 values (now(),now());

Query OK, 1 row affected (0.00 sec)
mysql> select * from t2;

+---------------------+---------------------+

| date1               | date2               |

+---------------------+---------------------+

| 2015-04-08 13:45:46 | 2015-04-08 13:45:46 |

+---------------------+---------------------+

1 row in set (0.00 sec)
mysql> set time_zone=‘+9:00‘;

Query OK, 0 rows affected (0.00 sec)
mysql> select * from t2;

+---------------------+---------------------+

| date1               | date2               |

+---------------------+---------------------+

| 2015-04-08 13:45:46 | 2015-04-08 14:45:46 |

+---------------------+---------------------+

1 row in set (0.00 sec)

mysql中datetime和timestamp类型的区别

标签:范围   extra   second   retrieve   --   mil   values   支持   0.00   

人气教程排行