您好,登錄后才能下訂單哦!
最近修改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.查看是否被鎖
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);
查看表結構發現已完成字段長度更新
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;
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。