In the database structure of
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;
I expect to delete parent from City by leaving the corresponding value in child intact by these three equal commands
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)
But when using NO ACTION OR RESTRICT or omitting ON DELETE. MySQL does not allow me to delete from parent column with this error:
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
Where am I wrong? Isn't it the responsibility of the SQL's NO ACTION to delete the parent and leave the child orphan?