错误代码1833 Cannot change column used in a foreign

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了错误代码1833 Cannot change column used in a foreign相关的知识,希望对你有一定的参考价值。

最近修改mysql数据库表中的字段长度时报错,执行更改的sql语句:
ALTER TABLE server_list MODIFY COLUMN server_lip CHAR(25);

报错信息:
1 queries executed, 0 success, 1 errors, 0 warnings
查询:alter table server_list modify column server_lip char(25)
错误代码: 1833
Cannot change column ‘server_lip‘: used in a foreign key constraint ‘server_history_ibfk_1‘ of table ‘OMServer.server_history‘

原因:server_list表中的server_lip字段为server_history表的外键,server_history建表语句:
CREATE TABLE server_history (
ID int(11) NOT NULL AUTO_INCREMENT COMMENT ‘主键ID‘,
history_id int(11) NOT NULL COMMENT ‘事件ID‘,
history_ip char(15) NOT NULL COMMENT ‘事件IP地址‘,
history_user char(15) NOT NULL COMMENT ‘事件用户名‘,
history_datetime datetime NOT NULL COMMENT ‘事件时间‘,
db_datetime timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT ‘入库时间‘,
history_command char(255) NOT NULL COMMENT ‘事件命令‘,
PRIMARY KEY (ID),
KEY history_ip (history_ip),
CONSTRAINT server_history_ibfk_1 FOREIGN KEY (history_ip) REFERENCES server_list (server_lip)
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8 COMMENT=‘操作事件表‘

解决步骤:
1.锁表
LOCK TABLES server_history WRITE,server_list WRITE;
2.查看是否被锁
技术分享图片
4.解除server_history表外键约束
ALTER TABLE server_history DROP FOREIGN KEY server_history_ibfk_1;
5.更改server_list表server_lip字段长度
ALTER TABLE server_list MODIFY COLUMN server_lip CHAR(25);
查看表结构发现已完成字段长度更新
技术分享图片
6.新增server_history表外键约束
ALTER TABLE server_history ADD CONSTRAINT server_history_ibfk_1 FOREIGN KEY (history_ip) REFERENCES server_list (server_lip);
7.解除锁定
UNLOCK TABLES;

以上是关于错误代码1833 Cannot change column used in a foreign的主要内容,如果未能解决你的问题,请参考以下文章

centos yum安装ftp 及解决vsftp错误500 OOPS: cannot change directory:/home/**

cannot change version web module 3.0

Cannot find name ‘Set‘. Do you need to change your target library? Try changing the `lib` compiler o

Cannot find name ‘Set‘. Do you need to change your target library? Try changing the `lib` compiler o

Redux 中的调度操作给出错误'[react-router] You cannot change <Router routes>;它将被忽略

eclipse有时候会报错:Cannot change version of project facet Dynamic Web Module to 2.5。这个错误不会影响程序的运行,不过看着总是不