简体   繁体   English

UNIQUE索引如何在多列上工作?

[英]How do UNIQUE indexes work on multiple columns?

If I add a UNIQUE index on the email,password columns, will the index check if: 如果我在email,password列上添加了唯一索引,索引将检查是否:

  1. Is email unique AND is password unique, or email独特的ANDpassword特有的,或
  2. The combination of email,password is unique, as in, it would allow u1@mail.com, 123 and u2@mail.com, 123 email,password的组合是唯一的,例如,允许u1@mail.com, 123u2@mail.com, 123

Apologies if my thoughts seem abrupt here, I'm having difficulty in expressing them today for some reason. 抱歉,如果我的想法在这里突然出现,出于某种原因,我今天很难表达它们。 Thanks in advance! 提前致谢!

Your second answer is the correct one. 您的第二个答案是正确的。 The combination of the two fields must be unique. 这两个字段的组合必须唯一。

For #1 you would need a separate unique index on each column. 对于#1,您将需要在每一列上使用单独的唯一索引。

I'm agree with @redFilter the combination is a better option. 我同意@redFilter,该组合是更好的选择。 But remember that UNIQUE is different that primary key. 但是请记住,UNIQUE与主键不同。 You can have null values in UNIQUE, if you want to avoid that when you create the table use the "NOT NULL" at the end of each data type. 如果要避免在创建表时在UNIQUE中包含空值,请在每个数据类型的末尾使用“ NOT NULL”。

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

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