EDITOR: How to DELETE from multiple Tables

EDITOR: How to DELETE from multiple Tables

mdesignmdesign Posts: 72Questions: 17Answers: 0

Ist there a tutorial how can i idelete data from multiple tables.
so i got one table with calendar dates and the other one with sign ins to specific dates. when i delete a calendar-date editor should also delete the sign ins for this date, with the common id.

how would you do this ? thx

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 2,806Questions: 85Answers: 406
    Answer ✓

    if the id of "calendar-date" is the foreign key in "sign-ins" then I would simply make the database do the work.
    Just define a referential integrity (ri) rule: ON DELETE CASCADE

    This makes sure the database deletes dependent entries to avoid orphaned rows.

    Of course there are other ways to achieve this. But this is the best because you are using built-in database functionality instead of coding something manually.

    Here an example: A certain report type has multiple reports. So if the report type is deleted all of the dependent reports need to be deleted as well:

  • rf1234rf1234 Posts: 2,806Questions: 85Answers: 406
    edited June 2020

    ... and here is an alternative solution. Just found this old thread showing on how to delete child records manually on "postRemove" of the parent table.
    https://datatables.net/forums/discussion/48625

  • allanallan Posts: 61,667Questions: 1Answers: 10,096 Site admin

    Exactly what @rf1234 says :). ON DELETE CASCADE is the method I would suggest implementing.

    Allan

  • mdesignmdesign Posts: 72Questions: 17Answers: 0

    thx, i know about the method with foreign key, but will need an alternative in some cases.

This discussion has been closed.