Skip to content
Advertisement

Update to replace specific value with another except when it already exists

I have a table with values like this:

There’s also a UNIQUE constraint on both columns

I want to change all ‘poo’ to ‘woo’, as long as it doesn’t violate the constraint, and then delete the remaining ‘poo’, in order to obtain this table:

My attempts to far for the first step:

Result: UNIQUE constraint failed: [..]

Result: [..] 0 rows affected

This is an action I’m executing in sqlite-browser, and I’ll probably need it only a couple dozen or so times. So the solution doesn’t need to be very efficient or single-query.

Since my database isn’t ‘live’ I can also, for example, temporarily disable constraint checking, execute my first query, and then clean up duplicates (how?)

Advertisement

Answer

You should correlate the subquery:

And then delete the remaining rows:

See the demo.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement