gpt4 book ai didi

mysql - 我无法更新具有两个唯一键的表

转载 作者:行者123 更新时间:2023-11-29 02:20:27 26 4
gpt4 key购买 nike

我有一个这样的表:

DROP TABLE IF EXISTS `locations`;
CREATE TABLE IF NOT EXISTS `locations` (
`tenant_id` int(11) NOT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
`waypoint_id` int(11) NOT NULL,
`material` int(11),
`price` decimal(10,2) NOT NULL,
PRIMARY KEY (`tenant_id`,`id`),
UNIQUE KEY `id` (`id`),
UNIQUE KEY(`waypoint_id`, `material`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;

我正在运行这个查询:

    UPDATE locations
SET waypoint_id=23,
material=19,
price=22.22,
unit_id=1
WHERE tenant_id=3 AND id = 54;

我收到以下错误:

Duplicate entry '23-19' for key 'waypoint_id' 

我知道我已经有了包含这些 ID 的记录,但如果它不允许我更改它们,我该如何编辑该行中的值?

如果我不尝试插入 ID 为 23-19 的新记录,但我只是想更新该记录,我不明白为什么会出现此错误。我该如何解决这个问题?

注意

抱歉,我粘贴了错误的查询,我用第一个造成错误的查询编辑了查询。

最佳答案

您的查询似乎有误。应该是这样的:

UPDATE locations
SET waypoint_id=23,
material=19,
price=22.22,
unit_id=1
WHERE tenant_id=3 AND id = 54;

编辑:

您需要检查您的表是否没有 waypoint_id 为 23 和 material 为 19,因为您已将其设为唯一。如果已经存在任何重复条目,则不能添加 23 值和 19 值。

你可以这样检查:

select waypoint_id, material from locations WHERE tenant_id=3 AND id = 54;

或者更确切地说,检查一下

select * from locations where waypoint_id = 23 or material = 19

您的问题的解决方法是像这样从您的表中删除唯一键约束

alter table locations drop index waypoint_id ;
alter table locations drop index material ;

然后就可以进行更新了

然后像这样在两列的组合上应用唯一键:

ALTER TABLE `locations` ADD UNIQUE `unique_index`(`waypoint_id`, `material`);

关于mysql - 我无法更新具有两个唯一键的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32775422/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com