Swap unique indexed column values in database

前端 未结 12 964
伪装坚强ぢ
伪装坚强ぢ 2020-12-03 06:36

I have a database table and one of the fields (not the primary key) is having a unique index on it. Now I want to swap values under this column for two rows. How could this

12条回答
  •  温柔的废话
    2020-12-03 06:50

    I have the same problem. Here's my proposed approach in PostgreSQL. In my case, my unique index is a sequence value, defining an explicit user-order on my rows. The user will shuffle rows around in a web-app, then submit the changes.

    I'm planning to add a "before" trigger. In that trigger, whenever my unique index value is updated, I will look to see if any other row already holds my new value. If so, I will give them my old value, and effectively steal the value off them.

    I'm hoping that PostgreSQL will allow me to do this shuffle in the before trigger.

    I'll post back and let you know my mileage.

提交回复
热议问题