mySQL更新唯一值返回错误代码:1062。重复条目
我在表中更新唯一的列值有问题。
我有一个客户表,我有一个单独的custerution_address表,客户可以在其中有一个主要地址和其他地址。主要地址是交付货物的主要地址。客户可以有很多地址,但必须是主要地址。
当我尝试更新时,我会收到消息:错误代码:1062。键'primate_unique''0-3'
CREATE TABLE `customer_address` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) DEFAULT NULL,
`address` varchar(525) NOT NULL,
`customer_id` int(11) NOT NULL,
`primary` tinyint(4) DEFAULT '0',
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `primary_UNIQUE` (`primary`,`customer_id`),
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8
我的计划是所有非主要地址的值为0,并且只有主要地址的值为1。
id | name | address | customer_id | primary
------------------------------------------------------------------------------
'2', 'Store 1', 'Address name 24', '3', '0'
'3', 'Wholesale', 'Address name 24', '3', '1'
'4', 'Store 2', 'Address name 24', '1', '0'
'5', 'Wholesale 3', 'Address name 24', '1', '1'
我如何尝试更改主要地址:
首先,我需要更新id = 3
并设置primary
<代码> 0 。这将导致重复的条目
UPDATE customer_address SET primary = 1 WHERE id=2;
UPDATE customer_address SET primary = 0 WHERE id=3;
有其他方法可以解决这个问题吗?
I have a problem with updating the unique column value in the table.
I have a customer table and I have a separate customer_address table where the customer can have one primary address and others. The primary address is the main address to which the goods are delivered. Customers can have many addresses but one must be primary.
When I try to update I get the message: Error Code: 1062. Duplicate entry '0-3' for key 'primary_UNIQUE'
CREATE TABLE `customer_address` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) DEFAULT NULL,
`address` varchar(525) NOT NULL,
`customer_id` int(11) NOT NULL,
`primary` tinyint(4) DEFAULT '0',
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `primary_UNIQUE` (`primary`,`customer_id`),
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8
My plan was that all non-primary addresses have a value of 0, and only the primary address has a value of 1.
id | name | address | customer_id | primary
------------------------------------------------------------------------------
'2', 'Store 1', 'Address name 24', '3', '0'
'3', 'Wholesale', 'Address name 24', '3', '1'
'4', 'Store 2', 'Address name 24', '1', '0'
'5', 'Wholesale 3', 'Address name 24', '1', '1'
How I try to change primary address:
First, I need to update id = 3
and set primary
to 0
. This will cause Duplicate entry
UPDATE customer_address SET primary = 1 WHERE id=2;
UPDATE customer_address SET primary = 0 WHERE id=3;
Is there another way to solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,我认为您的桌子结构与您发布的设计相矛盾,
使用上表设计,您只能有两个条目,每个客户 customer_id ,您将永远无法存储每个客户的2个地址,一个将是主要的,并且另一个是非主要的。
为此,您可能需要先重新设计表格才能满足您的要求。
First of all I think your table structure is contradicting the design you posted,
With the above table design you can have only two entries per customer_id so you will never be able to store more then 2 addresses per customer, and one will be primary and the other being the non-primary.
For this you might need to redesign your table first to fulfill the requirements you have.
因此,客户的主要地址是成为次要地址,次要地址是成为主要地址。问题是,如果您尝试修改它们中的任何一个,则该操作将由于另一个而失败。
但是,我们知道
null = null
是一个错误的标准,因此,您需要将其中一个设置为null
,update> update
然后更新
无效的一个:So, a customer's primary address is to become the secondary address and secondary address is to become primary address. The problem is that if you try to modify either of them, then the operation will fail because of the other.
However, we know that
NULL = NULL
is a false criteria, so, you need to set one of them toNULL
,update
the other and thenupdate
the nulled one: