创建表
CREATE TABLE parent(id INT NOT NULL, PRIMARY KEY(id))ENGINE=INNODB; CREATE TABLE child(id INT,parent_id INT,FOREIGN KEY(parent_id) REFERENCES parent(id))ENGINE=INNODB; insert into parent(id) values(1); insert into child values(1,1); select * from parent; select * from child; update parent set id = 2 where id = 1;
删除约束
首先查看约束名:
show create table child;
则child_ibfk_1
是外键名称,删除外键
ALTER TABLE child DROP FOREIGN KEY child_ibfk_1
增加约束
ALTER TABLE child ADD CONSTRAINT FOREIGN KEY(parent_id) REFERENCES parent(id); ALTER TABLE child ADD CONSTRAINT KF_PARENT_CHILD FOREIGN KEY(parent_id) REFERENCES parent(id);
来源:https://www.cnblogs.com/zhichun/p/12443935.html