我有下表(mariaDB):
+----+--------------+-------------+-------------+
| id | content_type | sort_number | document_id |
+----+--------------+-------------+-------------+
| 1 | text | 1 | 1 |
| 2 | table | 2 | 1 |
| 3 | text | 3 | 1 |
| 4 | image | 4 | 1 |
+----+--------------+-------------+-------------+
sort_number
和的组合document_id
是独一无二的。
现在,当我想在位置 2 处添加一个新条目时,我需要将sort_number
所有条目中的sort_number >= 2
增加一个步骤。
为此,我使用以下查询:
update `table_name` set `sort_number` = sort_number +1 where `sort_number` > ? and `document_id` = ?
但是由于唯一键 ( sort_number
and document_id
) 我得到一个错误:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '3' for key 'table_name_sort_number_document_id_unique'
我厌倦了避免错误SET unique_checks=0;
但仍然得到错误......
有没有(更好的)方法来更新sort_number
一个查询?