Is it that a primary key is the selected candidate key chosen for a given table?
主键是为给定表选择的候选键吗?
11 个解决方案
#1
76
Candidate Key
– A Candidate Key can be any column or a combination of columns that can qualify as unique key in database. There can be multiple Candidate Keys in one table. Each Candidate Key can qualify as Primary Key.
候选键——候选键可以是任何列,也可以是可以作为数据库中唯一键的列的组合。一个表中可以有多个候选键。每个候选键都可以限定为主键。
Primary Key
– A Primary Key is a column or a combination of columns that uniquely identify a record. Only one Candidate Key can be Primary Key.
主键——主键是一个列,或者是唯一标识记录的列的组合。只有一个候选键可以是主键。
More on this link with example
更多关于这个链接的例子。
#2
19
John Woo's answer is correct, as far as it goes. Here are a few additional points.
吴宇森的答案是正确的。这里有一些额外的要点。
A primary key is always one of the candidate keys. Fairly often, it's the only candidate.
主键总是候选键之一。通常,它是唯一的候选者。
A table with no candidate keys does not represent a relation. If you're using the relational model to help you build a good database, then every table you design will have at least one candidate key.
没有候选键的表不表示关系。如果您正在使用关系模型来帮助您构建一个良好的数据库,那么您设计的每个表将至少有一个候选键。
The relational model would be complete without the concept of primary key. It wasn't in the original presentation of the relational model. As a practical matter, the use of foreign key references without a declared primary key leads to a mess. It could be a logically correct mess, but it's a mess nonetheless. Declaring a primary key lets the DBMS help you enforce the data rules. Most of the time, having the DBMS help you enforce the data rules is a good thing, and well worth the cost.
没有主键的概念,关系模型将是完整的。它不在关系模型最初的表示中。实际上,使用没有声明主键的外键引用会导致混乱。它可能是逻辑上正确的混乱,但它仍然是混乱。声明一个主键可以让DBMS帮助您执行数据规则。大多数情况下,使用DBMS帮助您执行数据规则是一件好事,而且值得付出代价。
Some database designers and some users have some mental confusion about whether the primary key identifies a row (record) in a table or an instance of an entity in the subject matter that the table represents. In an ideal world, it's supposed to do both, and there should be a one-for-one correspondence between rows in an entity table and instances of the corresponding entity.
一些数据库设计人员和一些用户对主键是标识表中的行(记录)还是表中表示的主题内容中的实体实例有一些困惑。在一个理想的世界中,它应该同时执行这两种操作,在实体表中的行和对应实体的实例之间应该有一个一对一的对应关系。
In the real world, things get screwed up. Somebody enters the same new employee twice, and the employee ends up with two ids. Somebody gets hired, but the data entry slips through the cracks in some manual process, and the employee doesn't get an id, until the omission is corrected. A database that does not collapse the first time things get screwed up is more robust than one that does.
在现实世界里,事情搞砸了。有人输入同一名新员工两次,该员工得到两个id。有人被雇佣了,但是数据录入从一些手工过程的裂缝中滑了出来,而员工直到被遗漏的地方被纠正后才会得到一个id。一个不会在第一次出错时崩溃的数据库比一个会崩溃的数据库更健壮。
#3
8
Primary key -> Any column or set of columns that can uniquely identify a record in the table is a primary key. (There can be only one Primary key in the table)
主键——>任何一列或一组列可以惟一地标识表中的记录是主键。(表中只能有一个主键)
Candidate key -> Any column or set of columns that are candidate to become primary key are Candidate key. (There can be one or more candidate key(s) in the table, if there is only one candidate key, it can be chosen as Primary key)
候选键->任何成为主键的候选列或列集都是候选键。(表中可以有一个或多个候选键,如果只有一个候选键,可以选择它作为主键)
#4
6
There is no difference. A primary key is a candidate key. By convention one candidate key in a relation is usually chosen to be the "primary" one but the choice is essentially arbitrary and a matter of convenience for database users/designers/developers. It doesn't make a "primary" key fundamentally any different to any other candidate key.
没有区别。主键是候选键。按照惯例,关系中的一个候选键通常被选择为“主”键,但是这个选择本质上是任意的,对于数据库用户/设计人员/开发人员来说是非常方便的。它不会使“主”键从根本上与任何其他候选键不同。
#5
2
A table can have so many column which can uniquely identify a row. This columns are referred as candidate keys, but primary key should be one of them because one primary key is enough for a table. So selection of primary key is important among so many candidate key. Thats the main difference.
一个表可以有如此多的列,可以惟一地标识行。这些列被称为候选键,但是主键应该是其中的一个,因为一个主键就足够一个表了。因此,在众多候选密钥中,主密钥的选择是非常重要的。的主要区别。
#6
2
A Primary key is a special kind of index in that:
主键是其中一种特殊的索引:
there can be only one;
it cannot be nullable
it must be unique.
Candidate keys are selected from the set of super keys, the only thing we take care while selecting candidate key is: It should not have any redundant attribute.
从超级键的集合中选择候选键,我们在选择候选键时要注意的是:它不应该有任何冗余属性。
Example of an Employee table: Employee ( Employee ID, FullName, SSN, DeptID )
员工表示例:Employee(员工ID、全称、SSN、DeptID)
-
Candidate Key: are individual columns in a table that qualifies for uniqueness of all the rows. Here in Employee table EmployeeID & SSN are Candidate keys.
候选键:是表中具有所有行惟一性的单个列。这里的Employee表EmployeeID和SSN是候选键。
-
Primary Key: is the columns you choose to maintain uniqueness in a table. Here in Employee table you can choose either EmployeeID or SSN columns, EmployeeID is preferable choice, as SSN is a secure value.
主键:是您选择在表中保持惟一性的列。在Employee表中,您可以选择EmployeeID或SSN列,EmployeeID是更好的选择,因为SSN是一个安全值。
-
Alternate Key: Candidate column other the Primary column, like if EmployeeID is PK then SSN would be the Alternate key.
备用键:候选列另一个主列,例如如果EmployeeID是PK,那么SSN将是备用键。
-
Super Key: If you add any other column/attribute to a Primary Key then it become a super key, like EmployeeID + FullName is a Super Key.
超级键:如果您向主键添加任何其他列/属性,那么它将成为超级键,就像EmployeeID + FullName是超级键一样。
-
Composite Key: If a table do have a single columns that qualifies for a Candidate key, then you have to select 2 or more columns to make a row unique. Like if there is no EmployeeID or SSN columns, then you can make FullName + DateOfBirth as Composite primary Key. But still there can be a narrow chance of duplicate row.
复合键:如果一个表有一个单独的列,它限定了一个候选键,那么您必须选择2个或更多的列来创建一个唯一的行。例如,如果没有EmployeeID或SSN列,那么可以将FullName + DateOfBirth设置为复合主键。但仍然有很小的可能重复行。
#7
1
Think of a table of vehicles with an integer Primary Key.
考虑一个具有整数主键的车辆表。
The registration number would be a candidate key.
注册号将是候选键。
In the real world registration numbers are subject change so it depends somewhat on the circumstances what might qualify as a candidate key.
在现实世界中,注册号码是主题变更,所以它在某种程度上取决于什么可能符合候选密钥的条件。
#8
0
A primary key is a column (or columns) in a table that uniquely identifies the rows in that table.
主键是表中唯一标识该表中的行的列(或列)。
CUSTOMERS
CustomerNo FirstName LastName
1 Sally Thompson
2 Sally Henderson
3 Harry Henderson
4 Sandra Wellington
For example, in the table above, CustomerNo is the primary key.
例如,在上面的表中,CustomerNo是主键。
The values placed in primary key columns must be unique for each row: no duplicates can be tolerated. In addition, nulls are not allowed in primary key columns.
放置在主键列中的值必须对每一行都是唯一的:不能容忍重复。此外,在主键列中不允许使用null。
So, having told you that it is possible to use one or more columns as a primary key, how do you decide which columns (and how many) to choose?
因此,在告诉您可以使用一个或多个列作为主键之后,您如何决定要选择哪些列(以及多少列)?
Well there are times when it is advisable or essential to use multiple columns. However, if you cannot see an immediate reason to use multiple columns, then use one. This isn't an absolute rule, it is simply advice. However, primary keys made up of single columns are generally easier to maintain and faster in operation. This means that if you query the database, you will usually get the answer back faster if the tables have single column primary keys.
有时,使用多个列是明智的或必需的。但是,如果您看不到使用多个列的直接原因,那么请使用一个列。这不是绝对的规则,只是建议。然而,由单个列组成的主键通常更容易维护,操作也更快。这意味着,如果您查询数据库,如果表中只有一个列主键,您通常会更快地得到答案。
Next question — which column should you pick? The easiest way to choose a column as a primary key (and a method that is reasonably commonly employed) is to get the database itself to automatically allocate a unique number to each row.
下一个问题,你应该选哪一列?选择列作为主键(以及通常使用的方法)的最简单方法是让数据库本身自动为每一行分配唯一的数字。
In a table of employees, clearly any column like FirstName is a poor choice since you cannot control employee's first names. Often there is only one choice for the primary key, as in the case above. However, if there is more than one, these can be described as 'candidate keys' — the name reflects that they are candidates for the responsible job of primary key.
在员工表中,很明显,任何像FirstName这样的列都是糟糕的选择,因为您无法控制员工的名字。通常,主键只有一个选择,如上面所示。但是,如果有多个密钥,则可以将它们描述为“候选密钥”——该名称反映了它们是主密钥负责工作的候选者。
#9
0
If superkey is a big set than candidate key is some smaller set inside big set and primary key any one element(one at a time or for a table) in candidate key set.
如果超键是一个大集合,那么候选键是大集合和主键中任何一个元素(一次一个或一个表)中的一个小集合。
#10
0
First you have to know what is a determinant? the determinant is an attribute that used to determine another attribute in the same table. SO the determinant must be a candidate key. And you can have more than one determinant. But primary key is used to determine the whole record and you can have only one primary key. Both primary and candidate key can consist of one or more attributes
首先你要知道行列式是什么?行列式是用于确定同一表中的另一个属性的属性。所以行列式必须是一个候选键。你可以有多个行列式。但是主键用于确定整个记录,您只能拥有一个主键。主键和候选键都可以由一个或多个属性组成
#11
0
Primary Key
主键
The primary key of a table is a column (or a combination of columns) used as a unique identification of rows in that table. In other words, two different rows in a table may never have the same value in their primary key, and for every row in the table, the primary key must always have one value.The latter means that null values are not allowed in a primary key.
表的主键是一个列(或多个列的组合),用于惟一地标识该表中的行。换句话说,一个表中的两个不同的行在它们的主键中可能永远不会有相同的值,并且对于表中的每一行,主键必须始终有一个值。后者意味着在主键中不允许空值。
We come across primary keys everywhere. For example, the table in which a bank stores data about bank accounts has the column bank account number as a primary key. Similarly, a table in which different cars are registered uses the license plate as primary key
我们到处都能看到主键。例如,银行存储关于银行帐户的数据的表中,列bank account number作为主键。类似地,不同车辆注册的表使用车牌作为主键
Candidate Key
候选关键字
Some tables contain more than one column (or combination of columns) that can act as a primary key. These columns all possess the uniqueness property of a primary key. Here, also, null values are not allowed. These columns are called candidate keys. However, only one is designated as the primary key. Therefore, a table always has at least one candidate key.
有些表包含多个列(或列的组合),它们可以作为主键。这些列都具有主键的惟一性。这里也不允许使用空值。这些列称为候选键。但是,只有一个被指定为主键。因此,表总是至少有一个候选键。
If we assume that passport numbers are also included in a PLAYERS table of a club, that column will be used as the candidate key because passport numbers are unique. Two players can never have the same passport number. This column could also be designated as the primary key.
如果我们假设护照号码也包含在一个俱乐部的球员表中,那一栏将作为候选人的钥匙,因为护照号码是唯一的。两个球员的护照号码永远不会一样。这一列也可以指定为主键。
Alternate Key
替换关键字
A candidate key that is not the primary key of a table is called an alternate key. Zero or more alternate keys can be defined for a specific table. The term candidate key is a general term for all primary and alternate keys.
不是表的主键的候选键称为备用键。可以为特定的表定义零个或多个备用键。“候选关键字”是所有主键和备用键的通用术语。
Foreign Key
外键
A foreign key is a column (or combination of columns) in a table in which the population is a subset of the population of the primary key of a table (this does not have to be another table). Foreign keys are sometimes called referential keys.
外键是表中的列(或列的组合),其中填充是表的主键的填充的子集(这并不一定是另一个表)。外键有时被称为引用键。
#1
76
Candidate Key
– A Candidate Key can be any column or a combination of columns that can qualify as unique key in database. There can be multiple Candidate Keys in one table. Each Candidate Key can qualify as Primary Key.
候选键——候选键可以是任何列,也可以是可以作为数据库中唯一键的列的组合。一个表中可以有多个候选键。每个候选键都可以限定为主键。
Primary Key
– A Primary Key is a column or a combination of columns that uniquely identify a record. Only one Candidate Key can be Primary Key.
主键——主键是一个列,或者是唯一标识记录的列的组合。只有一个候选键可以是主键。
More on this link with example
更多关于这个链接的例子。
#2
19
John Woo's answer is correct, as far as it goes. Here are a few additional points.
吴宇森的答案是正确的。这里有一些额外的要点。
A primary key is always one of the candidate keys. Fairly often, it's the only candidate.
主键总是候选键之一。通常,它是唯一的候选者。
A table with no candidate keys does not represent a relation. If you're using the relational model to help you build a good database, then every table you design will have at least one candidate key.
没有候选键的表不表示关系。如果您正在使用关系模型来帮助您构建一个良好的数据库,那么您设计的每个表将至少有一个候选键。
The relational model would be complete without the concept of primary key. It wasn't in the original presentation of the relational model. As a practical matter, the use of foreign key references without a declared primary key leads to a mess. It could be a logically correct mess, but it's a mess nonetheless. Declaring a primary key lets the DBMS help you enforce the data rules. Most of the time, having the DBMS help you enforce the data rules is a good thing, and well worth the cost.
没有主键的概念,关系模型将是完整的。它不在关系模型最初的表示中。实际上,使用没有声明主键的外键引用会导致混乱。它可能是逻辑上正确的混乱,但它仍然是混乱。声明一个主键可以让DBMS帮助您执行数据规则。大多数情况下,使用DBMS帮助您执行数据规则是一件好事,而且值得付出代价。
Some database designers and some users have some mental confusion about whether the primary key identifies a row (record) in a table or an instance of an entity in the subject matter that the table represents. In an ideal world, it's supposed to do both, and there should be a one-for-one correspondence between rows in an entity table and instances of the corresponding entity.
一些数据库设计人员和一些用户对主键是标识表中的行(记录)还是表中表示的主题内容中的实体实例有一些困惑。在一个理想的世界中,它应该同时执行这两种操作,在实体表中的行和对应实体的实例之间应该有一个一对一的对应关系。
In the real world, things get screwed up. Somebody enters the same new employee twice, and the employee ends up with two ids. Somebody gets hired, but the data entry slips through the cracks in some manual process, and the employee doesn't get an id, until the omission is corrected. A database that does not collapse the first time things get screwed up is more robust than one that does.
在现实世界里,事情搞砸了。有人输入同一名新员工两次,该员工得到两个id。有人被雇佣了,但是数据录入从一些手工过程的裂缝中滑了出来,而员工直到被遗漏的地方被纠正后才会得到一个id。一个不会在第一次出错时崩溃的数据库比一个会崩溃的数据库更健壮。
#3
8
Primary key -> Any column or set of columns that can uniquely identify a record in the table is a primary key. (There can be only one Primary key in the table)
主键——>任何一列或一组列可以惟一地标识表中的记录是主键。(表中只能有一个主键)
Candidate key -> Any column or set of columns that are candidate to become primary key are Candidate key. (There can be one or more candidate key(s) in the table, if there is only one candidate key, it can be chosen as Primary key)
候选键->任何成为主键的候选列或列集都是候选键。(表中可以有一个或多个候选键,如果只有一个候选键,可以选择它作为主键)
#4
6
There is no difference. A primary key is a candidate key. By convention one candidate key in a relation is usually chosen to be the "primary" one but the choice is essentially arbitrary and a matter of convenience for database users/designers/developers. It doesn't make a "primary" key fundamentally any different to any other candidate key.
没有区别。主键是候选键。按照惯例,关系中的一个候选键通常被选择为“主”键,但是这个选择本质上是任意的,对于数据库用户/设计人员/开发人员来说是非常方便的。它不会使“主”键从根本上与任何其他候选键不同。
#5
2
A table can have so many column which can uniquely identify a row. This columns are referred as candidate keys, but primary key should be one of them because one primary key is enough for a table. So selection of primary key is important among so many candidate key. Thats the main difference.
一个表可以有如此多的列,可以惟一地标识行。这些列被称为候选键,但是主键应该是其中的一个,因为一个主键就足够一个表了。因此,在众多候选密钥中,主密钥的选择是非常重要的。的主要区别。
#6
2
A Primary key is a special kind of index in that:
主键是其中一种特殊的索引:
there can be only one;
it cannot be nullable
it must be unique.
Candidate keys are selected from the set of super keys, the only thing we take care while selecting candidate key is: It should not have any redundant attribute.
从超级键的集合中选择候选键,我们在选择候选键时要注意的是:它不应该有任何冗余属性。
Example of an Employee table: Employee ( Employee ID, FullName, SSN, DeptID )
员工表示例:Employee(员工ID、全称、SSN、DeptID)
-
Candidate Key: are individual columns in a table that qualifies for uniqueness of all the rows. Here in Employee table EmployeeID & SSN are Candidate keys.
候选键:是表中具有所有行惟一性的单个列。这里的Employee表EmployeeID和SSN是候选键。
-
Primary Key: is the columns you choose to maintain uniqueness in a table. Here in Employee table you can choose either EmployeeID or SSN columns, EmployeeID is preferable choice, as SSN is a secure value.
主键:是您选择在表中保持惟一性的列。在Employee表中,您可以选择EmployeeID或SSN列,EmployeeID是更好的选择,因为SSN是一个安全值。
-
Alternate Key: Candidate column other the Primary column, like if EmployeeID is PK then SSN would be the Alternate key.
备用键:候选列另一个主列,例如如果EmployeeID是PK,那么SSN将是备用键。
-
Super Key: If you add any other column/attribute to a Primary Key then it become a super key, like EmployeeID + FullName is a Super Key.
超级键:如果您向主键添加任何其他列/属性,那么它将成为超级键,就像EmployeeID + FullName是超级键一样。
-
Composite Key: If a table do have a single columns that qualifies for a Candidate key, then you have to select 2 or more columns to make a row unique. Like if there is no EmployeeID or SSN columns, then you can make FullName + DateOfBirth as Composite primary Key. But still there can be a narrow chance of duplicate row.
复合键:如果一个表有一个单独的列,它限定了一个候选键,那么您必须选择2个或更多的列来创建一个唯一的行。例如,如果没有EmployeeID或SSN列,那么可以将FullName + DateOfBirth设置为复合主键。但仍然有很小的可能重复行。
#7
1
Think of a table of vehicles with an integer Primary Key.
考虑一个具有整数主键的车辆表。
The registration number would be a candidate key.
注册号将是候选键。
In the real world registration numbers are subject change so it depends somewhat on the circumstances what might qualify as a candidate key.
在现实世界中,注册号码是主题变更,所以它在某种程度上取决于什么可能符合候选密钥的条件。
#8
0
A primary key is a column (or columns) in a table that uniquely identifies the rows in that table.
主键是表中唯一标识该表中的行的列(或列)。
CUSTOMERS
CustomerNo FirstName LastName
1 Sally Thompson
2 Sally Henderson
3 Harry Henderson
4 Sandra Wellington
For example, in the table above, CustomerNo is the primary key.
例如,在上面的表中,CustomerNo是主键。
The values placed in primary key columns must be unique for each row: no duplicates can be tolerated. In addition, nulls are not allowed in primary key columns.
放置在主键列中的值必须对每一行都是唯一的:不能容忍重复。此外,在主键列中不允许使用null。
So, having told you that it is possible to use one or more columns as a primary key, how do you decide which columns (and how many) to choose?
因此,在告诉您可以使用一个或多个列作为主键之后,您如何决定要选择哪些列(以及多少列)?
Well there are times when it is advisable or essential to use multiple columns. However, if you cannot see an immediate reason to use multiple columns, then use one. This isn't an absolute rule, it is simply advice. However, primary keys made up of single columns are generally easier to maintain and faster in operation. This means that if you query the database, you will usually get the answer back faster if the tables have single column primary keys.
有时,使用多个列是明智的或必需的。但是,如果您看不到使用多个列的直接原因,那么请使用一个列。这不是绝对的规则,只是建议。然而,由单个列组成的主键通常更容易维护,操作也更快。这意味着,如果您查询数据库,如果表中只有一个列主键,您通常会更快地得到答案。
Next question — which column should you pick? The easiest way to choose a column as a primary key (and a method that is reasonably commonly employed) is to get the database itself to automatically allocate a unique number to each row.
下一个问题,你应该选哪一列?选择列作为主键(以及通常使用的方法)的最简单方法是让数据库本身自动为每一行分配唯一的数字。
In a table of employees, clearly any column like FirstName is a poor choice since you cannot control employee's first names. Often there is only one choice for the primary key, as in the case above. However, if there is more than one, these can be described as 'candidate keys' — the name reflects that they are candidates for the responsible job of primary key.
在员工表中,很明显,任何像FirstName这样的列都是糟糕的选择,因为您无法控制员工的名字。通常,主键只有一个选择,如上面所示。但是,如果有多个密钥,则可以将它们描述为“候选密钥”——该名称反映了它们是主密钥负责工作的候选者。
#9
0
If superkey is a big set than candidate key is some smaller set inside big set and primary key any one element(one at a time or for a table) in candidate key set.
如果超键是一个大集合,那么候选键是大集合和主键中任何一个元素(一次一个或一个表)中的一个小集合。
#10
0
First you have to know what is a determinant? the determinant is an attribute that used to determine another attribute in the same table. SO the determinant must be a candidate key. And you can have more than one determinant. But primary key is used to determine the whole record and you can have only one primary key. Both primary and candidate key can consist of one or more attributes
首先你要知道行列式是什么?行列式是用于确定同一表中的另一个属性的属性。所以行列式必须是一个候选键。你可以有多个行列式。但是主键用于确定整个记录,您只能拥有一个主键。主键和候选键都可以由一个或多个属性组成
#11
0
Primary Key
主键
The primary key of a table is a column (or a combination of columns) used as a unique identification of rows in that table. In other words, two different rows in a table may never have the same value in their primary key, and for every row in the table, the primary key must always have one value.The latter means that null values are not allowed in a primary key.
表的主键是一个列(或多个列的组合),用于惟一地标识该表中的行。换句话说,一个表中的两个不同的行在它们的主键中可能永远不会有相同的值,并且对于表中的每一行,主键必须始终有一个值。后者意味着在主键中不允许空值。
We come across primary keys everywhere. For example, the table in which a bank stores data about bank accounts has the column bank account number as a primary key. Similarly, a table in which different cars are registered uses the license plate as primary key
我们到处都能看到主键。例如,银行存储关于银行帐户的数据的表中,列bank account number作为主键。类似地,不同车辆注册的表使用车牌作为主键
Candidate Key
候选关键字
Some tables contain more than one column (or combination of columns) that can act as a primary key. These columns all possess the uniqueness property of a primary key. Here, also, null values are not allowed. These columns are called candidate keys. However, only one is designated as the primary key. Therefore, a table always has at least one candidate key.
有些表包含多个列(或列的组合),它们可以作为主键。这些列都具有主键的惟一性。这里也不允许使用空值。这些列称为候选键。但是,只有一个被指定为主键。因此,表总是至少有一个候选键。
If we assume that passport numbers are also included in a PLAYERS table of a club, that column will be used as the candidate key because passport numbers are unique. Two players can never have the same passport number. This column could also be designated as the primary key.
如果我们假设护照号码也包含在一个俱乐部的球员表中,那一栏将作为候选人的钥匙,因为护照号码是唯一的。两个球员的护照号码永远不会一样。这一列也可以指定为主键。
Alternate Key
替换关键字
A candidate key that is not the primary key of a table is called an alternate key. Zero or more alternate keys can be defined for a specific table. The term candidate key is a general term for all primary and alternate keys.
不是表的主键的候选键称为备用键。可以为特定的表定义零个或多个备用键。“候选关键字”是所有主键和备用键的通用术语。
Foreign Key
外键
A foreign key is a column (or combination of columns) in a table in which the population is a subset of the population of the primary key of a table (this does not have to be another table). Foreign keys are sometimes called referential keys.
外键是表中的列(或列的组合),其中填充是表的主键的填充的子集(这并不一定是另一个表)。外键有时被称为引用键。