Swap unique indexed column values in database

前端 未结 12 963
伪装坚强ぢ
伪装坚强ぢ 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:56

    I also think that #2 is the best bet, though I would be sure to wrap it in a transaction in case something goes wrong mid-update.

    An alternative (since you asked) to updating the Unique Index values with different values would be to update all of the other values in the rows to that of the other row. Doing this means that you could leave the Unique Index values alone, and in the end, you end up with the data that you want. Be careful though, in case some other table references this table in a Foreign Key relationship, that all of the relationships in the DB remain intact.

提交回复
热议问题