具有 UNIQUE INDEX MySQL 和 NULL 列的重复行

发布于 2024-10-09 01:00:57 字数 1929 浏览 0 评论 0原文

表后缀: id, name

(3, 'com')

表域名: id, name

(2, 'microsoft')

表域: id、name_code、后缀

(1, 2, 3)    -- microsoft.com

表子域名:

(4, 'windows')

表子域名: id、name_code、domain

(7, 4, 1)     -- windows.microsoft.com

表电子邮件: id, name, atserver

(3, 'myemail', 7)    -- [email protected]
(4, 'other', 1)      -- [email protected]

这里有一个外键约束的问题。如何解析域和子域以正确创建电子邮件?我在使用具有 NULL 值的唯一索引时遇到问题,例如,解决方案可能是:

表电子邮件: ID、名称、子域、域

(3, 'myemail', 7, NULL)  -- [email protected]
(4, 'other', NULL, 1)    -- [email protected]

但是

(5, 'newemail', NULL, NULL)  -- will duplicated values in the table
(6, 'newemail', NULL, NULL)
(7, 'newemail', NULL, NULL)
(8, 'newemail', NULL, NULL)

并且

(**3**, 'myemail', 7, 1)   -- [email protected] and [email protected]

Table Sufix:
id, name

(3, 'com')

Table DomainName:
id, name

(2, 'microsoft')

Table Domain:
id, name_code, sufix

(1, 2, 3)    -- microsoft.com

Table SubDomainName:

(4, 'windows')

Table SubDomain:
id, name_code, domain

(7, 4, 1)     -- windows.microsoft.com

Table Email:
id, name, atserver

(3, 'myemail', 7)    -- [email protected]
(4, 'other', 1)      -- [email protected]

Here is a problem of foreign key constraints. How can I resolve domains and subdomains to create emails correctly? I'm having problems with Unique INDEX with NULL values, for example, a solution maybe:

Table Email:
id, name, subdomain, domain

(3, 'myemail', 7, NULL)  -- [email protected]
(4, 'other', NULL, 1)    -- [email protected]

BUT

(5, 'newemail', NULL, NULL)  -- will duplicated values in the table
(6, 'newemail', NULL, NULL)
(7, 'newemail', NULL, NULL)
(8, 'newemail', NULL, NULL)

AND

(**3**, 'myemail', 7, 1)   -- [email protected] and [email protected]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

櫻之舞 2024-10-16 01:00:57

怎么样(5,'newemail',domain_id / subdomain_id,'域/子域')

所以你可以有

(5,'newemail',7,'子域')或(5,'newemail',1,'域')

您仍然可以左连接子域和域表,但您将仅根据“域/子域”字段从您需要的数据中获取数据。

这就是快速解决方案。恕我直言,您的数据库结构不是很好,可以优化。您应该将所有域/子域记录保存在一张表中并将其用于电子邮件。该表应该是Table FullDomain:id,name_code,domain_name,subdomain_name


(1, 3, 2, 4) -- windows.microsoft.com

<代码>
(1, 3, 2, 0) -- microsoft.com

How about (5, 'newemail', domain_id/subdomain_id, 'domain/subdomain')

So you could have

(5, 'newemail', 7, 'subdomain') or (5, 'newemail', 1, 'domain')

YOu could still LEFT JOIN SubDomain and Domain table but you will get only the data from the one that you need based on the 'domain/subdomain' field.

That is the quick solution. IMHO your DB structure is not very good and could be optimized. You should keep all domain/subdomain records in one table and use it for the emails. The table should be Table FullDomain: id, name_code, domain_name, subdomain_name


(1, 3, 2, 4) -- windows.microsoft.com

or


(1, 3, 2, 0) -- microsoft.com

后知后觉 2024-10-16 01:00:57

MySQL 中的 Unique 只检查非 NULL 值是否唯一。
因此,如果您不希望有多个以上行:

(6, 'newemail', NULL, NULL)

您必须在最后两个字段上放置一个唯一索引,并在其中放置 NULL(即 0)以外的值

(6, 'newemail', 0, 0)

,然后 MySQL 将阻止多个条目。

Unique in MySQL does only check non-NULL values to be unique.
So if you don't like to have more than one of theese lines:

(6, 'newemail', NULL, NULL)

You have to put a unique index over theese last two fields and put values other than NULL (i.e. 0) in them

(6, 'newemail', 0, 0)

Then MySQL will prevent multiple entries.

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