暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

mysql在给表新增主键时报错ERROR 1063

原创 Leo 2023-07-05
416

问题描述:mysql在给表新增主键时报错ERROR 1063,如下所示:

数据库:mysql 5.7.21

1、模拟异常

1.1、建测试表

mysql> create table fruits

    -> (

    -> s_id int not null,

    -> f_name char(255) not null,

    -> f_price decimal(8,2) not null

    -> );

Query OK, 0 rows affected (0.00 sec)

 

mysql> desc fruits;

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

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

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

| s_id    | int(11)      | NO   |     | NULL    |       |

| f_name  | char(255)    | NO   |     | NULL    |       |

| f_price | decimal(8,2) | NO   |     | NULL    |       |

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

3 rows in set (0.00 sec)

1.2、新增字段与主键

mysql> alter table fruits add f_id char(10) not null comment '主键ID';

Query OK, 0 rows affected (0.01 sec)

Records: 0  Duplicates: 0  Warnings: 0

 

mysql> alter table fruits change f_id f_id char(10) not null comment '主键ID' auto_increment primary key;

ERROR 1063 (42000): Incorrect column specifier for column 'f_id'

 

说明:如上所示,添加主键时报错ERROR 1063.

2、异常原因

auto_increment columns must be integer type (TINYINT, SMALLINT, INTEGER, or BIGINT)

3、解决方案

--调整f_id为bigint类型.

mysql> alter table fruits drop f_id;

Query OK, 0 rows affected (0.01 sec)

Records: 0  Duplicates: 0  Warnings: 0

 

mysql> desc fruits;

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

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

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

| s_id    | int(11)      | NO   |     | NULL    |       |

| f_name  | char(255)    | NO   |     | NULL    |       |

| f_price | decimal(8,2) | NO   |     | NULL    |       |

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

3 rows in set (0.00 sec)

 

mysql> alter table fruits add f_id bigint(20) not null comment '主键ID';

Query OK, 0 rows affected (0.01 sec)

Records: 0  Duplicates: 0  Warnings: 0

 

mysql> alter table fruits change f_id f_id bigint(20) not null comment '主键ID' auto_increment primary key;

Query OK, 0 rows affected (0.01 sec)

Records: 0  Duplicates: 0  Warnings: 0

 

说明:如上所示,成功添加主键.

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论