众所周知,year(2) 以 2 位数字格式存储年份。例如,我们可以写 69 将 1969 存储为年份。在 year (2) 中,年份可以指定为 1970 到 2069(70 到 69)。
mysql 借助以下规则解释 2 位年份值 -
00-69 范围内的年份值将转换为 2000-2069。 70-99 范围内的年份值将转换为 1970-1999。 ul>我们不能将日期值存储为 2 位数字格式,因为由于世纪未知,以这种格式存储的值会变得模糊。
借助以下 mysql 可以更清楚地理解它示例 -
mysql> create table year_test(val year(2));query ok, 0 rows affected, 1 warning (0.23 sec)mysql> insert into year_test(val) values('70');query ok, 1 row affected (0.14 sec)mysql> insert into year_test(val) values('00');query ok, 1 row affected (0.06 sec)mysql> select * from year_test;+-----+| val |+-----+| 70 || 00 |+-----+2 rows in set (0.00 sec)mysql> select * from year_test where val = '1970';+-----+| val |+-----+| 70 |+-----+1 row in set (0.03 sec)mysql> select * from year_test where val = '2000';+-----+| val |+-----+| 00 |+-----+1 row in set (0.00 sec)mysql> select * from year_test where val = '1900';empty set (0.06 sec)
通过将 00 存储到“val”,我们不确定是指哪一年,“1900”还是“2000”。 mysql 将其解释为 2000 年。
以上就是为什么在 mysql 中使用两位数年份的日期值不是一个好习惯?的详细内容。