温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-08-03 06:35:00 来源:网络 阅读:6804 作者:loong576 栏目:MySQL数据库

最近修改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.查看是否被锁
错误代码1833 Cannot change column used in a foreign
3.解除server_history表外键约束
ALTER TABLE server_history DROP FOREIGN KEY server_history_ibfk_1;
4.更改server_list表server_lip字段长度
ALTER TABLE server_list MODIFY COLUMN server_lip CHAR(25);
查看表结构发现已完成字段长度更新
错误代码1833 Cannot change column used in a foreign
5.新增server_history表外键约束
ALTER TABLE server_history ADD CONSTRAINT server_history_ibfk_1 FOREIGN KEY (history_ip) REFERENCES server_list (server_lip);
6.解除锁定
UNLOCK TABLES;

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI