If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > how to duplicate a row where tables are linked

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-22-09, 19:18
anilp anilp is offline
Registered User
 
Join Date: Sep 2009
Posts: 9
Question how to duplicate a row where tables are linked

I am using mssql but I think this is generic.
how to duplicate a row where the table could have dependencies?
ie. table B has a foreign key into tables A, B and C. table A is not a dependent table.
thanks,
Anil
Reply With Quote
  #2 (permalink)  
Old 09-22-09, 19:58
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,085
which row do you want to duplicate?

and in which table is this row?

and does this table have a primary key?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 09-23-09, 07:00
dportas dportas is offline
Registered User
 
Join Date: Dec 2007
Location: London, UK
Posts: 732
Quote:
Originally Posted by anilp
I am using mssql but I think this is generic.
how to duplicate a row where the table could have dependencies?
ie. table B has a foreign key into tables A, B and C. table A is not a dependent table.
thanks,
Anil
Your table ought to have a key which would prevent duplicate rows. I suggest you rethink or restate your requirement to be clear about what key(s) are involved and exactly what you want to duplicate.
Reply With Quote
  #4 (permalink)  
Old 09-23-09, 12:05
anilp anilp is offline
Registered User
 
Join Date: Sep 2009
Posts: 9
Quote:
Originally Posted by r937
which row do you want to duplicate?

and in which table is this row?

and does this table have a primary key?
Sorry for not specifying more clearly.
the row is in table B. All the tables have primary keys.
by 'duplicate', I mean cloning the table row and inserting the clone in the table with another primary key, of course.
Someone proposed a solution but that involved add ing a 'parent-id' column to each table. A solution seems difficult because the foreign keys are not null. So if there are 15 tables, I dont know how.
Reply With Quote
  #5 (permalink)  
Old 09-23-09, 12:05
anilp anilp is offline
Registered User
 
Join Date: Sep 2009
Posts: 9
Quote:
Originally Posted by dportas
Your table ought to have a key which would prevent duplicate rows. I suggest you rethink or restate your requirement to be clear about what key(s) are involved and exactly what you want to duplicate.
Sorry for not specifying more clearly. Please see above.
Reply With Quote
  #6 (permalink)  
Old 09-23-09, 12:18
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,002
Can you explain your real world scenario rather than "A" and "B"? I find it rather confusing to follow to be honest!
__________________
George
Twitter | Blog
Reply With Quote
  #7 (permalink)  
Old 09-23-09, 12:30
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,085
Quote:
Originally Posted by anilp
by 'duplicate', I mean cloning the table row and inserting the clone in the table with another primary key, of course.
of course


Quote:
Originally Posted by anilp
Someone proposed a solution but that involved add ing a 'parent-id' column to each table.
that someone is misguided, to say the least

alternatively, it's possible that this someone knew more about your tables than we do
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 09-23-09, 14:59
anilp anilp is offline
Registered User
 
Join Date: Sep 2009
Posts: 9
Quote:
Originally Posted by gvee
Can you explain your real world scenario rather than "A" and "B"? I find it rather confusing to follow to be honest!
Suppose a natural disaster (flood, tornado, fires...) strikes, and farmer Brown's crops, fences, soil, buildings are damaged. So he files an application requesting financial aid to help him repair the damage. He lists the project line items or techniques (building fence, reseeding crops etc.) and gets approval for each and a percentage of his request is paid for.

So looking at a subset:

main tables:
1) aid_request
primary key: aid_request_id

2) farmer
primary key: farmer_id


linking tables:

3) project_item
primary key: project_item_id
foreign keys (not null): aid_request_id

4) farmer_project_item_estimate
primary key: farmer_project_item_estimate_id
foreign keys (not null): farmer_id, project_item_id, aid_request_id

5) farmer_aid_request
primary key: farmer_aid_request_id
foreign keys (not null): aid_request_id, farmer_id

Suppose the aid application is to be versioned, then the new version of the application is created by cloning the initial application, so a copy of the row(s) in the original table is created and inserted there.

We can create an ordering of the tables to be processed. We start with the independent tables first.
To clone the row(s) related to the aid request in farmer_project_item_estimate, how would we do so?
We would create the three tables it depends upon, and then use the ids farmer_id, project_item_id, aid_request_id to insert a row in this table, since the foreign keys are not null.
Is there a general way or an algorithm to do this? would I have to create a dependency graph - but what if there is a 1:N relationship (so multiple rows are associated as in project_item). I am wondering how one can do it in SQL, or SQL and Java?

Last edited by anilp; 09-23-09 at 17:22.
Reply With Quote
  #9 (permalink)  
Old 09-23-09, 17:17
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,085
this isn't duplicating a row, this is duplicating a comprehensive set of related rows

you'll have to write some application logic to do that
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On