Mysql中的两列在column1和column2之间具有唯一性约束

发布于 2024-07-30 10:57:25 字数 676 浏览 4 评论 0原文

让我们考虑一个用户表: Username,email1,email2

现在,我想确保此表中没有两行具有任何共同的电子邮件地址。 确保这一点的最佳方法是什么?

例如:如果表中有行:“Bill,[电子邮件受保护],< a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8ae8e3e6e6caede7ebe3e6a4e9e5e7">[电子邮件受保护]",然后 尝试插入类似“Billy,[email protected],[email protected]" 应该会给出错误。

提前致谢, 钱德雷什

Let us consider a user table:
Username,email1,email2

Now, I want to make sure that no two rows in this table have any common email address. What would be the best way to ensure this ?

E.g.: If table has row: "Bill,[email protected],[email protected]", then
trying to insert a row like "Billy,[email protected],[email protected]" should give an error.

Thanks in advance,
Chandresh

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

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

发布评论

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

评论(3

热鲨 2024-08-06 10:57:25

看起来您正在尝试使用数据模型强制执行 1:1 和 1:multiple 之间的组合。 更好的选择是单独存储每个用户名/电子邮件组合。 一行代表“用户名,电子邮件1”,另一行代表“用户名,电子邮件2”。 如果您有其他与“用户名”相关的字段,这些字段可以保留在核心用户表中,而电子邮件将移动到两列电子邮件表,并在(用户名,电子邮件)上具有多列唯一索引。

It looks like you're trying to force a combo between 1:1 and 1:multiple with your data model. Your better bet is to store each Username/Email combo separately. One row for "Username,email1" and another row for "Username,email2". If you have additional "Username"-related fields, those could stay in the core User table, while emails would move to a two-column Email table with a multi-column unique index on (Username,Email).

撩发小公举 2024-08-06 10:57:25

遗憾的是,check 约束并不是 MySQL 的一项功能。 嗯,确实如此,但他们被忽视了。

使用触发器代替:

create trigger MyValidation after insert on user for each row
begin
    if (select count(*) from user where 
        email1 = new.email1
        or email1 = new.email2 
        or email2 = new.email1 
        or email2 = new.email2) > 0 then
        --I know that the delete could use this logic, but you can
        --insert other instructions here.

        delete from user where username = new.username

    end if
end

Sadly, check constraints are not a feature of MySQL. Well, they are, but they're ignored.

Use a trigger, instead:

create trigger MyValidation after insert on user for each row
begin
    if (select count(*) from user where 
        email1 = new.email1
        or email1 = new.email2 
        or email2 = new.email1 
        or email2 = new.email2) > 0 then
        --I know that the delete could use this logic, but you can
        --insert other instructions here.

        delete from user where username = new.username

    end if
end
葬シ愛 2024-08-06 10:57:25

也许您可以使用某种存储过程,但这可能是您在插入数据之前可以检查的内容。

SELECT email1, email2 FROM yourTable 
WHERE email1 LIKE %@email% or email2 LIKE %@email%

Maybe you could use some sort of stored procedure, but this may be something that you could check for before inserting your data.

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