I’m a bit confused as to how to best update a join table.
Presentaion
id PK
Asset
id PK
PresentationAsset
presentationid
assetid
So when updating a Presentation and it’s associated assets within PresentationAsset, do I first search for all relationships, delete those and then create new ones?
What’s the best approach to a scenario like this?
It’s certainly easiest, at least, to delete them all and create new ones. After all, it’s only two queries.
Assuming those are indeed the only two columns in that table (no timestamps or anything, for example), I can’t imagine any reason you’d need to complicate it any more than that.
After all, the alternative to wiping your existing rows and inserting all new ones would be to keep the ones that already exist, but delete ones that no longer should, and insert new ones. Essentially, you’re still ending up with at least one
DELETEand oneINSERT, so there shouldn’t be much performance difference in most real world applications. Performance may even be better in the first approach, since it requires less value checking per operation.Of course, if you’re working with a large enough scenario where such a minute performance difference would be important, you’d probably want to do real tests of all your options and measure performance yourself.
And of course, if those aren’t the only two columns in your table, you’ll need to properly keep the values of the other columns for the rows you’re updating, so deleting all and inserting new wouldn’t be an option anyway.