首页 > 数据库 >两个Mysql唯一索引的交换: 避免重复索引 Duplicate entry '3' for key 'priority_UNIQUE'

两个Mysql唯一索引的交换: 避免重复索引 Duplicate entry '3' for key 'priority_UNIQUE'

时间:2024-01-10 10:44:06浏览次数:37  
标签:tasks UPDATE priority 索引 Duplicate MySQL UNIQUE

需求

我做了一个排行榜,但是主键是pid,不是排名,排名作为唯一索引,两个人排名交换,只需要交换 排名唯一索引值即可.
但是直接单独更新 提示错误: Duplicate entry '3' for key 'priority_UNIQUE'

方法

本来希望可以在一条SQL语句中交换两个唯一索引值,但是发现这是不可能的,因为值得修改是一个一个 修改的,但凡修改为了 其他唯一索引值 就会报错.
所以只能先把两个记录的值 修改为 负数(保证仍然不会重复的值) , 然后再修改为需要交换的值,这样就可以达成需求,不过这个过程需要4条SQL语句.

后来发现 一条SQL语句修改两个值的, 那么就可以缩减为 2条SQL语句,为了避免错误导致只修改了一般,所以一般使用SQL事务.

SQL事务代码如下:

START TRANSACTION ;

UPDATE tasks
SET priority =
CASE
WHEN priority = 2 THEN -3
WHEN priority = 3 THEN -2
END
WHERE priority IN (2,3) ;

UPDATE tasks
SET priority = - priority
WHERE priority IN (-2,-3) ;

COMMIT ;

参考资料

mysql 互换两行,如何在不违反唯一约束的情况下在MySQL中交换两行的值?

I have a "tasks" table with a priority column, which has a unique constraint.

I'm trying to swap the priority value of two rows, but I keep violating the constraint. I saw this statement somewhere in a similar situation, but it wasn't with MySQL.

UPDATE tasks

SET priority =

CASE

WHEN priority=2 THEN 3

WHEN priority=3 THEN 2

END

WHERE priority IN (2,3);

This will lead to the error:

Error Code: 1062. Duplicate entry '3' for key 'priority_UNIQUE'

Is it possible to accomplish this in MySQL without using bogus values and multiple queries?

EDIT:

Here's the table structure:

CREATE TABLE `tasks` (

`id` int(11) NOT NULL,

`name` varchar(200) DEFAULT NULL,

`priority` varchar(45) DEFAULT NULL,

PRIMARY KEY (`id`),

UNIQUE KEY `priority_UNIQUE` (`priority`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8

解决方案

Is it possible to accomplish this in MySQL without using bogus values and multiple queries?

No. (none that I can think of).

The problem is how MySQL processes updates. MySQL (in difference with other DBMS that implement UPDATE properly), processes updates in a broken manner. It enforces checking of UNIQUE (and other) constraints after every single row update and not - as it should be doing - after the whole UPDATE statement completes. That's why you don't have this issue with (most) other DBMS.

For some updates (like increasing all or some ids, id=id+1), this can be solved by using - another non-standard feature - an ORDER BY in the update.

For swapping the values from two rows, that trick can't help. You'll have to use NULL or a bogus value (that doesn't exist but is allowed in your column) and 2 or 3 statements.

You could also temporarily remove the unique constraint but I don't think that's a good idea really.

So, if the unique column is a signed integer and there are no negative values, you can use 2 statements wrapped up in a transaction:

START TRANSACTION ;

UPDATE tasks

SET priority =

CASE

WHEN priority = 2 THEN -3

WHEN priority = 3 THEN -2

END

WHERE priority IN (2,3) ;

UPDATE tasks

SET priority = - priority

WHERE priority IN (-2,-3) ;

COMMIT ;

标签:tasks,UPDATE,priority,索引,Duplicate,MySQL,UNIQUE
From: https://www.cnblogs.com/ayanmw/p/17955985

相关文章

  • ES7.3版本,批量添加,索引创建,索引判断
    importcom.link.risk.model.RiskTradeDetail;importcom.link.util.BeanBuilder;importorg.apache.commons.lang3.StringUtils;importorg.elasticsearch.action.DocWriteRequest;importorg.elasticsearch.action.bulk.*;importorg.elasticsearch.action.index.Index......
  • oracle复合索引怎么建立
    在Oracle中,可以使用以下语法来创建复合索引:CREATEINDEXindex_nameONtable_name(column1,column2,...);其中,index_name是你给索引起的名称,table_name是要在其上创建索引的表名,column1,column2,...是要包含在索引中的列名(按照你希望的顺序)。以下是一个示例,展示如何创建......
  • MySQL中的索引:深入理解与案例解析
    引言在数据库中,索引是提高查询速度的关键。特别是在MySQL这样的关系型数据库中,索引的作用尤为重要。本文将深入探讨MySQL中的索引,通过案例解析帮助您更好地理解其工作原理和应用。一、索引的基本概念索引是什么?:简而言之,索引是数据库中用于快速查找数据的数据结构。它类似于书籍......
  • 数据库索引
    一、索引的基本概念数据库索引是一种数据结构,用于快速定位到表中的数据记录。通过创建索引,数据库系统可以快速找到需要的数据,避免全表扫描,从而大大提高查询速度。索引的创建和使用需要占用额外的存储空间,并会影响数据插入、更新和删除操作的性能。因此,索引的使用需要权衡利弊,根据实......
  • elasticsearch集群red恢复损坏的索引
    背景客户磁盘损坏,修复磁盘后,重启机器,发现elasticsearch启动成功,ES状态正常green,但是历史数据都没有加载进,查看ES存储数据目录,发现数据还在。解决方案首先,需要确认indices目录下的lucene索引正常。需要关闭ES(实际操作索引处于close状态也可以)。#检测lucene索引数据是否正......
  • 在TypeScript中重命名类型索引?
    在TypeScript中,要重命名类型索引,你可以使用TypeScript的类型别名或接口来实现。下面是两种常见的方法:1.使用类型别名(TypeAliases):类型别名允许你为一个类型定义一个别名,通过使用关键字type来创建。你可以在别名中定义索引类型,并为该类型定义一个新的名称。```typescripttypeMy......
  • VMware Aria Suite 8.12 - 云管理解决方案 (下载索引)
    VMwareAriaSuite8.12-云管理解决方案(下载索引)作者主页:sysin.org云管理套包VMwareAriaSuite(以前称为vRealizeSuite)和vCloudSuiteVMwareAriaSuite(vRealizeSuite)和vCloudSuite可为VMwareCloud及其他平台提供自助服务使用层、自动化框架和自动驾驶式后续运......
  • 8.Elasticsearch,分布式搜索引擎
    1.Elasticsearch入门Elasticsearch简介一个分布式的、Restful风格的搜索引擎。支持对各种类型的数据的检索。搜索速度快,可以提供实时的搜索服务。便于水平扩展,每秒可以处理PB级海量数据。Elasticsearch术语索引、类型、文档、字段。集群、节点、分片、副本。术......
  • 如何在使用logstash导出的时候,包含索引名称和_id
    这里,我的需求是,把es中的数据导出到本地文件中,一行一个json。我还需要把原来数据中的_id保存下来,在新的集群中导入的时候,指定_id。这里有一个配置文件!logstash配置文件如下#SampleLogstashconfigurationforcreatingasimple#Beats->Logstash->Elasticsearchpipeline.......
  • 【C++】STL 容器 - priority_queue 优先级队列容器 ( 容器简介 | 容器操作性能分析 |
    文章目录一、priority_queue优先级队列容器1、priority_queue优先级队列容器简介2、priority_queue优先级队列容器操作性能分析二、代码示例-priority_queue优先级队列容器1、默认优先级队列容器2、最大值优先级队列3、最小值优先级队列一、priority_queue优先级队列容器......