Is a delete with cascade faster then delete from 2 tables independently?

Hi there,
I have 2 tables, TableA and TableB. TableA's primary key is a foreign key of TableB.
My question is, Would it be faster to delete the associated rows from TableA and TableB independently?
Or is it faster to create TableB with the ON DELETE CASCADE clause?
thanks for the help.

Hi there,
I have 2 tables, TableA and TableB. TableA's primary key is a foreign key of TableB.
My question is, Would it be faster to delete the associated rows from TableA and TableB independently?
Or is it faster to create TableB with the ON DELETE CASCADE clause?
thanks for the help.

Similar Messages

  • Delete from two tables in one statement

    Hi,
    Is there a way to delete from two tables in one statement?
    Actually I have two tables:
    1. Base table (id, name, age)
    2. Person table (id, city, street)
    The id in both tables is identical.
    I would like to delete using something like a join:
    Delete from base, person where id=2;
    Thanks
    dyahav

    Hi,
    If you want to delete records both at a time them your table must use ON DELETE CASCADE. See the below example.
    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) not null,
    CONSTRAINT fk_supplier
    FOREIGN KEY (supplier_id)
    REFERENCES supplier(supplier_id)
    ON DELETE CASCADE
    In this example, we've created a primary key on the supplier table called supplier_pk. It consists of only one field - the supplier_id field. Then we've created a foreign key called fk_supplier on the products table that references the supplier table based on the supplier_id field.
    Because of the cascade delete, when a record in the supplier table is deleted, all records in the products table will also be deleted that have the same supplier_id value.
    Thank you.

  • Deleting from multiple tables

    I have a master table and a detail table. I need to delete a set of records from both the master and detail tables based on a criteria. Now if I delete from one table then I will not know which records I have to delete from the other table. So the records needs to be deleted from both the tables using the criteria. My SQL statement to select the records is
    <<
    select *
    FROM TL_RATE A, TL_RATE_DETAIL B
    WHERE A.CARRIER_ID = B.CARRIER_ID
    AND A.TARIFF_CLASS_ID = B.TARIFF_CLASS_ID
    AND A.LANE_ID = B.LANE_ID
    AND A.SERVICE_COM_ID = B.SERVICE_COM_ID
    AND A.EFFECTIVE = B.EFFECTIVE
    AND DATE_INVALID < SYSDATE-365;
    >>
    Thanks

    You don't show what table DATE_INVALID is in. However, you should just delete the matching rows from the OTHER table first then that table. This is very slightly different from the rows returned by your query since your query is an inner join. In other words, you're excluding rows that might exist in one table but not the other. Assuming DATE_INVALID is in TL_RATE ...
    delete tl_rate_detail
    where (CARRIER_ID, TARIFF_CLASS_ID, LANE_ID, SERVICE_COM_ID, EFFECTIVE) in (
      select CARRIER_ID, TARIFF_CLASS_ID, LANE_ID, SERVICE_COM_ID, EFFECTIVE
      from tl_rate
      where date_invalid < sysdate - 365)
    delete tl_rate
    where date_invalid < sysdate - 365
    /Richard

  • Deleting from multiple tables where few tables have same column name

    Hi,
    I am new to PL/SQL and need some help. I need to delete data older then X years from some 35 odd tables in my schema and out of those tables 25 tables have same column name on which i can have my "where" clause and rest 10 table have different table names. I am doing something like this :
    declare
    table_list UTL_FILE.FILE_TYPE;
    string_line VARCHAR2(1000);
    tables_count number:=0;
    table_name VARCHAR2(400);
    column_name VARCHAR2(400);
    BEGIN
    table_list := UTL_FILE.FOPEN('ORALOAD','test7.txt','R');
    DBMS_OUTPUT.PUT_LINE(table_list);
    LOOP
    UTL_FILE.GET_LINE(table_list,string_line);
    table_name := substr(string_line,1, instr(string_line,'|')-1);
    column_name := substr(string_line, instr(string_line,'|')+1);
    DBMS_OUTPUT.PUT_LINE(table_name);
    DBMS_OUTPUT.PUT_LINE(column_name);
    IF column_name = 'SUBMISSION_TIME' THEN
    delete from :table_name where to_date(:column_name)<(sysdate-(365*7));
    ELSE
    delete from || table_name || where ( || to_date(column_name) || ) <(sysdate-(365*7));
    END IF;
    END LOOP;
    EXCEPTION
    WHEN NO_DATA_FOUND THEN
    UTL_FILE.FCLOSE(table_list);
    DBMS_OUTPUT.PUT_LINE('Number of Tables processed is : ' || tables_count);
    UTL_FILE.FCLOSE(table_list);
    END;
    WHERE the text file "text7.txt" contains list of table name and column names separated by a pipe line. But when I execute the above proc it gives error "invalid table name".
    Can something like this be done or is there any other way to execute this task of deletion from 35 tables.
    Thanks.

    Thanks for replies. I don't know what I am doing wrong but still not getting this damn thing work...This is the proc i am running now :
    declare
    table_list UTL_FILE.FILE_TYPE;
    string_line VARCHAR2(1000);
    tables_count number:=0;
    table_name VARCHAR2(4000);
    column_name VARCHAR2(4000);
    code_text VARCHAR2(2000);
    BEGIN
    table_list := UTL_FILE.FOPEN('ORALOAD','test7.txt','R');
    LOOP
    UTL_FILE.GET_LINE(table_list,string_line);
    table_name := substr(string_line,1, instr(string_line,'|')-1);
    column_name := substr(string_line, instr(string_line,'|')+1);
    IF column_name = 'SUBMISSION_TIME' THEN
    DBMS_OUTPUT.PUT_LINE('do nothing');
    ELSE
    code_text:= 'begin delete from'|| (table_name) ||'where to_date' || (column_name) || '<(sysdate-(365*7))';
    Execute Immediate code_text;
    END IF;
    END LOOP;
    EXCEPTION
    WHEN NO_DATA_FOUND THEN
    UTL_FILE.FCLOSE(table_list);
    DBMS_OUTPUT.PUT_LINE('Number of Tables processed is : ' || tables_count);
    UTL_FILE.FCLOSE(table_list);
    END;
    But it gives following error :
    " ORA-06550: line 1, column 51:
    PL/SQL: ORA-00933: SQL command not properly ended
    ORA-06550: line 1, column 7:
    PL/SQL: SQL Statement ignored
    ORA-06550: line 1, column 68:
    PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
    ORA-06512: at line 22 "

  • WBS elements not deleted from PRPS table

    Hi Experts,
    In CJ20N transaction:
    When I delete a project with project profile (ZABC):
      - The project record is completely deleted from 'PROJ' table.
      - The WBS records are completely deleted from 'PRPS' table.
    When I delete another project with different project profile (ZXYZ):
      - The project record is completely deleted from 'PROJ' table.
      - The WBS records are NOT deleted from 'PRPS' table.
    What is the reason for not deleting records from PRPS table?
    Thanks in advance for your valuable answers.

    WBS should be having actuals.

  • Deletion from parent table

    Hi,
    Is it possible to delete parent records from parent table without deleting from child tables ?
    I can delete the records after disabling the constraints . But I couldn't enable the constraint on child table after deletion of parent records from parent table.
    Please Help.

    Two Alternatives
    1. What is the need of child records if no parent record ? No use keeping the child records without parent records.
    For e.g., If we have Invoice_Master containing the Distributor and total amount and Other Transport details and Invoice_Details containing the list of Items the Distributor has purchased. In this case if we delete the Invoice_Master then the Invoice_Details is meaning less.
    2. Another case If really need to maintain the Child records then create one dummy record in the Parent and link the Child records to the dummy Parent by changing the Foreign Key value to the dummy Parent record Primary Key Value.

  • Create Trigger to Delete from multiple tables

    Hello:
    I'm trying to write a trigger which will allow me to delete from multiple records. I have two tables where the record for the same client_id needs to be deleted.
    Is it possible to do this? I started writing some code and this is what I have so far:
    create or replace trigger app_t1
    before delete on <table1> ?? -
    for each row
    begin
    delete from client where clientid = :new.clientid;
    delete from key where pk = :newclientid;
    end;I'm stuck on the line where I have "before delete on" where I'm supposed to provide a table name. I can only use one table and I need to delete from two.
    This trigger is supposed to be used within APEX. In APEX, fields are designated as :P1_clientid where P1 references page 1 of the application. Yet, :P1_clientid is set to the field clientid in the table.
    So when I write my trigger, I'm not sure how I'm supposed to set my variables.
    Can someone help?
    I'm also going to post this into the APEX forum.
    Thanks.

    It's not clear to me if you are just trying to keep two tables in syn or whether you are trying to achieve something else.
    A couple of points though:
    - In delete database triggers the :new attributes are NULL. You probably mean to use the :old attributes.
    - Is there some relationship between the two tables? If so, setting the foreign key to CASCADE DELETE might do the trick for you.
    - Another option - better than a database trigger in my opinion - is to just code a procedure that deletes from both tables and call that one from APEX.
    Regards,
    Rob.

  • Condition Record PB00 gets deleted from A018 table

    Hi Gurus-
    Could anyone of you explain how the PB00 condition is getting deleted from A018 table (Purch Org Level Condition)? It looks like some batch job is randomly setting the Valid to Date A018-DATBI to yesterday's date and today there is no valid time dependent condition.
    I don't know any process/program that accesses PB00 condition in table A018 other than MEK1 or MEK4.
    Does SD condition type change has any impact in Purchasing Price (PB00). Although all the Rate/Price is stored in the same table KONP but separated by application area and table (A017, A018, A071, A073).
    Can anybody shed some lights on this please?

    Thanks for your reply. We know the user name who is making the change in A018. It is a batch user ID that makes the change but we have about 100 differentg background batch job that is scheduled everyday and I went through every one of them but did not raise any flag.
    And the worst part is since it is done by the batch user id (batch job), there is no change record (if I try to do MEK3-Environment>display changes) not even an entry in CDHDR table or CDPOS table.
    Trying to see if you guys know of any SD or MM program that accesses that table and set the valid to field to yesterdays date and create a new condition record (KNUMH) with valid to date 31.12.9999 and rate (value) to 0.00?
    Please let me know if you have any suggestion.
    Thanks in advance.

  • Could not delete from specified table?

    hi all,
    i'm getting this error could not delete from specified table when i execute a delete on dbf file via JDBC-ODBC bridge, when i execute an update table command i get operation must use an updatable query, but i'm not updating the query returned from select statement.
    Does anyone have similar problem before??

    Hi,
    my code is below:
    try {     
    conn=DriverManager.getConnectio("jdbc:odbc:sui","","");
    stmt = conn.createStatement();     
    int r= stmt.executeUpdate("Update Alarms set ACK=True WHERE DT = { d '" + msgdate + "' } AND TM= '" + msgtime + "'");
    System.out.println(r+" records updated in Alarms ");
    stmt.close();
    conn.close();
    stmt=null;
    conn=null;
    catch (NullPointerException e) {System.out.println(e.getMessage());}
    catch (SQLException e) {System.out.println(e.getMessage());}
    I have an ODBC datasource called sui and a table called alarms, i need to write into the column Ack to true when DT equals msgdate and TM equals msgtime, the error returned is Operation must use an updatable query.
    I believe the SQL has no problem because there is no SQL syntax error and i'm not updating any resultset returned from select query.
    When i delete, i get could not delete from specified table. Anyone has any idea wh'at's wrong?

  • Delete from internal table

    Hi,
    I want to delete from internal table some regords.
    I write code:
    delete  isrot where bldat < '01.09.2005'.
    it doesn't work, what is wrong?
    regards,
    Joanna

    hi,
    you write the statement like....
    <b>delete FROM isrot where bldat < '01.09.2005'.</b>
    now it will work...
    To select the lines that you want to delete using a condition, use the following:
    <b>DELETE FROM <target> WHERE <cond> .</b>
    All of the lines in the database table that satisfy the conditions in the WHERE clause are deleted. The FROM expression must occur between the keyword and the database table.
    You should take particular care when programming the WHERE clause to ensure that you do not delete the wrong lines. For example, if you specify an empty internal table in a dynamic WHERE clause, all of the lines in the table are deleted.
    If at least one line is deleted, the system sets SY-SUBRC to 0, otherwise to 4. SY-DBCNT contains the number of lines deleted.
    follow this link for more information on internal table operation.
    http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb3aef358411d1829f0000e829fbfe/content.htm
    regards,
    Ashok Reddy
    Message was edited by:
            Ashok Reddy

  • Once a row is deleted from a table ,how can i recover it back in the table?

    Hi
    I have made a delete program in Jdev. I had deleted some of the rows from table such that count goes 2 from 10.
    Now I want to use this table again for some other program,how should i restore the deleted rows so that i can use the table properly.
    However toad tool showing that no row is deleted from the table.
    Thanks & Regards

    Can you please elaborate how are you deleting rows from table.

  • Delete from two tables at the same time

    Hi,
    Is there way to delete from many tables at the same time ?
    delete from tbl1, tbl2;
    Thank you

    953402 wrote:
    Hi,
    Is there way to delete from many tables at the same time ?
    delete from tbl1, tbl2;
    Thank youNO
    Consider to actually Read The Fine Manual below
    http://docs.oracle.com/cd/E11882_01/server.112/e26088/toc.htm

  • Records have been deleted from the table.

    Hi all ,
    If records have been deleted from the table that any log file maintains the history as following Way.
    User Name who delete the records.
    Machine name where the command is execute.
    The command syntax e.g delete from abc where ……..
    or any other help related to mentioned problem.
    Regards,
    Mobeen.

    Wrong forum .. your question doesnt make much sense.
    But take a look at Oracle Auditing.

  • Dynamic record deletion from database table

    Hi,
    I need to delete selected records from database table(dynamic names). Table names are being passed from main program with some of their field names. The record to be deleted from the database table is being decided based on the fields passed for the table and their contains passed from the main program.
    It is not possible to write dynamic where clause for DELETE statement directly.
    So, I created a dynamic internal table and i am trying to fetch all records using SELECT statement(for which we can write dynamic where condition, something like...SELECT...WHERE (itab).  ) which need to be deleted in the iternal table.
    Piece of code :
              CONCATENATE c_im v_tablefield1 INTO v_imprtfield1.
              CONCATENATE v_tablefield1 c_in v_imprtfield1
                       into s_condition separated by space.
              APPEND s_condition TO t_condition.
              PERFORM GET_DYNAMIC_ITAB USING s_flds_agtab-tabname
                                    changing t_itab.
              ASSIGN t_itab->* TO <itab>.
    *Select the data (to be deleted) from the database table
               SELECT * FROM (s_flds_agtab-tabname) INTO TABLE <itab>
                 WHERE (t_condition).
    *Delete the records from the table
               IF SY-SUBRC = 0.
                 DELETE (s_flds_agtab-tabname) FROM TABLE <itab>.
               ENDIF.
    Here t_condition is of standard table of WHERETXT.
    t_condition at the run time before giving dump was:
    SPART IN IM_SPART
    AND KUNNR IN IM_KUNNR
    Here IM_SPART is renge type of SPART and IM_KUNNR is renge of KUNNR.
    I am getting a DUMP:
    The WHERE condition has an unexpected format.
    Error analysis                                                                               
    The current ABAP/4 program attempted to execute an ABAP/4 Open SQL
    statement containing a WHERE condition of the form WHERE (itab) or
    WHERE ... AND (itab). The part of the WHERE condition specified at
    runtime in the internal table itab contains the operator         
             IN (v1, ..., vn)                                        
    in incomplete form.                                              
    How to correct the error
    If the error occurred in a non-modified SAP program, you may be  
    able to find a solution in the SAP note system.                  
    If you have access to the note system yourself, use the following
    search criteria:                                                 
    "SAPSQL_IN_ILLEGAL_LIST"                               
    "SAPLZSD_TAB_REFRESH " or "LZSD_TAB_REFRESHU01 "       
    "Z_SD_REFRESH_AGTABLES"                                
    If you cannot solve the problem yourself, please send the
    following documents to SAP:                             
    I would like to know whether "IN" operator is allowed in (itab) of WHERE clause. While testing I changed the "IN" to "=" specifying a suitable value there. It worked. So please let me know if i can give "IN" operator using renge table in the dynamic where clause.
    Thanking you,
    Surya

    Hi again,  so if you can not use the IN in a dynamic where clause you might be forced to dynamically build the entire select statement,  Here is a sample program which may give you some ideas, notice that we are writing the select statement code, putting it in another program and generating the subroutine at runtime, then call this routine.  I'm sure that this will help you see what you need to do.
    report zrich_0003 .
    tables: kna1.
    types: t_source(72).
    data: routine(32) value 'DYNAMIC_SELECT',
                 program(8),
                 message(128),
                 line type i.
    data: isource type table of t_source,
                xsource type t_source.
    ranges:
            r_kunnr for kna1-kunnr.
    data: ikna1 type table of kna1.
    data: xkna1 type kna1.
    r_kunnr-sign = 'I'.
    r_kunnr-option = 'EQ'.
    r_kunnr-low    = '0001000500'.
    append r_kunnr.
    xsource = 'REPORT ZTEMP.'.
    insert xsource  into isource index 1.
    xsource = 'FORM dynamic_select'.
    insert xsource  into isource index 2.
    xsource = 'Tables r_kunnr ikna1.'.
    append xsource to isource.
    xsource = 'select * into table ikna1 from kna1'.
    append xsource to isource.
    xsource = 'where kunnr in r_kunnr.'.
    append xsource to isource.
    xsource = 'ENDFORM.'.
    append xsource to isource.
    generate subroutine pool isource name program
                             message message
                             line line.
    if sy-subrc = 0.
      perform (routine) in program (program) tables r_kunnr
                                                    ikna1.
    else.
      write:/ message.
    endif.
    loop at ikna1 into xkna1.
      write:/ xkna1-kunnr.
    endloop.
    Regards,
    Rich Heilman

  • Delete from multiple tables

    Hello guys,
    Can somebody please help me. I want to delete this one transaction from all the below tables in my testing environment. Can somebody please help me with a script to do that? So basically I want a delete statement for the same select statement I have below.
    select distinct b.*
    from ra_customer_trx_all a,
         ra_customer_trx_lines_all b,
         ra_cust_trx_line_salesreps_all c,
         ra_cust_trx_line_gl_dist_all d,
         ar_payment_schedules_all e
    where a.CUSTOMER_TRX_ID = b.CUSTOMER_TRX_ID
    and b.CUSTOMER_TRX_ID = c.CUSTOMER_TRX_ID
    and c.CUSTOMER_TRX_ID = d.CUSTOMER_TRX_ID
    and d.CUSTOMER_TRX_ID = e.CUSTOMER_TRX_ID
    and b.CUSTOMER_TRX_LINE_ID = d.CUSTOMER_TRX_LINE_ID
    and a.customer_trx_id = 1328 Thanking you in advance

    If you are checking to see if that ID exists in all 5 tables then
    DECLARE
      v_trx_id   ra_customer_trx_all.CUSTOMER_TRX_ID%TYPE;
    BEGIN
      SELECT DISTINCT b.CUSTOMER_TRX_ID
        INTO v_trx_id
        FROM ra_customer_trx_all A,
             ra_customer_trx_lines_all b,
             ra_cust_trx_line_salesreps_all c,
             ra_cust_trx_line_gl_dist_all d,
             ar_payment_schedules_all E
       WHERE A.CUSTOMER_TRX_ID = b.CUSTOMER_TRX_ID
         AND b.CUSTOMER_TRX_ID = c.CUSTOMER_TRX_ID
         AND c.CUSTOMER_TRX_ID = d.CUSTOMER_TRX_ID
         AND d.CUSTOMER_TRX_ID = E.CUSTOMER_TRX_ID
         AND b.CUSTOMER_TRX_LINE_ID = d.CUSTOMER_TRX_LINE_ID
         AND A.customer_trx_id = 1328;
      IF v_trx_id IS NOT NULL
      THEN
        DELETE FROM ra_customer_trx_all
         WHERE CUSTOMER_TRX_ID = v_trx_id;
        DELETE FROM ra_customer_trx_lines_all
         WHERE CUSTOMER_TRX_ID = v_trx_id;
        DELETE FROM ra_cust_trx_line_salesreps_all
         WHERE CUSTOMER_TRX_ID = v_trx_id;
        DELETE FROM ra_cust_trx_line_gl_dist_all
         WHERE CUSTOMER_TRX_ID = v_trx_id;
        DELETE FROM ar_payment_schedules_all
         WHERE CUSTOMER_TRX_ID = v_trx_id;
      END IF;
    END;Other wise do this simply.
    DELETE FROM ra_customer_trx_all
    WHERE CUSTOMER_TRX_ID = 1328;
    DELETE FROM ra_customer_trx_lines_all
    WHERE CUSTOMER_TRX_ID = 1328;
    DELETE FROM ra_cust_trx_line_salesreps_all
    WHERE CUSTOMER_TRX_ID = 1328;
    DELETE FROM ra_cust_trx_line_gl_dist_all
    WHERE CUSTOMER_TRX_ID = 1328;
    DELETE FROM ar_payment_schedules_all
    WHERE CUSTOMER_TRX_ID = 1328;G.
    Edited by: Ganesh Srivatsav on Apr 1, 2011 12:34 PM

Maybe you are looking for