简体   繁体   English

表格中的两个主非复合键

[英]Two primary non composite keys in a table

I am working on a project and I realized I am unsure about how to use multiple primary keys. 我正在从事一个项目,但我意识到我不确定如何使用多个主键。 I have a table named "User_Details" that has the details of Customer ID, email address and password. 我有一个名为“ User_Details”的表,其中包含客户ID,电子邮件地址和密码的详细信息。 From my understanding, I can use both Customer ID and email address as the primary key. 据我了解,我可以同时使用客户ID和电子邮件地址作为主键。 In this case do I use only one as Primary Key or both? 在这种情况下,我仅使用一个作为主键还是同时使用两者? If I use both, do they become composite primary keys? 如果同时使用它们,它们是否会成为复合主键?

(PS. I have other tables, where the foreign key is the customer ID) (PS。我还有其他表,其中外键是客户ID)

You can only have one primary key, but you could definitely have other unique fields. 您只能有一个主键,但是您肯定可以有其他唯一字段。

Usually using an integer / id as primary key is preferred over a string key, and an id is presumably auto assigned, where as email could change - which would be a problem for foreign key relations. 通常首选使用整数/ id作为主键而不是字符串键,并且id可能是自动分配的,因为电子邮件可能会更改-这对于外键关系来说是个问题。

Since you already use customer Id as a foreign key in other tables, I would suggest you continue to do that. 由于您已经在其他表中将客户ID用作外键,因此建议您继续这样做。

You can only have one primary key, but you can have multiple columns in your primary key, alternatively you can also have Unique Indexes on your table, which will work a bit like a primary key in that they will enforce unique values, and will speed up querying of those values. 您只能有一个主键,但是主键中可以有多个列,或者表上也可以有唯一索引,这与主键有点像,因为它们将强制执行唯一值并加快速度查询这些值。

Easiest way tho is a Composite Primary Key which is a primary key made from two or more columns. 最简单的方法是组合主键 ,它是由两列或更多列组成的主键。 For example: 例如:

CREATE TABLE userdata (
userid INT,
userdataid INT,
info char(200),
primary key (userid, userdataid),
);

Here is more info: Link 这是更多信息: 链接

You can have a Composite Primary Key which is a primary key made from two or more columns. 您可以有一个复合主键,它是由两列或更多列组成的主键。 For example: 例如:

CREATE TABLE userdata (
  userid INT,
  userdataid INT,
  info char(200),
  primary key (userid, userdataid),
);

A table can have multiple candidate keys. 一个表可以有多个候选键。 Each candidate key is a column or set of columns that are UNIQUE, taken together, and also NOT NULL. 每个候选键都是唯一的一列或一组列,这些列合在一起,并且也不为空。 Thus, specifying values for all the columns of any candidate key is enough to determine that there is one row that meets the criteria, or no rows at all. 因此,为任何候选键的所有列指定值就足以确定存在符合条件的一行,或者根本没有任何行。

Candidate keys are a fundamental concept in the relational data model. 候选键是关系数据模型中的基本概念。

It's common practice, if multiple keys are present in one table, to designate one of the candidate keys as the primary key. 如果一个表中有多个键,通常的做法是将候选键之一指定为主键。 It's also common practice to cause any foreign keys to the table to reference the primary key, rather than any other candidate key. 导致表中的任何外键都引用主键而不是其他任何候选键也是一种常见的做法。

I recommend these practices, but there is nothing in the relational model that requires selecting a primary key among the candidate keys. 我推荐这些做法,但是关系模型中没有任何内容需要在候选键中选择一个主键。

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

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