在数据库结构中
CREATE TABLE Country (
name varchar(40) NOT NULL,
PRIMARY KEY (name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE City (
name varchar(40) NOT NULL,
PRIMARY KEY (name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE Map (
country varchar(40) NOT NULL,
city varchar(100) NOT NULL,
PRIMARY KEY (country,city),
FOREIGN KEY (country) REFERENCES Country (name) ON DELETE CASCADE,
FOREIGN KEY (city) REFERENCES City (name) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
我希望City
通过这三个相等的命令完整保留子级中的相应值来删除父级
FOREIGN KEY (city) REFERENCES City (name) ON DELETE NO ACTION
FOREIGN KEY (city) REFERENCES City (name) ON DELETE RESTRICT
FOREIGN KEY (city) REFERENCES City (name)
但是当使用NO ACTION
OR RESTRICT
或省略时ON DELETE
。MySQL不允许我从父列中删除此错误:
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails
('test'.'Map', CONSTRAINT 'Map_ibfk_2' FOREIGN KEY ('city') REFERENCES 'City'('name')
ON DELETE RESTRICT
我哪里错了?SQL的责任是NO ACTION
删除父级并留下子级孤儿吗?