ORA-02291: integrity constraint (PRUEBA4.AUTO_MARCA_FK) violated - parent key not found

302 views Asked by At

I'm learning about this recently and I wanted to know if I could help myself, here is my Database:

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

Insert Values

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: integrity constraint violated - parent key not found

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

Throws the following exception

ORA-02291: integrity constraint (PRUEBA4.AUTO_MARCA_FK) violated - parent key not found.

1

There are 1 answers

0
Derezed On
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)
);

Insert Values

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