【发布时间】:2012-01-14 16:48:32
【问题描述】:
我有一个在两列上具有唯一索引的表,确切地说是 id_parent 和 sort_order
+----+-----------+------------+-------------+-------------+-------------+
| id | id_parent | sort_order | some_data | other_data | more_data |
+----+-----------+------------+-------------+-------------+-------------+
| 1 | 1 | 1 | lorem ipsum | lorem ipsum | lorem ipsum |
| 2 | 1 | 2 | lorem ipsum | lorem ipsum | lorem ipsum |
| 3 | 1 | 3 | lorem ipsum | lorem ipsum | lorem ipsum |
+----+-----------+------------+-------------+-------------+-------------+
现在我想一次性更新它们、它们的数据和 sort_order。 sort_order 将从1 - 2 - 3 更改为,例如2 - 3 - 1。
但是当我开始运行更新语句时,唯一索引阻止了我,正如预期的那样,说我不能有两行 id_parent = 1 and sort_order = 2。
好吧,我现在可以将其设置为 4,以正确的顺序更新其他行,然后设置这一行。
但是,我将不得不运行一个额外的语句,并且很可能会在我的脚本语言中添加额外的逻辑来确定正确的更新顺序。
我也用ORM,就更不方便了。
我现在的问题是,有什么方法可以让mysql暂时忽略这个索引吗?就像开始一个特殊的事务,其中的索引只会在提交之前计算?
【问题讨论】:
-
你的表是 InnoDB 还是 MyISAM?
-
删除唯一键:stackoverflow.com/a/9172188/5381704。稍后再添加。
标签: mysql sql indexing sql-update