oracle 违反完整性约束-值存在时找不到父键[关闭]

slwdgvem  于 2023-10-16  发布在  Oracle
关注(0)|答案(1)|浏览(164)

已关闭此问题为not reproducible or was caused by typos。它目前不接受回答。

此问题是由打印错误或无法再重现的问题引起的。虽然类似的问题可能是on-topic在这里,这一个是解决的方式不太可能帮助未来的读者。
11天前关闭
Improve this question

CREATE TABLE Film
(
    idFilm        NUMBER         GENERATED BY DEFAULT as IDENTITY (START WITH 1 INCREMENT BY 1) PRIMARY KEY,
    titre         VARCHAR2(100)  NOT NULL,
    anneeDeSortie NUMBER(4)      NOT NULL,
    langueOrigine VARCHAR2(25),
    duree         Number(3)      NOT NULL,
    resume        VARCHAR2(600)  NOT NULL,
    affiche       VARCHAR2(2083),
    realisateur   number,
    constraint fk_idRealisateur
        FOREIGN KEY (realisateur) REFERENCES Personnalite (idPersonnalite)
);

它包含以下行:

SELECT * FROM film where film.idfilm = 164184;

当我尝试向表Scenariste添加一行时:

CREATE TABLE Scenariste
    (
    idScenariste  NUMBER GENERATED ALWAYS as IDENTITY (START WITH 1 INCREMENT BY 1) PRIMARY KEY,
    nom           VARCHAR2(100)  NOT NULL,
    idFilm        NUMBER         NOT NULL,
    constraint fk_idScenariste
        FOREIGN KEY (idScenariste) REFERENCES Film (idFilm)
);

INSERT INTO scenariste (nom, idFilm) VALUES ('Paul Attanasio', 164184);

我得到以下错误:

Error starting at line : 3 in command -
INSERT INTO scenariste (nom, idFilm) VALUES ('Paul Attanasio', 164184)
Error report -
ORA-02291: integrity constraint (EQUIPE106.FK_IDSCENARISTE) violated - parent key not found

如果ID为164184的胶片行确实存在,为什么会发生这种情况?

ercv8c1e

ercv8c1e1#

您使用错误的FK约束定义了表。
您有:

CREATE TABLE Scenariste
(
    idScenariste  NUMBER GENERATED ALWAYS as IDENTITY (START WITH 1 INCREMENT BY 1) PRIMARY KEY,
    nom           VARCHAR2(100)  NOT NULL,
    idFilm        NUMBER         NOT NULL,
    CONSTRAINT fk_idScenariste
        FOREIGN KEY (idScenariste) REFERENCES Film (idFilm)
);

但我认为FK约束应该使用idFilm(而不是idScenariste列):

CONSTRAINT fk_idScenariste
        FOREIGN KEY (idFilm) REFERENCES Film (idFilm)

相关问题