I’ve searched quite a while but I did not find an answer for my question. First let me explain where my question comes from.
I have a web-application that has a user-form in which the user can specify on which days of the week he/she is available and what the office times are at this days. Imagine this as a webform with checkboxes for mo-su and from-time, to-time dropdowns for each checkbox/day. Now if a user profile was inserted to the database table I have something like this in my table:
idUser | idDay | fromTime | toTime
1 | 0 | 08:00 | 09:00
(in this example 0 stands for Monday (0=mo – 6=su))
Of course there is the feature to change the users profile. So it might be possible that first the user was available mo-fr from 5am to 3pm. Now the profile is changed so that the user is only available mo, fr from 5am to 3pm and we, th only till 1pm AND on sa from 9am-5pm, In this case I have to update the existing entries, to delete the removed entries (tue in this example) and to insert the new available days.
Right now I first delete all entries for the user and the I perform a new insert. This is quite easy and in this case probably the best thing to do (because there are max. 7 entries for each user). BUT the same problem could appear in a different situation where it might be better to optimize the query.
So finally my question is: Is it possible to perform an update if exists, insert if not and delete all other entries in just ONE query?
I hope my question is understandable.
The simplest thing is just to delete all instances then create a new schedule for the updated entries. It sounds like you are trying to do some premature optimisation of the process which IMHO will not be necessary. If you find that deleting all existing entries and replacing them is a performance bottleneck then you might want to consider using an approach which tries to minimise the additional inserts but I think that this would probably end up actually being more work than the simpler option of just removing everything and replacing it