简体   繁体   English

跨栏唯一值

[英]Value unique across columns

I have a case in which I have two columns, but the value should be unique across both. 在这种情况下,我有两列,但该值在两者之间应该是唯一的。 So you can consider the below table 所以你可以考虑下表

CREATE TABLE user_details(
  id int(11) AUTO_INCREMENT PRIMARY KEY,
  email varchar(200),
  secondary_email varchar(200),
  unique key(email),
  unique key(secondary_email)
);

The feature I want is, email cannot repeat across email and secondary_email columns. 我想要的功能是,电子邮件不能在emailsecondary_email列之间重复。 So a given email can be linked only with one user. 因此,给定的电子邮件只能与一个用户链接。 Can we achieve it in MySQL without triggers? 我们可以在没有触发器的情况下在MySQL中实现它吗? Currently I am managing it through application logic, and it sometimes very hard to handle as different people across modules from different teams insert/update data in the table. 目前,我是通过应用程序逻辑来管理它的,有时很难处理,因为来自不同团队的模块中的不同人员在表中插入/更新数据。

Also to add, my user table is in order of millions of records and there are option for secondary mobile also. 另外要添加的是,我的用户表的记录顺序为数百万条记录,并且还有用于辅助移动设备的选项。 Creating an second (or third) table of 2x size and joining always the two(or three) tables will worsen my case. 创建2x大小的第二个(或第三个)表并始终将两个(或三个)表连接在一起会使我的情况恶化。

Thanks in advance for all your inputs.. 预先感谢您的所有输入。

If you normalize your design, you can have an e-mail table that references your user details: 如果您对设计进行规范化,则可以有一个引用您的用户详细信息的电子邮件表:

user_details_email
------------------
user_details_id (FK)
email_address

With a primary key across user_details_id, email_address . 通过user_details_id, email_address的主键。 You can optionally add a boolean column to flag it as a primary. 您可以选择添加一个布尔列以将其标记为主要列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM