Home » SQL & PL/SQL » SQL & PL/SQL » How to update the foreign key table
How to update the foreign key table [message #610173] Tue, 18 March 2014 05:39 Go to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Hi all,


I want to update the foreign key table , But i am getting "ORA-02291: integrity constraint (APPS.FK_SUPPLIER_COMP) violated - parent key not found"

Is this value mandatory in the primary table(parent table)?

CREATE TABLE supplier
( supplier_id numeric(10) not null,
  supplier_name varchar2(50) not null,
  contact_name varchar2(50),
  CONSTRAINT supplier_pk PRIMARY KEY (supplier_id)
);

CREATE TABLE products
( product_id numeric(10) not null,
  supplier_id numeric(10),
  supplier_name varchar2(50),
  CONSTRAINT fk_supplier_comp
    FOREIGN KEY (supplier_id)
    REFERENCES supplier(supplier_id)
    ON DELETE SET NULL
);

insert into supplier values(10,'rajesh','m')

insert into supplier values(20,'rajesh','m')


insert into products values(100,10,'m')

insert into products values(200,20,'m')



select * from supplier


select * from products 

delete from supplier where SUPPLIER_ID=10


SQL> select * from products
  2  /

PRODUCT_ID SUPPLIER_ID SUPPLIER_NAME
---------- ----------- --------------------
       100             m
       200          20 m


update products
set SUPPLIER_ID=40
where PRODUCT_ID=100


"ORA-02291: integrity constraint (APPS.FK_SUPPLIER_COMP) violated - parent key not found"



I want to update the SUPPLIER_ID with different value.

[Updated on: Tue, 18 March 2014 05:40]

Report message to a moderator

Re: How to update the foreign key table [message #610174 is a reply to message #610173] Tue, 18 March 2014 05:42 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
No problem, as long as you first insert parent key 40 into the parent (supplier) table.
Re: How to update the foreign key table [message #610175 is a reply to message #610174] Tue, 18 March 2014 05:49 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Quote:
No problem, as long as you first insert parent key 40 into the parent (supplier) table.


I want to update after SUPPLIER_ID column is NULL in the products Table.Means i have SUPPLIER_ID=10 from supplier right? Mean SUPPLIER_ID=10 is got the NULL Value in the products Table right? I want to update this column(NULL Column )with any value and i don't want to insert into the parent table

SQL> select * from supplier
  2  /

SUPPLIER_ID SUPPLIER_NAME                                      CONTACT_NAME
----------- -------------------------------------------------- ---------------------
         20 siva                                               m

SQL> select * from products
  2  /

PRODUCT_ID SUPPLIER_ID SUPPLIER_NAME
---------- ----------- --------------------------------------------------
       100             m
       200          20 m


[Updated on: Tue, 18 March 2014 05:50]

Report message to a moderator

Re: How to update the foreign key table [message #610177 is a reply to message #610174] Tue, 18 March 2014 05:50 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
@mist - what do you think foreign keys do?
Re: How to update the foreign key table [message #610178 is a reply to message #610177] Tue, 18 March 2014 05:52 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Quote:
what do you think foreign keys do?

values in one table must also appear in another table and
Re: How to update the foreign key table [message #610179 is a reply to message #610178] Tue, 18 March 2014 05:56 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
If you know what they do then why are you asking this question?
Re: How to update the foreign key table [message #610180 is a reply to message #610179] Tue, 18 March 2014 05:58 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
See what's written under "Parent Key Modifications and Foreign Keys" in Data Integrity.
Re: How to update the foreign key table [message #610181 is a reply to message #610179] Tue, 18 March 2014 05:59 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Quote:
If you know what they do then why are you asking this question?

values in one table must also appear in another table

Yes you are right cookiemonster, But ,is this not possible to update the column with any one of the values right? if yes please let me know.

Thank you
Re: How to update the foreign key table [message #610184 is a reply to message #610181] Tue, 18 March 2014 06:07 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
The whole point of foreign keys is to make sure that values in the child table must match values in the parent.
Re: How to update the foreign key table [message #610187 is a reply to message #610184] Tue, 18 March 2014 06:13 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Quote:
The whole point of foreign keys is to make sure that values in the child table must match values in the parent.


Ok can i update the existing column of the child Table when i remove the ON DELETE SET NULL attribute.
Re: How to update the foreign key table [message #610188 is a reply to message #610184] Tue, 18 March 2014 06:16 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
cookiemonster wrote on Tue, 18 March 2014 11:07
The whole point of foreign keys is to make sure that values in the child table must match values in the parent.

Re: How to update the foreign key table [message #610189 is a reply to message #610188] Tue, 18 March 2014 06:17 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
The on delete clauses dictate what happens when you delete a row in the parent, nothing else
Re: How to update the foreign key table [message #610192 is a reply to message #610189] Tue, 18 March 2014 06:28 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Quote:
Ok can i update the existing column of the child Table when i remove the ON DELETE SET NULL attribute.


Quote:

cookiemonster wrote on Tue, 18 March 2014 11:07
The whole point of foreign keys is to make sure that values in the child table must match values in the parent.


create table xxc_sample(t1 number primary key);

create table xxc_sample1(t2 number references xxc_sample(t1));

insert into xxc_sample
values(1);

insert into xxc_sample1
values(1);

update xxc_sample
set t1 = 2
where t1 = 1;

xxc_sample
*
ORA-02292: integrity constraint (APPS.SYS_C001107136) violated - child record found


create or replace trigger xxc_update
after update of t1 on xxc_sample
for each row
begin
   update xxc_sample1
   set t2 = :new.t1
   where t2 = :old.t1;
end;


update xxc_sample
set t1= 2
where t1=1;

               
select * from xxc_sample

output T1: 2 
Re: How to update the foreign key table [message #610193 is a reply to message #610192] Tue, 18 March 2014 06:35 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
What's that got to do with your original problem?
Re: How to update the foreign key table [message #610195 is a reply to message #610193] Tue, 18 March 2014 06:38 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
I want to know the below column contains the FK right?

create table xxc_sample1(t2 number references xxc_sample(t1));


Then, possible for update the FK Column right?

This is issue only if i do any wrong on this please help me..
Re: How to update the foreign key table [message #610196 is a reply to message #610195] Tue, 18 March 2014 06:41 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
The fk column in the child can be set to any value that matches a value in the parent.
Re: How to update the foreign key table [message #610197 is a reply to message #610196] Tue, 18 March 2014 06:42 Go to previous message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Thank you cookiemonster.. Smile Smile
Previous Topic: ORA-06504: PL/SQL: Return types of Result Set variables or query do not match
Next Topic: Help with grouping documents
Goto Forum:
  


Current Time: Wed Apr 24 20:48:10 CDT 2024