mySQL更新唯一值返回错误代码:1062。重复条目

发布于 2025-02-01 11:22:08 字数 1564 浏览 3 评论 0原文

我在表中更新唯一的列值有问题。

我有一个客户表,我有一个单独的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜唯美灬不弃 2025-02-08 11:22:08

首先,我认为您的桌子结构与您发布的设计相矛盾,

我有一个客户表,我有一个单独的custeruty_address表
客户可以拥有一个主要地址和其他地址。这
主要地址是交付货物的主要地址。
客户可以有很多地址,但必须是

使用上表设计,您只能有两个条目,每个客户 customer_id ,您将永远无法存储每个客户的2个地址,一个将是主要的,并且另一个是非主要的。

为此,您可能需要先重新设计表格才能满足您的要求。

First of all I think your table structure is contradicting the design you posted,

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

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.

不知在何时 2025-02-08 11:22:08

因此,客户的主要地址是成为次要地址,次要地址是成为主要地址。问题是,如果您尝试修改它们中的任何一个,则该操作将由于另一个而失败。

但是,我们知道null = null是一个错误的标准,因此,您需要将其中一个设置为nullupdate> update然后更新无效的一个:

UPDATE customer_address SET primary = NULL WHERE id=3;
UPDATE customer_address SET primary = 1 WHERE id=2;
UPDATE customer_address SET primary = 0 WHERE id=3;

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 to NULL, update the other and then update the nulled one:

UPDATE customer_address SET primary = NULL WHERE id=3;
UPDATE customer_address SET primary = 1 WHERE id=2;
UPDATE customer_address SET primary = 0 WHERE id=3;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文