0

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:

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)
);
GMB
  • 216,147
  • 25
  • 84
  • 135
Eglė Ged
  • 27
  • 6
  • Welcome. MySQL is telling you the issue, you will need to remove the foreign key constraint in table `Darbuotojas` before attempting to drop `LogistikosCentras`. https://dev.mysql.com/doc/refman/5.6/en/constraint-foreign-key.html – JBES Feb 28 '19 at 20:56
  • 2
    Possible duplicate of [Can't drop a MySQL table due to foreign key constraints](https://stackoverflow.com/questions/32851989/cant-drop-a-mysql-table-due-to-foreign-key-constraints) – GMB Feb 28 '19 at 21:29
  • Thank you for helping. I just reversed the order of the requests to delete tables and it worked. – Eglė Ged Mar 01 '19 at 18:28

1 Answers1

0

I just reversed the order of the requests to delete tables and it worked.

Eglė Ged
  • 27
  • 6