Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / May 2005

Tip: Looking for answers? Try searching our database.

basic  sql question

Thread view: 
hilz - 25 May 2005 06:43 GMT
Hi all
I have a basic sql situation that is bugging me. I could not find the
answer anywhere that i looked, and i could not find a newsgroups
specialized in sql to ask this question.
If this is not the right place to ask, please be gentle! and point me in
the right direction.
Please note that this is a real situation i am running into, but i
simplified it to this example. So this is not a homework (if you were
wondering!)

here we go...
i have four tables T1, T2, T3 and T4 related to each other in a one to
many relation (one in T1 to many in T2. one in T2 to many in T3, and one
 in T3 to many in T4).

how would i delete all rows in T4 that are related (or trace back) to a
specific record in T1.

let's assume this structure for simplicity:

TABLE T1
ID    (PK)

TABLE T2
ID    (PK)
T1_ID (FK)

TABLE T3
ID    (PK)
T2_ID (FK)

TABLE T4
ID    (PK)
T3_ID (FK)

so now i want to delete all rows in table T4 that trace back to a
specific row in T1.

Thanks for any help.
hilz.
Arnaud Berger - 25 May 2005 07:06 GMT
Hi,

Not sure this may work, but give it a try :

delete from T4 where id in
( select id from T4 where T3_ID in
   ((select id from T3 where T2_ID in
       (select id from T2 where T1_ID="yourId"))))

Regards,

Arnaud

> Hi all
> I have a basic sql situation that is bugging me. I could not find the
[quoted text clipped - 36 lines]
> Thanks for any help.
> hilz.
Bjorn Abelli - 25 May 2005 10:40 GMT
"Arnaud Berger" wrote...

>> i have four tables T1, T2, T3 and T4 related to each other
>> in a one to many relation (one in T1 to many in T2. one in
>> T2 to many in T3, and one in T3 to many in T4).
>>
>> how would i delete all rows in T4 that are related
>> (or trace back) to a specific record in T1.

> Not sure this may work, but give it a try :
>
> delete from T4 where id in
> ( select id from T4 where T3_ID in
>    ((select id from T3 where T2_ID in
>        (select id from T2 where T1_ID="yourId"))))

Not necessary with that many levels in the subqueries...

 delete from T4
 where T3_ID in
 ( select T3.ID
   from   T3, T2
   where  T3.T2_ID = T2.ID
   and    T2.T1_ID = ? )

I've put a question mark where you insert the id for T1 as a parameter in a
PreparedStatement.

// Bjorn A
hilz - 25 May 2005 14:25 GMT
>>Not sure this may work, but give it a try :
>>
[quoted text clipped - 11 lines]
>     where  T3.T2_ID = T2.ID
>     and    T2.T1_ID = ? )

Thanks to you  Bjorn and Arnaud. That was pretty quick answer.
I like Bjorn's way since i might have more levels!

thank you very much
hilz
Arnaud Berger - 26 May 2005 07:28 GMT
Quite right, this looks a bit cleaner ;)

Regards,

Arnaud

> "Arnaud Berger" wrote...
>
[quoted text clipped - 25 lines]
>
> // Bjorn A


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.