ORA-02291: 违反完整性约束 (PRUEBA4.AUTO_MARCA_FK) - 未找到父键

Posted

技术标签:

【中文标题】ORA-02291: 违反完整性约束 (PRUEBA4.AUTO_MARCA_FK) - 未找到父键【英文标题】:ORA-02291: integrity constraint (PRUEBA4.AUTO_MARCA_FK) violated - parent key not found 【发布时间】:2018-05-23 21:27:48 【问题描述】:

我最近正在学习这个,我想知道我是否可以帮助自己, 这是我的数据库:

CREATE TABLE MARCA(
COD_MARCA NUMBER(3) CONSTRAINT MARCA_PK PRIMARY KEY,
NOMBRE VARCHAR2(20) CONSTRAINT NOTNULL_NOMBRE_MARCA NOT NULL
);

CREATE TABLE AUTO(
COD_AUTO NUMBER(3) CONSTRAINT AUTO_PK PRIMARY KEY,
MODELO VARCHAR2(20) CONSTRAINT NOTNULL_MODELO_AUTO NOT NULL,
AUTO_VERSION VARCHAR2(10) CONSTRAINT NOTNULL_VERSION_AUTO NOT NULL,
COD_MARCA NUMBER(3) CONSTRAINT NOTNULL_COD_MARCA_AUTO NOT NULL,
CONSTRAINT AUTO_MARCA_FK FOREIGN KEY (COD_MARCA) REFERENCES MARCA (COD_MARCA)
);

CREATE TABLE VENTA(
COD_VENTA NUMBER(3) CONSTRAINT VENTA_PK PRIMARY KEY,
COD_VENDEDOR VARCHAR2(4) CONSTRAINT NOTNULL_COD_VND_VNTA NOT NULL,
RUT VARCHAR2(10) CONSTRAINT NOTNULL_RUT_VNTA NOT NULL,
COD_AUTO NUMBER (3) CONSTRAINT NOTNULL_COD_AUTO_VNTA NOT NULL,
COD_COLOR NUMBER(3) CONSTRAINT NOTNULL_COD_COLOR_VNTA NOT NULL,
FECHA DATE CONSTRAINT NOTNULL_FECHA_VENTA NOT NULL,
VALOR NUMBER(6) CONSTRAINT NOTNULL_VALOR_VENTA NOT NULL,
CONSTRAINT VENTA_VENDEDOR_FK FOREIGN KEY (COD_VENDEDOR) REFERENCES VENDEDOR (COD_VENDEDOR),
CONSTRAINT VENTA_CLIENTE_FK FOREIGN KEY (RUT) REFERENCES CLIENTE (RUT),
CONSTRAINT VENTA_AUTO_FK FOREIGN KEY (COD_AUTO) REFERENCES AUTO (COD_AUTO),
CONSTRAINT VENTA_COLOR_FK FOREIGN KEY (COD_COLOR) REFERENCES COLOR (COD_COLOR)
);

插入值

CREATE SEQUENCE SEQ_AUTO
INCREMENT BY 1
START WITH 1;

INSERT INTO AUTO VALUES(SEQ_AUTO.NEXTVAL,'ibiza','16V',1);
INSERT INTO AUTO VALUES(SEQ_AUTO.NEXTVAL,'ibiza','tsi',1);
INSERT INTO AUTO VALUES(SEQ_AUTO.NEXTVAL,'ibiza','tdi',1);

ORA-02291:违反完整性约束 - 未找到父密钥

INSERT INTO AUTO VALUES(SEQ_AUTO.NEXTVAL,'ibiza','16V',1)

抛出以下异常

ORA-02291:违反完整性约束 (PRUEBA4.AUTO_MARCA_FK) - 未找到父密钥。

【问题讨论】:

您似乎没有先在marca 中插入一行...? 没错,首先是“MARCA”表中的数据 【参考方案1】:
CREATE TABLE MARCA(
COD_MARCA NUMBER(3) CONSTRAINT MARCA_PK PRIMARY KEY,
NOMBRE VARCHAR2(20) CONSTRAINT NOTNULL_NOMBRE_MARCA NOT NULL
);

CREATE TABLE AUTO(
COD_AUTO NUMBER(3) CONSTRAINT AUTO_PK PRIMARY KEY,
MODELO VARCHAR2(20) CONSTRAINT NOTNULL_MODELO_AUTO NOT NULL,
AUTO_VERSION VARCHAR2(10) CONSTRAINT NOTNULL_VERSION_AUTO NOT NULL,
COD_MARCA NUMBER(3) CONSTRAINT NOTNULL_COD_MARCA_AUTO NOT NULL,
CONSTRAINT AUTO_MARCA_FK FOREIGN KEY (COD_MARCA) REFERENCES MARCA (COD_MARCA)
);

CREATE TABLE VENTA(
COD_VENTA NUMBER(3) CONSTRAINT VENTA_PK PRIMARY KEY,
COD_VENDEDOR VARCHAR2(4) CONSTRAINT NOTNULL_COD_VND_VNTA NOT NULL,
RUT VARCHAR2(10) CONSTRAINT NOTNULL_RUT_VNTA NOT NULL,
COD_AUTO NUMBER (3) CONSTRAINT NOTNULL_COD_AUTO_VNTA NOT NULL,
COD_COLOR NUMBER(3) CONSTRAINT NOTNULL_COD_COLOR_VNTA NOT NULL,
FECHA DATE CONSTRAINT NOTNULL_FECHA_VENTA NOT NULL,
VALOR NUMBER(6) CONSTRAINT NOTNULL_VALOR_VENTA NOT NULL,
CONSTRAINT VENTA_VENDEDOR_FK FOREIGN KEY (COD_VENDEDOR) REFERENCES VENDEDOR (COD_VENDEDOR),
CONSTRAINT VENTA_CLIENTE_FK FOREIGN KEY (RUT) REFERENCES CLIENTE (RUT),
CONSTRAINT VENTA_AUTO_FK FOREIGN KEY (COD_AUTO) REFERENCES AUTO (COD_AUTO),
CONSTRAINT VENTA_COLOR_FK FOREIGN KEY (COD_COLOR) REFERENCES COLOR (COD_COLOR)
);

插入值

CREATE SEQUENCE SEQ_MARCA
INCREMENT BY 1
START WITH 1;

INSERT INTO MARCA VALUES(SEQ_MARCA.NEXTVAL,'Seat');
INSERT INTO MARCA VALUES(SEQ_MARCA.NEXTVAL,'Renault');

【讨论】:

以上是关于ORA-02291: 违反完整性约束 (PRUEBA4.AUTO_MARCA_FK) - 未找到父键的主要内容,如果未能解决你的问题,请参考以下文章

ORA-02291: 违反完整性约束 (PRUEBA4.AUTO_MARCA_FK) - 未找到父键

ORA-02291: 违反完整约束条件 (*) - 未找到父项关键字

ORA-02291: 违反完整性约束 (OPS$P2417335.VOD_FILM_CLASS_FK) - 未找到父密钥

ORA-02291: 违反完整约束条件 (*) - 未找到父项关键字

ORA-02291: 违反完整约束条件 - 未找到父项关键字

ORA-02291: 完整性约束;找不到父键错误