如何在使用 SQL UPDATE 语句时更改更新顺序?
How to change the update order while using the SQL UPDATE statement?
我有一个 table,其中每一行都使用唯一索引 (1,2,3,...) 进行编号。
我想将每个条目的索引增加 4,以便在开头为四个新条目(索引 1、2、3 和 4)创建 space。
我尝试使用:
UPDATE table SET key_index = key_index + 4;
但它会导致错误,因为正如所说,索引必须是唯一的(将 1 增加 4 结果为 5。索引 5 可能已经存在)。
如果我可以使用UPDATE语句,从底部开始(从最高到最低索引),那么就会出现这样的错误。是否可以使用这样的 UPDATE 语句?
这可能很棘手。我使用的一个技巧是两次更新——首先更新到 "safe" 的一系列值。第二个期望值:
update t
set key_index = - key_index ;
update t
set key_index = (- key_index) + 4;
您也可以删除唯一 index/constraint -- 或者在某些数据库中禁用它。
如果您是 运行 MySQL,您可以使用简单的 update
,并使用 order by
子句按降序对行进行排序。这使您可以仅在一个查询中执行更改,而没有任何冲突风险(因为您的值是唯一的)。
MySQL 在更新语句中支持 order by
,行为是 described in the documentation
If an UPDATE
statement includes an ORDER BY
clause, the rows are updated in the order specified by the clause. This can be useful in certain situations that might otherwise result in an error.
文档继续提供一个与您的用例相似的示例。
所以:
UPDATE table SET key_index = key_index + 4 ORDER BY id DESC;
create table mytable (key_index int);
insert into mytable values (1), (4), (5), (8), (9);
update mytable
set key_index = key_index + 4
order by key_index desc;
select * from mytable;
| key_index |
| --------- |
| 5 |
| 8 |
| 9 |
| 12 |
| 13 |
戈登一针见血地回答了这个问题。但是,如果您要更新 key_index,请记住还要更新外部引用表中的值(如果有的话)。
或者,您可以尝试其他方法。添加另一列,例如 dup_key_index
,值为 key_index + 4
。然后删除 key_index
列并将此新列 dup_key_index
重命名为 key_index
列。
# Add new column to hold the key_index value
alter table TABLE_NAME add dup_key_index int not null;
# Update this columns value from the key_index value and increment
update TABLE_NAME set dup_key_index = key_index + 4;
# Drop the key_index
alter table TABLE_NAME drop key_index;
# Rename the new column with same name as key_index
alter table TABLE_NAME change dup_key_index key_index int primary key;
SQL 服务器:
如果您使用 SQL 服务器,引擎会在内部处理以下查询:
update TABLE_NAME set key_index = key_index + 4;
参考this.
我有一个 table,其中每一行都使用唯一索引 (1,2,3,...) 进行编号。 我想将每个条目的索引增加 4,以便在开头为四个新条目(索引 1、2、3 和 4)创建 space。
我尝试使用:
UPDATE table SET key_index = key_index + 4;
但它会导致错误,因为正如所说,索引必须是唯一的(将 1 增加 4 结果为 5。索引 5 可能已经存在)。
如果我可以使用UPDATE语句,从底部开始(从最高到最低索引),那么就会出现这样的错误。是否可以使用这样的 UPDATE 语句?
这可能很棘手。我使用的一个技巧是两次更新——首先更新到 "safe" 的一系列值。第二个期望值:
update t
set key_index = - key_index ;
update t
set key_index = (- key_index) + 4;
您也可以删除唯一 index/constraint -- 或者在某些数据库中禁用它。
如果您是 运行 MySQL,您可以使用简单的 update
,并使用 order by
子句按降序对行进行排序。这使您可以仅在一个查询中执行更改,而没有任何冲突风险(因为您的值是唯一的)。
MySQL 在更新语句中支持 order by
,行为是 described in the documentation
If an
UPDATE
statement includes anORDER BY
clause, the rows are updated in the order specified by the clause. This can be useful in certain situations that might otherwise result in an error.
文档继续提供一个与您的用例相似的示例。
所以:
UPDATE table SET key_index = key_index + 4 ORDER BY id DESC;
create table mytable (key_index int);
insert into mytable values (1), (4), (5), (8), (9);
update mytable
set key_index = key_index + 4
order by key_index desc;
select * from mytable;
| key_index |
| --------- |
| 5 |
| 8 |
| 9 |
| 12 |
| 13 |
戈登一针见血地回答了这个问题。但是,如果您要更新 key_index,请记住还要更新外部引用表中的值(如果有的话)。
或者,您可以尝试其他方法。添加另一列,例如 dup_key_index
,值为 key_index + 4
。然后删除 key_index
列并将此新列 dup_key_index
重命名为 key_index
列。
# Add new column to hold the key_index value
alter table TABLE_NAME add dup_key_index int not null;
# Update this columns value from the key_index value and increment
update TABLE_NAME set dup_key_index = key_index + 4;
# Drop the key_index
alter table TABLE_NAME drop key_index;
# Rename the new column with same name as key_index
alter table TABLE_NAME change dup_key_index key_index int primary key;
SQL 服务器: 如果您使用 SQL 服务器,引擎会在内部处理以下查询:
update TABLE_NAME set key_index = key_index + 4;
参考this.