I have an assignment where I have to make tables in MagicDraw and then to generate MySQL code. I pasted the generated code to SQL code section in phpMyAdmin and it showed an error:
SQL query:
DROP TABLE IF EXISTS LogistikosCentras;
MySQL answer:
#1217 - Cannot delete or update a parent row: a foreign key constraint fails
These are my tables:
And this is some of my code:
CREATE TABLE LogistikosCentras
(
pavadinimas varchar (255) NOT NULL,
adresas varchar (255) NOT NULL,
telefonas int NOT NULL,
el_pastas varchar (255) NOT NULL,
id_LogistikosCentras integer NOT NULL,
PRIMARY KEY(id_LogistikosCentras)
);
CREATE TABLE SiuntosPervezimoTarnyba
(
pavadinimas varchar (255) NOT NULL,
transportoPriemonesTalpa double precision NOT NULL,
transportoPriemoniuKiekis int NOT NULL,
transportoPriemone varchar (15) NOT NULL,
pristatymoGreitis varchar (8) NOT NULL,
id_SiuntosPervezimoTarnyba integer NOT NULL,
CHECK(transportoPriemone in ('sunkvezimis', 'laivas', 'lektuvas',
'lengvojiMasina')),
CHECK(pristatymoGreitis in ('valandos', 'dienos')),
PRIMARY KEY(id_SiuntosPervezimoTarnyba)
);
CREATE TABLE Darbuotojas
(
vardas varchar (255) NOT NULL,
pavarde varchar (255) NOT NULL,
tabelio_nr char (255) NOT NULL,
pareigos char (10) NOT NULL,
id_Darbuotojas integer NOT NULL,
fk_LogistikosCentrasid_Logistikoscentras integer NOT NULL,
CHECK(pareigos in ('buhalteris', 'krovejas')),
PRIMARY KEY(id_Darbuotojas, fk_LogistikosCentrasid_Logistikoscentras),
CONSTRAINT dirba FOREIGN KEY(fk_LogistikosCentrasid_Logistikoscentras)
REFERENCES LogistikosCentras (id_LogistikosCentras)
);
I just reversed the order of the requests to delete tables and it worked.