database 超级键和候选键有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4519825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are the differences between a superkey and a candidate key?
提问by Dinesh Kumar
What are the differences between a super key and a candidate key? I have already referred to wiki,dotnet spider and also Database Concepts 4th edition book. But I am unable to understand the concept. Can anyone please explain it with proper example?
超级键和候选键有什么区别?我已经提到了 wiki、dotnet spider 和 Database Concepts 第 4 版书籍。但我无法理解这个概念。任何人都可以用适当的例子解释它吗?
回答by Quassnoi
Candidate key
is a super key
from which you cannot remove any fields.
Candidate key
是一个super key
不能从中删除任何字段的字段。
For instance, a software release can be identified either by major/minor version, or by the build date (we assume nightly builds).
例如,可以通过主要/次要版本或构建日期(我们假设每晚构建)来识别软件版本。
Storing date in three fields is not a good idea of course, but let's pretend it is for demonstration purposes:
将日期存储在三个字段中当然不是一个好主意,但让我们假设它是出于演示目的:
year month date major minor
2008 01 13 0 1
2008 04 23 0 2
2009 11 05 1 0
2010 04 05 1 1
So (year, major, minor)
or (year, month, date, major)
are super keys (since they are unique) but not candidate keys, since you can remove year
or major
and the remaining set of columns will still be a super key.
所以(year, major, minor)
or(year, month, date, major)
是超级键(因为它们是唯一的)但不是候选键,因为您可以删除year
ormajor
并且剩余的列集仍然是超级键。
(year, month, date)
and (major, minor)
are candidate keys, since you cannot remove any of the fields from them without breaking uniqueness.
(year, month, date)
和(major, minor)
是候选键,因为您不能在不破坏唯一性的情况下从中删除任何字段。
回答by Jonathan Leffler
A super key is any combination of columns that uniquely identifies a row in a table. A candidate key is a super key which cannot have any columns removed from it without losing the unique identification property. This property is sometimes known as minimality or (better) irreducibility.
超级键是唯一标识表中行的列的任意组合。候选键是一个超级键,它不能在不丢失唯一标识属性的情况下从中删除任何列。此属性有时称为极小性或(更好的)不可约性。
A super key ≠ a primary key in general. The primary key is simply a candidate key chosen to be the main key. However, in dependency theory, candidate keys are important and the primary key is not more important than any of the other candidate keys. Non-primary candidate keys are also known as alternative keys.
一般而言,超级键≠主键。主键只是一个被选为主键的候选键。然而,在依赖理论中,候选键很重要,主键并不比任何其他候选键更重要。非主候选键也称为替代键。
Consider this table of Elements:
考虑这个元素表:
CREATE TABLE elements
(
atomic_number INTEGER NOT NULL PRIMARY KEY
CHECK (atomic_number > 0 AND atomic_number < 120),
symbol CHAR(3) NOT NULL UNIQUE,
name CHAR(20) NOT NULL UNIQUE,
atomic_weight DECIMAL(8,4) NOT NULL,
period SMALLINT NOT NULL
CHECK (period BETWEEN 1 AND 7),
group CHAR(2) NOT NULL
-- 'L' for Lanthanoids, 'A' for Actinoids
CHECK (group IN ('1', '2', 'L', 'A', '3', '4', '5', '6',
'7', '8', '9', '10', '11', '12', '13',
'14', '15', '16', '17', '18')),
stable CHAR(1) DEFAULT 'Y' NOT NULL
CHECK (stable IN ('Y', 'N'))
);
It has three unique identifiers - atomic number, element name, and symbol. Each of these, therefore, is a candidate key. Further, unless you are dealing with a table that can only ever hold one row of data (in which case the empty set (of columns) is a candidate key), you cannot have a smaller-than-one-column candidate key, so the candidate keys are irreducible.
它具有三个唯一标识符——原子序数、元素名称和符号。因此,这些中的每一个都是候选键。此外,除非您正在处理一个只能保存一行数据的表(在这种情况下,空集(列)是候选键),否则您不能拥有小于一列的候选键,因此候选键是不可约的。
Consider a key made up of { atomic number, element name, symbol }. If you supply a consistent set of values for these three fields (say { 6, Carbon, C }), then you uniquely identify the entry for an element - Carbon. However, this is very much a super key that is not a candidate key because it is not irreducible; you can eliminate any two of the three fields without losing the unique identification property.
考虑一个由{原子序数、元素名称、符号}组成的键。如果您为这三个字段提供一组一致的值(例如 { 6, Carbon, C }),则您可以唯一标识元素的条目 - Carbon。然而,这在很大程度上是一个不是候选键的超级键,因为它不是不可约的;您可以消除三个字段中的任何两个,而不会丢失唯一标识属性。
As another example, consider a key made up of { atomic number, period, group }. Again, this is a unique identifier for a row; { 6, 2, 14 } identifies Carbon (again). If it were not for the Lanthanoids and Actinoids, then the combination of { period, group } would be unique, but because of them, it is not. However, as before, atomic number on its own is sufficient to uniquely identify an element, so this is a super key and not a candidate key.
再举一个例子,考虑一个由 { atomic number, period, group } 组成的键。同样,这是一行的唯一标识符;{ 6, 2, 14 } 标识碳(再次)。如果不是镧系元素和锕系元素,那么 { period, group } 的组合将是唯一的,但由于它们,它不是。然而,和以前一样,原子序数本身就足以唯一标识一个元素,所以这是一个超级键而不是候选键。
回答by Shakti Singh
super key is the combination of fields by which the row is uniquely identified and the candidate key is the minimal super key.
超级键是唯一标识行的字段组合,候选键是最小超级键。
回答by K.Suthagar
A Super keyis a set or one of more columns to uniquely identify rows in a table.
一个Super键是一组或多列唯一标识表中的行之一。
Candidate keysare 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. That's the reason they are also termed as minimal super key.
候选键是从一组超级键中选择出来的,我们在选择候选键时唯一要注意的是:它不应该有任何冗余属性。这就是它们也被称为最小超级键的原因。
In Employee
table there are Three Columns : Emp_Code,Emp_Number,Emp_Name
在Employee
表中有三列:Emp_Code,Emp_Number,Emp_Name
Super keys:
超级钥匙:
All of the following sets are able to uniquely identify rows of the employee table.
以下所有集合都能够唯一标识员工表的行。
{Emp_Code}
{Emp_Number}
{Emp_Code, Emp_Number}
{Emp_Code, Emp_Name}
{Emp_Code, Emp_Number, Emp_Name}
{Emp_Number, Emp_Name}
Candidate Keys:
候选键:
As I stated above, they are the minimal super keys with no redundant attributes.
如上所述,它们是没有冗余属性的最小超级键。
{Emp_Code}
{Emp_Number}
Primary key:
首要的关键:
Primary key is being selected from the sets of candidate keys by database designer. So Either {Emp_Code}
or {Emp_Number}
can be the primary key.
数据库设计者正在从候选键集中选择主键。所以无论是{Emp_Code}
或{Emp_Number}
可能是主键。
回答by Nusret Can Pekcetin
A super keyof an entity set is a set of one or more attributes whose values uniquely determine each entity.
甲超级密钥的实体集的是一组的值唯一地确定每个实体的一个或多个属性。
A candidate keyof an entity set is a minimal super key.
一个候选键的实体集是最小的超级关键。
Let's go on with customer, loan and borrower sets that you can find an image from the link ≥≥ 1
让我们继续使用客户、贷款和借款人集,您可以从链接中找到图像 ≥ ≥ 1
- Rectangles represent entity sets ≥≥ 1
- Diamonds represent relationship set ≥≥ 1
- Elipses represent attributes ≥≥ 1
- Underline indicates Primary Keys ≥≥ 1
customer_idis the candidate keyof the customer set, loan_numberis the candidate keyof the loan set.
CUSTOMER_ID是候选键顾客设置的loan_number是候选键贷款集合。
Although several candidate keys may exist, one of the candidate keys is selected to be the primary key.
尽管可能存在多个候选键,但选择候选键之一作为主键。
Borrower set is formed customer_idand loan_numberas a relationship set.
借款人集由customer_id和loan_number作为关系集形成。
回答by Paolo Maresca
Basically, a Candidate Keyis a Super Keyfrom which no more Attribute can be pruned.
基本上,候选键是一个超级键,不能从中修剪更多的属性。
A Super Keyidentifies uniquely rows/tuples in a table/relation of a database. It is composed by a set of attributes that combined can assume values unique over the rows/tuples of a table/relation. A Candidate Keyis built by a Super Key, iteratively removing/pruning non-key attributes, keeping an invariant: the newly created Keystill need to uniquely identifies the rows/tuples.
一个超级中心唯一地标识行/元组在数据库中的表/关系。它由一组属性组成,这些属性组合起来可以假定在表/关系的行/元组中具有唯一性的值。一个候选键是由一个超级中心建成,反复删除/修剪非关键属性,保持不变:新创建的关键仍然需要唯一标识行/元组。
A Candidate Keymight be seen as a minimal Super Key, in terms of attributes.
一个候选键可以被看作是一个最小的超级中心,在属性方面。
Candidate Keys can be used to reference uniquely rows/tuples but from the RDBMS engine perspective the burden to maintain indexes on them is far heavier.
候选键可用于唯一地引用行/元组,但从 RDBMS 引擎的角度来看,维护它们的索引的负担要重得多。
回答by Habeeb Perwad
One candidate key is chosen as the primary key. Other candidate keys are called alternate keys.
选择一个候选键作为主键。其他候选键称为备用键。
回答by minto5z
Super Key:A superkey is any set of attributes for which the values are guaranteed to be unique for all possible set of tuples in a table at all time.
超级键:超级键是任何一组属性,其值对于表中所有可能的元组组始终是唯一的。
Candidate Key:A candidate key is a 'minimal' super key meaning the smallest subset of superkey attribute which is unique.
候选键:候选键是“最小”超级键,表示唯一的超级键属性的最小子集。
回答by sonal
Superkey :A set of attributes or combination of attributes which uniquely identify the tuple in a given relation . Superkey have two properties uniqueness and reducible set
超键:一组属性或属性组合,唯一标识给定关系中的元组。Superkey 具有唯一性和可约集两个属性
Candidate key: Minimal set of superkey which have following two properties: uniqueness and irreducible set or attribute
候选键:具有以下两个属性的最小超键集:唯一性和不可约集或属性
回答by Rohit Das
Super key: super key is a set of atttibutes in a relation(table).which can define every tupple in the relation(table) uniquely.
超级键:超级键是关系(表)中的一组属性。它可以唯一定义关系(表)中的每个元组。
Candidate key: we can say minimal super key is candidate key. Candidate is the smallest sub set of super key. And can uniquely define each and every tupple.
候选键:我们可以说最小的超级键是候选键。Candidate 是 super key 的最小子集。并且可以唯一地定义每一个元组。