Delete child records before I can delete the parent

Hello,I'm a newbie to SQL and if it is not to difficult I can succeed to write querie's. I don't much know about PL/SQL.
We are working with Oracle.The parent ERD is this.
Table 1 --> linking Table --> Table 2 --> Table 3(detail)
1 | 1 1 1 1 1 n
|--> Table 1 detail
1
Now I want to delete for every record of table 1 who doesn't have a record in the table 1 detail.
But before I can do this:
- I have to delete the one or more records in table 3,
- then in table 2 and then the records in the linking table
- and at last the records from Table 1, which is the parent.
Can anybody help or put me in a direction how to solve this?
Cascade doesn't work I think.
I mean by 'Linking table' a table with only three UID's(Table 1, Table 3 and his self)
I mean by the numbers the relation between the tables.
I'm working with Toad
Thanks in advance,
Nico

if I am understanding you correctly you have 3 tables kind of like this
create table  table1 as (select 1 t1_id from dual union
                      select 2 from dual union
                      select 3 from dual);
create table      table2 as (select 1 t2_id, 1 t1_id from dual union
                      select 2 t2_id, 1 t1_id from dual union
                      select 3 t2_id, 2 t1_id from dual union
                      select 3 t2_id, 2 t1_id from dual);
create table  table3  as (select 1 t3_id, 1 t2_id from dual union
                      select 2 t3_id, 1 t2_id from dual union
                      select 3 t3_id, 2 t2_id from dual);
select t1_id, t2_id, t3_id
from  table1 left outer join table2 using (t1_id) left outer join table3 using (t2_id)
order by t1_id;
T1_ID     T2_ID     T3_ID
1     1     1
1     1     2
1     2     3
2     3     
3          so you would like to delete table 1 t1_id 2 and 3 because there are no corresponding entris in table 3 (the detail table)
delete from table1 where t1_id in
  select t1_id
from  table1 left outer join table2 using (t1_id) left outer join table3 using (t2_id)
where t3_id is null
select * from table1;
t1_id
1of course that leaves you with orphans so you will need to clean up table 2
delete from table2 where  t1_id not in
(select t1_id from table1);why won't a cascade delete work?
Edited by: pollywog on Nov 18, 2010 12:03 PM

Similar Messages

  • I'm unable to delete a message. When I select edit and hit the red ( | ) button it reverts back to (-) before I can delete the message.

    I'm running IOS 4.3.3(8J2) on an Iphone 4. When I try to delete a message by editing and then selecting the red (|) before I can delete it the delete button goes away and reverts back to the . I also noticed since this has started that my battery drains quite quickly now. Is this a bug in the current IOS release?

    I'm having the EXACT same problem!  I look forward to getting an answer since no one seems to know why this is happening.

  • Deleting Child Records

    I'm a bit confused on how to accomplish the deletion of child records in a master/detail relationship.
    I have a master table and a child table. I created a foreign link between the tables. On my detail table, I have a Delete button, which I would like to use for removing child records. I understand for the master table, I will need to enable cascade delete to remove the master and all children records.
    Whenever I try to delete a child record I get the following error, integrity constraint - violated child record found. I understand the error, but how can I remove a single child record without deleting the master record. For example, I may have a 1 master record and 20 child records. I only want to remove one of the child records. The deletion seems to work fine when using AppModule or SQL Developer. Thanks.

    Thanks Surendrams. Your question made me take another closer look. Apparently, I was calling the Delete operation from the master table. I added another Delete operation in the Bindings from my child table and the deletion is now working. I should have caught this the first time. Thanks.

  • Delete child record

    hi is there a quikestt way i can delete child record of the parent table,lets say i have 100 record in child record related to one parent how can i delete the 100 record in smart way not by deleting one by one vale

    You coluld find it easily by googling..
    create table test_dept(dept_id number primary key,dep_name varchar2(50));
    create table test_emp(emp_id number primary key,
          dept_id number references test_dept on delete cascade,emp_name varchar2(50));

  • DELETING child record of same table..  qry  required( complicated)

    hai
    how to delete the child record of same table...?
    Here is the example...
    CREATE TABLE TDA
    (     PKNODAVE VARCHAR2(7 BYTE) NOT NULL ENABLE,
         DTCREATION DATE,
         DAVETRANSFERT VARCHAR2(7 BYTE),
         PKPARC NUMBER(5,0)
    ALTER TABLE TDA ADD CONSTRAINT PK_TDAV8 PRIMARY KEY (PKNODAVE)
    ALTER TABLE TDA ADD CONSTRAINT FK_TDA1 FOREIGN KEY (DAVETRANSFERT)
         REFERENCES TDA (PKNODAVE) ENABLE
    REM INSERTING into TDA
    Insert into TDA (PKNODAVE,DTCREATION,DAVETRANSFERT,PKPARC) values ('1',to_date('05-JAN-10','DD-MON-RR'),'1',10);
    Insert into TDA (PKNODAVE,DTCREATION,DAVETRANSFERT,PKPARC) values ('2',to_date('05-JAN-10','DD-MON-RR'),'2',10);
    Insert into TDA (PKNODAVE,DTCREATION,DAVETRANSFERT,PKPARC) values ('3',to_date('05-JAN-10','DD-MON-RR'),'3',10);
    Insert into TDA (PKNODAVE,DTCREATION,DAVETRANSFERT,PKPARC) values ('4',to_date('05-JAN-10','DD-MON-RR'),null,10);
    Insert into TDA (PKNODAVE,DTCREATION,DAVETRANSFERT,PKPARC) values ('5',to_date('05-JAN-10','DD-MON-RR'),'4',14);
    Insert into TDA (PKNODAVE,DTCREATION,DAVETRANSFERT,PKPARC) values ('6',to_date('05-JAN-10','DD-MON-RR'),'5',15);
    DELETE FROM TDA WHERE davetransfert IN ( SELECT PKNODAVE FROM TDA WHERE PKPARC='10')
    ERROR: FK_TDA1 CHILD RECORD FOUND...
    Pls give me the suggestions to solve this
    S

    You could try with a recursive procedure like this
    Processing ...
    select *
    from TDA
    Query finished, retrieving results...
    PKNODAVE      DTCREATION    DAVETRANSFERT   PKPARC  
    1                   05/01/10 1                     10
    2                   05/01/10 2                     10
    3                   05/01/10 3                     10
    4                   05/01/10                       10
    5                   05/01/10 4                     14
    6                   05/01/10 5                     15
    6 row(s) retrieved
    Processing ...
    declare
         cursor c is
              SELECT PKNODAVE FROM TDA WHERE PKPARC='10';
         procedure tda_cascade_delete(
                   del_PKNODAVE in varchar2
         as
              cursor sons is
                   select PKNODAVE
                   from TDA
                   where DAVETRANSFERT = del_PKNODAVE
                        and PKNODAVE <> DAVETRANSFERT;
         begin
              for x in sons loop
                   tda_cascade_delete(x.PKNODAVE);
              end loop;
              delete TDA
              where PKNODAVE = del_PKNODAVE;
         end;
    begin
         for x in c loop
              tda_cascade_delete(x.PKNODAVE);
         end loop;
    end;
    Processing ...
    select *
    from TDA
    Query finished, retrieving results...
    PKNODAVE      DTCREATION    DAVETRANSFERT   PKPARC  
    0 row(s) retrievedPS. A connect by subquery of the type
    delete tab
    where pk in (
              select fk
              from tab
              start with <my condition>
                   connect by fk = prior pk
         ) or <my condition>l;wouldn't generally work because it could try to delete a record before its children.
    Bye Alessandro

  • Does not delete child records - ADF-BC

    I am using ADF-BC to display data from three tables.
    I have a main view and a two view links to the children. when I try to delete the master record it says - Integrity constraint violated - child record found. And one more thing even though it does not delete the child records it deletes the master record from the view (only form the view, not from the database).
    How can I make it delete the child record first.
    Thanks

    I did the while exercise one more time and it started working. thanks.
    Now I have another problem. I have to keep that commit button on the page. If I delete the comit button from the page it gives me nullpointer exception at the line
    operationBinding = bindings.getOperationBinding("Commit");
    How can I solve this
    Thanks

  • Deleting Child records which are not associated to any parent

    Hi
    We have seen that in the application though the Activty records has been deleted the child records like sample dropped and the roduct details has remained in the system. This happened cos in R18 when contact was deleted activty got deleted but the Activity child records remained in the application.
    we found that child records are present by looking into record utilization.
    Wanted to know if there is any way to delete these child records from teh application cos we are unable to query for them from UI as well as via webservices.
    any pointers would be helpful.
    -MR

    There is not a reporting subject area or folder available on Books. The workaround we employed was to create a field on the entity that was assigned books and that field was checked when the books were assigned. We are using web services to assign books and the web services also updates the field "Books Assigned". This gives us a way of reporting on that field to see those records without book assigned.

  • How do I do this to use OVerdrive media on deviceThe Apple device must be formatted for use with Microsoft Windows.  The iTunes setting 'Manually manage music-' must be enabled for the device before you can complete the transfer.

    I cannot make these directions work
    I downloaded media on Overdrive MEdia on my PC
    I have the overdrive media ap on my I pod touch 4g
    this media is suppossed to be compatible w/I pd touch
    Notes on Transferring OverDrive MP3 Audiobooks…
    Most MP3 capable devices should play OverDrive MP3 Audiobooks.
    If you intend to transfer OverDrive MP3 Audiobooks to an Apple® device, note the following…
    iTunes® v9.0.2 (or newer) is required.
    The Apple device must be formatted for use with Microsoft® Windows®.
    The iTunes setting 'Manually manage music…' must be enabled for the device before you can complete the transfer. Adjust this setting in iTunes as follows…
    Connect the iPod® to your computer.
    If it does not launch automatically, open iTunes v9.0.2 (or newer).
    In iTunes, locate the device in the left vertical navigation panel (under heading 'DEVICES'), and click the device.
    The 'Summary' screen is displayed. 
    Place a checkmark next to 'Manually manage music…'.
    Click the 'Apply' button.
    The iTunes 'Summary' screen refreshes, and the changes are saved.
    If desired, close iTunes.
    Note that if an Apple device is connected to your computer, you can choose to simultaneously transfer a title to the iTunes Library and the Apple device. If you wish to only transfer a title to the iTunes Library, you must first disconnect the Apple device

    Recovering your iTunes library from your iPod or iOS device: Apple Support Communities
    Also you said " I want to add them to my iCloud, and also back to my computer.   " Note that unless  subscribe to iTunes match, only iTunes purchases are stored in iCloud.
    Also,
    You can redownload most iTunes purchases by:
      Downloading past purchases from the App Store, iBookstore, and iTunes Store

  • My iphone 5 screen needs to be turned on and off twice before I can slide the screen to unlock it.

    My iphone 5 screen needs to be turned on and off twice (hit the top button or home button, then turn off the screen again, then hit one of the buttons again) before I can slide the screen to unlock it. Also, when I was using it yesterday, the volume on my music was sliding up and down by itself, it clicked on albums by itself and wouldn't register my touch, the song kept pausing and playing by itself, and when I backed out of the app, all my running apps started sliding back and forth across the screen as if I were running my finger back and forth extremely fast. It also then opened the phone app, started to call one of my contacts, and wouldn't let me hang up. My phone is relatively new (got it back in June) and I haven't dropped my phone at all, haven't gotten it wet, etc... The only thing I've done to it recently is install iOS7. Does anyone know why this is happening and how I can fix it?

    Took my phone to the Apple store this week and they told me it was a hardware/screen malfunction. They replaced the screen for free since it was under warranty. Now it works fine again!

  • TS3274 I have thousands of email messages in my ipad mailbox that I can't delete en masse. I can delete a few at a time, but this would take forever???

    I have thousands of email messages in my ipad mailbox that I can't delete en masse. I can delete a few at a time, but this would take forever???

    Contact htem? No, I wouldn't think so, I'm not familier with fuse.net. Most ISPs adn email providers have web pages that can be used to acces your account settings and email. That may provide a means of selecting multiple emails and deleting them (but probably not 22,000 at a time).

  • When i try to purchase an inapp download it tells me i must purchase the app that this item is for before i can purchase the item.

    when i try to purchase an inapp download it tells me i must purchase the app that this item is for before i can purchase the item. I already own the app. Zen Pinball  im trying to buy add on tables.

    Me to, I spent 50 dollars to buy this and at first it said I didn't have enough money
    Credit, no saying I have to buy the app when I already have it....in getting tired of crap like this. Thinking about no buying apple products anymore.

  • After login but before I can access the internet I get a request for my login password so that - aosnotifyd - can access my login key chain. How can I get it to remember my password?

    After login but before I can access the internet I get a request for my login password so that - aosnotifyd - can access my login key chain. How can I get it to remember my password?

    Rephrase your question. Do you mean you keep getting login key chain errors?

  • Can I get a download of 10.8 to install before I can install the update 10.9.5

    Can I get a download of 10.9 to install before I can install the update 10.9.5.  I purchased a Mac Mini with Yosemite upon it and want to get back to Mavericks but when I try to download SL I can't because I get the message this Mini will not run SL.  Will it run 10.9?
    Thanks

    All the info on the jump drive is the stuff I was working with before the iMac went south (it's old, early 2009) and I cannot find some of the software to open some of the files.  The software on the older iMac needs some of the apps and Yosemite will not open them.  AM I still out of luck?

  • HT1212 My iPhone is disabled and I'm prompted in iTunes to turn off find my iPhone on the device before I can follow the steps to enable??? Please help.

    Find my iPhone is activated on my device. My device is locked. iTunes is prompting me to access my iPhone even though Im locked out of it to disable find my iphone before I can use the backup???
    Can anyone help me with this?
    Wonder if this has anything to do with iOS7 installation?

    iOS: Forgotten passcode or device disabled after entering wrong passcode

  • I use Nik soft ware plugins the HDR Efex Pro2  when i save the photo to aperture i have to close  aperture and reopen it befor i can view the photo works fine after that

    I use Nik soft ware plugins the HDR Efex Pro2  when i save the photo to aperture i have to close  aperture and reopen it befor i can view the photo works fine after that. Any help would be great thanks

    I have the same problem with NIK Color Efex Pro 4

Maybe you are looking for

  • Show slideshow and 2nd monitor and and the desktop on 1st

    ON my Mac mini, Monitor 1 HDMI to local monitor, monitor 2 is VGA to projector. I've been through a lot of the options and can't see where i can disable the presenter view so that i can see and use the desktop. I want to be able to start a slideshow,

  • PDF file in mini ipad

    I want to email a pdf file to my mini ipad that includes upc barcodes. Now all I get is a pdf and barcode is in text format.  I downloaded a pdf reader and it still comes in a text format.   Any ideas.  We are using these in the field and would like

  • How to learn webdynpro's

    hi all i want learn webdynpro's.plz help thanks advance

  • Can JavaMail send mail to Lotus Notes?

    Can JavaMail send e-mail to Lotus Notes? If I can't, is there any alternative method? I need it for my J2EE project. Thanks!! Dan. :)

  • How to get notified when new comment  is left on blog?

    I'd like to be notified (preferrably by e-mail) when a new comment is left on my blog. Is that possible in iWeb '09?