Foreign Key

I have one table ROUTE_DETAILS with column ROUTE_NUMBER and ROUTE_NAME.

I have another table CUSTOMER_DETAILS with column CUST_CODE, CUST_NAME, ROUTE_NUMBER. Here route number is the foreign key of ROUTE_DETAILS.

ROUTE_DETAILS is parent table and CUSTOMER_DETAILS is child table.

Data in ROUTE_DETAILS:

ROUTE_NUMBER        ROUTE_NAME
RN0001            ROUTE1
RN0002            ROUTE2
RN                ROUTE3

Data in CUSTOMER_DETAILS:

CUST_CODE        CUST_NAME       ROUTE_NUMBER
CC0001         CUSTOMER1        RN0001
CC0002         CUSTOMER2        RN

Now the problem is when I am trying to update ROUTE_NUMBER from ROUTE_DETAILS or update ROUTE_NUMBER from CUSTOMER_DETAILS an error is shown: integrity constraints violation child record found

Query is:

update ROUTE_DETAILS set ROUTE_NUMBER = 'RN0003' where ROUTE_NUMBER = 'RN'

same thing happen when I am trying to update customer_details.


Yes, you cannot update the parent primary key if a child is referencing that key value.

Also you cannot update the child to have a foreign key value that does not reference a primary key value in the parent table.

So, you can either:

1)Relax the constraint temporarily while you make your changes, being sure to re-apply it afterwards.

Or

2)Delete the child row, update the parent row, then re-insert the child with the new foreign key value.

你可能感兴趣的:(Foreign Key)