delete data in table

  • Hi to all,

    I have to delete some rows from a table based on timestamp, but this table has foreign key relationship with other table.

    So before I can delete rows from this table, I need to delete related rows from other table.

    But this other table also has foreign key relationship with third table, and third table with 4th table.

    So basically I will need to delete the rows from 4th Table, before I can delete third table rows, and so on.

    What is the best way to achieve this.

    Thanks

  • First of all determine if you REALLY have to delete the rows from the referencing tables. Is that really what you want to do?

    That said, you have three options:

    1) Follow the FK chain and delete starting from the last table in the chain

    2) Change the FK definition(s) to include "ON DELETE CASCADE"

    3) Set NULL on the first table in the FK chain

    There's no "best" way to do this: there's the way that does things according to your business rules and many other ways that don't. Make sure you're doing it the way the business wants.

    Hope this helps

    Gianluca

    -- Gianluca Sartori

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

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