I have 2 tables. Days and Parameters . In the first table - id , days (int). In the second table - id , param (int), daysId (int). daysId - key from the first table. It is necessary to remove data from the first table, where days <90, but also also need to remove parameters from the second table that are bound to the deleted data by daysId . How can I link 2 tables in a delete query?

In my tables there are more columns, I gave a minimal example.

  • Do you have a DBMS? - Viktorov
  • one
    I did not work with the SCL server. It seems to me that you can configure FK with cascade deletion, and then it will be necessary in principle to delete only from 1 table - Viktorov
  • 3
    daysId - is the key from the first FK table available? Create and install ON DELETE CASCADE - it will go away by itself ... - Akina
  • one
    @NickProskuryakov I now mistakenly deleted the values ​​together for more than 90, less than 90, as a result, a little work for the week was erased: D - Fresto
  • one
    @NickProskuryakov in my opinion, such things should be decided by architecture. If the data has value without a master table, then ban the cascade deletion. And if at removal from the main table, they should be deleted as well, then cascade deletion just right. DBMS are specially created in order to work with data. Why not use them for this? (this is a rhetorical question). I understand your approach, I just do not agree. Have a nice day =) - Viktorov

1 answer 1

To cascade delete: pre-set the parameter ON DELETE CASCADE

 DELETE FROM Days WHERE days < 90; 

Deletion by two queries:

DELETE FROM Days d WHERE d.days < 90; DELETE FROM Parameters p WHERE p.daysId IN ( SELECT * FROM Days d WHERE d.days < 90);

  • @Nick Proskuryakov, I know, I clearly divided where the removal is! - Yaroslav