简体   繁体   中英

Cannot delete or update a parent row: a foreign key constraint fails. MagicDraw tables error

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)
);

我只是颠倒了删除表的请求的顺序并且它起作用了。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM