Skip to Main Content
  • Questions
  • SELECT column from TABLE3, UPDATE column in TABLE2, INSERT all column in TABLE1

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Partheeban.

Asked: July 12, 2016 - 10:23 am UTC

Last updated: July 12, 2016 - 10:34 am UTC

Version: 11g

Viewed 1000+ times

You Asked

Hi

I need to solve some difficult logic process.

create table aa ( id int, name_child varchar2(25) );

create table bb ( id int, name_master varchar2(25) );

insert into bb values('-1', 'DUMMY');

bb is the master table, aa is the child table. name_child and name_master is foreign key relations for both the table.

create table cc ( id int, name varchar2(25) );

insert into cc values ( 1, 'ram' );
insert into cc values ( 2, 'sita' );
insert into cc values ( 3, 'luxmn' );

Now my question is I need to insert the table cc 's all values in to table aa. and do not want to insert into table bb. But in Oracle which are all the data presence in the table bb that data only can insert into table aa.

my logic is table cc's first value 'ram' need to update in table bb, after updating this table bb. 'ram' value can able to insert into table aa. and the same table cc's second value 'sita' need to update in table bb, after updating this table bb. 'sita' value can able to insert into table aa and same for third value too. this logic need to write in plsql proc

Example:
loop 1 .. 3
update bb set name_master=cc.name where id='-1'; -- only one record need to updated here
commit;

insert into aa select * from cc where id=cc.id; -- only one record need to insert here
commit;


finally my first two tables data will be like below format

select * from table aa;
id name_child
== ==========
1 ram
2 sita
3 luxmn

select * from table bb;
id name_child
== ==========
-1 luxmn

update bb set name_child='DUMMY' where id = '-1';
select * from table bb;
id name_child
== ==========
-1 DUMMY

Thanks
Partheeban.J


and Chris said...

I don't understand how table BB comes into this at all?

Why can't you just insert CC's values directly into AA?

create table aa ( id int, name_child varchar2(25) );
create table bb ( id int, name_master varchar2(25) );
create table cc ( id int, name varchar2(25) );

insert into bb values('-1', 'DUMMY');

insert into cc values ( 1, 'ram' );
insert into cc values ( 2, 'sita' );
insert into cc values ( 3, 'luxmn' );

insert into aa
  select * from cc;
 
select * from aa;

        ID NAME_CHILD              
---------- -------------------------
         1 ram                      
         2 sita                     
         3 luxmn

select * from bb;

        ID NAME_MASTER             
---------- -------------------------
        -1 DUMMY 


If you have a foreign key from AA -> BB, then all the values in AA must exist in BB. You can't update the value in BB, insert it into AA, then change BB's value. They have to be the same!

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Thanks You

Partheeban J, July 26, 2016 - 7:27 am UTC

Thanks the logic which i missed about primary key and foreign Key relationships.