Unable to add or update child row

I am trying to add entries to the two tables below,

CREATE TABLE customer (Custno CHAR(3), Custname VARCHAR(25) NOT NULL, Custstreet VARCHAR(30) NOT NULL, Custcity VARCHAR(15) NOT NULL, Custprov VARCHAR(3) NOT NULL, Custpcode VARCHAR(6) NOT NULL, Disc DECIMAL(3,1), Balance DECIMAL(7,2), Credlimit DECIMAL(5), Srepno CHAR(3), CONSTRAINT pkcustno PRIMARY KEY (Custno), CONSTRAINT fksrepno FOREIGN KEY (Srepno) REFERENCES salesrep(Srepno) ); CREATE TABLE orders (Orderno CHAR(5) UNIQUE NOT NULL, Orderdate DATE, Custno CHAR(3) NOT NULL, CONSTRAINT fkordercust FOREIGN KEY (Custno) REFERENCES customer (Custno) ); 

When adding like this,

 INSERT INTO orders(Orderno, Orderdate, Custno) VALUES('14587','2011-11-09', '125' ); INSERT INTO orders(Orderno, Orderdate, Custno) VALUES('11547','2011-11-07', '125' ); 

I get: I can not add or update the child row: the foreign key constraint fails ( sh . Orders, CONSTRAINT fkordercust FOREIGN KEY ( Custno ) LINKS customer ( Custno )) "Is something wrong in the table?

+4
source share
5 answers

You do not have a client with CustNo = '125' . Because of this, the Foreign key fails. You are trying to place an order for a nonexistent client, DB gives an error.

+7
source

Do you actually have a client line for client number 125? I think no. The error message tells you exactly what is wrong.

Foreign key violation, which guarantees that no orders can be created for non-existent customers, is violated:

 CONSTRAINT fkordercust FOREIGN KEY (Custno) REFERENCES customer (Custno) 

First create a customer line, then you can add order lines for that customer to your hearty content.

+3
source

Your table is fine, you just don't have a client with CustNo from '125' in the database.

+3
source

You created a foreign key for the client table, but (apparently) did not insert any data into it.

+3
source

Do you have a customer with number 125?

+1
source

Source: https://habr.com/ru/post/1382820/


All Articles