How to delete rows in multiple tables with foreign constraint rows at a time in singe query

  • How to delete rows in multiple tables with foreign constraints at a time in singe query..?

    I have 4 tables namely Users,User_Organization,User_Status_Role,and User_Organization_Role.

    Users table have user_id as Primary Key.remaining tables have foreign Key Dependences.

    How to delete rows in Primary key and foreign key rows in all tables at a time...

  • For that you need to use ON DELETE CASCADE

    _______________________________________________________________
    To get quick answer follow this link:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • Just remember that ON DELETE CASCADE also allows mistaken manual deletes to spread the mistake to 4 other tables, as well. 😉

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply