ITPub博客

首页 > Linux操作系统 > Linux操作系统 > mysql的date,datetime,timestamp的时间范围

mysql的date,datetime,timestamp的时间范围

原创 Linux操作系统 作者:hquxiaoqi 时间:2011-11-02 15:27:42 0 删除 编辑
我的mysql版本是5.0.67-community-nt.
今天看了下,上面说timestamp有一个2030-01-01的上限,于是就做了实验,发现不对.真正的范围是从1970-01-01 08:00:01 2038-01-19 11:14:07,可能MySQL版本不一样的话,时间函数的范围也不一样吧.
mysql> drop table if exists tt;
Query OK, 0 rows affected, 1 warning (0.00 sec)
 
mysql> create table tt(time_stamp timestamp);
Query OK, 0 rows affected (0.06 sec)
 
mysql> insert into tt values(20380119111407);
Query OK, 1 row affected (0.01 sec)
 
mysql> insert into tt values(20380119111408);
ERROR 1292 (22007): Incorrect datetime value: '20380119111408' for column 'time_
stamp' at row 1
 
 
mysql> select * from tt;
+---------------------+
| time_stamp          |
+---------------------+
| 2038-01-19 11:14:07 |
+---------------------+
1 row in set (0.00 sec)
 
然后清空表.
mysql> truncate tt;
Query OK, 15 rows affected (0.03 sec)
 
mysql> insert into tt values(19700101080001);
Query OK, 1 row affected (0.03 sec)
 
mysql> insert into tt values(19700101080000);
ERROR 1292 (22007): Incorrect datetime value: '19700101080000' for column 'time_
stamp' at row 1

mysql> select * from tt;
+---------------------+
| time_stamp          |
+---------------------+
| 1970-01-01 08:00:01 |
+---------------------+
1 row in set (0.00 sec)
 
从上面这个实验可以看出,当我插入时间20380119111407时,插入成功.当插入20380119111408时,显示错误.
当我插入时间19700101080001时,插入成功.当插入19700101080000时,显示错误.
 
 
接着我又对date和datetime做了实验,发现date的范围是0000-00-00到9999-12-31.
datetime的范围是0000-00-00 00:00:00到9999-12-31 23:59:59.
 
mysql> drop table tt;
Query OK, 0 rows affected (0.02 sec)
 
mysql> create table tt(date date);
Query OK, 0 rows affected (0.12 sec)
 
mysql> insert into tt values("0000-00-00");
Query OK, 1 row affected (0.03 sec)
 
mysql> select * from tt;
+------------+
| date       |
+------------+
| 0000-00-00 |
+------------+
1 row in set (0.00 sec)

mysql> insert into tt values("9999-12-31");
Query OK, 1 row affected (0.02 sec)
 
mysql> select * from tt;
+------------+
| date       |
+------------+
| 0000-00-00 |
| 9999-12-31 |
+------------+
2 rows in set (0.00 sec)
 
mysql> drop table tt;
Query OK, 0 rows affected (0.02 sec)
 
mysql> create table tt(date_time datetime);
Query OK, 0 rows affected (0.04 sec)
 
mysql> insert into tt values("0000-00-00 00:00:00");
Query OK, 1 row affected (0.02 sec)
 
mysql> insert into tt values("9999-12-31 23:59:59");
Query OK, 1 row affected (0.02 sec)
 
mysql> select * from tt;
+---------------------+
| date_time           |
+---------------------+
| 0000-00-00 00:00:00 |
| 9999-12-31 23:59:59 |
+---------------------+
2 rows in set (0.00 sec)
 
 
 
 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/25281640/viewspace-710118/,如需转载,请注明出处,否则将追究法律责任。

下一篇: MySQL的类型转换
请登录后发表评论 登录
全部评论

注册时间:2011-01-22

  • 博文量
    22
  • 访问量
    80899