mysql 添加autoincrement_Mysql - 将auto_increment添加到主键

bd96500e110b49cbb3cd949968f18be7.png

I have a strange problem with mysql.

I am trying to alter a table's column which is a primary key and has an auto_increment constraint defined on it. This is also a foreign key reference for multiple other tables.

I need to change the length of this column in both , parent and all children.

set foreign_key_checks=0;

alter table Parent modify Identifier smallint(10) unsigned;

alter table Child_1 modify FK_Identifier smallint(10) unsigned;

alter table Child_2 modify FK_Identifier smallint(10) unsigned;

alter table Child_3 modify FK_Identifier smallint(10) unsigned;

alter table Child_4 modify FK_Identifier smallint(10) unsigned;

alter table Child_5 modify FK_Identifier smallint(10) unsigned;

set foreign_key_checks=1;

This removes the auto increment on the parent table. What would be the best way to add the constraint back ?

The below seems to be failing.

mysql> ALTER TABLE Parent MODIFY Identifier smallint(10) PRIMARY KEY AUTO_INCREMENT;

ERROR 1068 (42000): Multiple primary key defined

ALTER TABLE Parent MODIFY Identifier smallint(10) AUTO_INCREMENT;

------------------------

LATEST FOREIGN KEY ERROR

------------------------

110125 15:49:08 Error in foreign key constraint of table db/Child_1:

there is no index in referenced table which would contain

the columns as the first columns, or the data types in the

referenced table do not match to the ones in table. Constraint:

,

CONSTRAINT Child_1_ibfk_1 FOREIGN KEY (FK_Identifier) REFERENCES RoomProfile (Identifier) ON DELETE CASCADE ON UPDATE CASCADE

The index in the foreign key in table is PRIMARY

Is there a better way to achieve this ?

Edit : Show create is (after alter) :

CREATE TABLE `Parent` (

`Identifier` smallint(10) unsigned NOT NULL default '0',

`Name` varchar(20) default NULL,

`Description` varchar(100) default NULL,

PRIMARY KEY (`Identifier`),

UNIQUE KEY `Name` (`Name`),

) ENGINE=InnoDB DEFAULT CHARSET=latin1 |

before alter

`Identifier` smallint(5) unsigned NOT NULL AUTO_INCREMENT,

Thanks!

解决方案

You don't need to specify PRIMARY KEY in the MODIFY statement:

ALTER TABLE Parent MODIFY Identifier smallint(10) AUTO_INCREMENT;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值