En la estructura de la base de datos de
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;
Espero eliminar el padre City
al dejar el valor correspondiente en el niño intacto por estos tres comandos iguales
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)
Pero cuando se usa NO ACTION
OR RESTRICT
o se omite ON DELETE
. MySQL no me permite eliminar de la columna principal con este 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
Donde me equivoco ¿No es responsabilidad de los SQL NO ACTION
eliminar al padre y dejar al niño huérfano?
fuente