mysql中year()_MySQL中的YEAR函数

一般这样认为 YEAR类型占用1个字节,并且在定义时可以指定显示的宽度为YEAR(4)或者YEAR(2),不过从MySQL5.6.6开始,YEAR(2)类型被自动装换为YEAR(4),YEAR(2)类型被禁用。

(一)YEAR(2)类型被自动装换为YEAR(4)

mysql>drop table if exists t;

Query OK,0 rows affected (0.01 sec)

mysql>create table t(a year(2));

Query OK,0 rows affected, 1 warning (0.00 sec)

mysql>show warnings\G;

***************************1. row ***************************

Level: Warning

Code: 1818

Message:YEAR(2) column type is deprecated. Creating YEAR(4) column instead.

1 row inset (0.00 sec)

ERROR:

No queryspecified

mysql>explain t;

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

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

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

| a     | year(4) | YES  |     |NULL    |       |

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

1 row inset (0.00 sec)

mysql>insert into t select '14';

Query OK,1 row affected (0.00 sec)

Records:1  Duplicates: 0  Warnings: 0

mysql>select * from t;

+------+

| a    |

+------+

| 2014 |

+------+

1 row inset (0.00 sec)

mysql>

(二)存在当前表中的YEAR(2)类型仍旧作为YEAR(2)存在和处理,但是下面几种方式会自动转换为YEAR(4):

(1)alter表

alter table语句与导致重建表:

mysql> alter table t modify a year(2);

Query OK, 0 rows affected, 1 warning (0.00 sec)

Records: 0  Duplicates: 0  Warnings: 1

mysql> explain t;

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

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

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

| a     | year(4) | YES  |     | NULL    |       |

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

1 row in set (0.00 sec)

mysql> alter table t modify a year(2);

Query OK, 0 rows affected, 1 warning (0.01 sec)

Records: 0  Duplicates: 0  Warnings: 1

mysql> show warnings;

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

| Level   | Code | Message                                                             |

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

| Warning | 1818 | YEAR(2) column type is deprecated. Creating YEAR(4) column instead. |

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

1 row in set (0.01 sec)

(2)PEAIR TABLE

如果check table时,数据库发现一张表中包含YEAR(2)列,就推荐使用YEAR(4)。

(3)mysql_upgrade

这个用于REPAIR TABLE的情况。

(4)在dump文件和重新装载dump文件时,被影响的数据值都会在dump和装载时存在潜在的影响。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值