SQL 行列式和候选键是相同的还是不同的东西?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16706637/
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
Are Determinants and Candidate Keys same or different things?
提问by Aparan
HereI found this:
在这里我发现了这个:
Definition:A determinant in a database table is any attribute that you can use to determine the values assigned to other attribute(s) in the same row.
Examples:Consider a table with the attributes employee_id, first_name, last_name and date_of_birth. In this case, the field employee_id determines the remaining three fields. The name fields do not determine the employee_id because the firm may have more than one employee with the same first and/or last name. Similarly, the DOB field does not determine the employee_id or the name fields because more than one employee may share the same birthday.
定义:数据库表中的决定因素是可用于确定分配给同一行中其他属性的值的任何属性。
示例:考虑一个具有 employee_id、first_name、last_name 和 date_of_birth 属性的表。在这种情况下,employee_id 字段决定了剩余的三个字段。姓名字段不能确定员工 ID,因为公司可能有多个员工的名字和/或姓氏相同。类似地,DOB 字段不能确定employee_id 或name 字段,因为可能有多个雇员具有相同的生日。
Isn't the definition applicable for candidate keys too?
该定义是否也适用于候选键?
回答by Ross Presser
From my understanding, a determinant may not be a candidate key if the table is not fully normalized. In fact, the word determinant is used when describing the process of taking non-normal data to a more useful, normalized form.
根据我的理解,如果表没有完全规范化,行列式可能不是候选键。事实上,在描述将非正态数据转化为更有用的规范化形式的过程时,会使用行列式一词。
Consider this (obviously non-normal) table:
考虑这个(显然是非正常的)表:
CREATE TABLE US_Address (
AddressID int,
Streetline varchar(80),
City varchar(80),
State char(2),
ZIP char(5),
StateName varchar(80),
StateTax DECIMAL(5,2)
)
State is a determinant for StateName and StateTax, but it is not a candidate key for the row. Proper normalization, would therefore move StateName and StateTax out of the US_Address table and into a States table.
State 是 StateName 和 StateTax 的决定因素,但它不是行的候选键。因此,适当的规范化会将 StateName 和 StateTax 从 US_Address 表中移到 State 表中。
See herefor more information.
请参阅此处了解更多信息。
回答by philipxy
TL;DRNo, "determinant" and "candidate key" are not the same concept. A determinant is of a FD. A CK is of a table. We can also reasonably say sloppily that a CK is a determinant (of a FD) of its table since it determines every column & column set in it.
TL;DR不,“行列式”和“候选键”不是同一个概念。行列式属于FD。一个 CK 是一个表。我们也可以合理地说,CK 是其表的决定因素(FD 的),因为它决定了其中的每一列和列集。
All the following terms/concepts are defined in parallel for table valuesand variables.A table variable has an instance of a FD (functional dependency), determinant, superkey, CK (candidate key) or PK (primary key) (in the variable sense) when every table value that can arise for it in the given business/application has that instance (in the table sense).
以下所有术语/概念都是为表值和变量并行定义的。表变量具有 FD(函数依赖)、行列式、超键、CK(候选键)或 PK(主键)(在变量意义上)的实例,当在给定业务/应用程序中可能出现的每个表值时有那个实例(在表格意义上)。
For sets of columns X and Y we can write X -> Y. We say that X is the determinant/determining setand Y is the determined setof/in functional dependency(FD) X -> Y.
对于列 X 和 Y 的集合,我们可以写X -> Y。我们说 X 是行列式/确定集,Y 是函数依赖( FD) X -> Y的确定集。
We say X functionally determinesY and Y is functionally determined byX. We say X is the determinantof X -> Y. In {C} -> Y we say C functionally determinesY. In X -> {C} we say X functionally determinesC. When X is a superset of Y we say X -> Y is trivial.
我们说 X 在功能上决定了Y,Y在功能上由X决定。我们说 X 是X -> Y的决定因素。在 {C} -> Y 中,我们说 C 在功能上决定了Y。在 X -> {C} 中,我们说 X功能上确定C。当 X 是 Y 的超集时,我们说 X -> Y is trivial。
We say X -> Y holds intable T when each subrow value for X only appears with the one particular subrow value for Y.Or we say X -> Y is a FD of/inT. When X is a determinant of some FD in table T we say X is a determinant of/inT.Every trivial FD of a table holds in it.
当 X 的每个子行值仅与 Y 的一个特定子行值一起出现时,我们说 X -> Y在表 T 中成立。或者我们说 X -> Y 是T的/在T 中的 FD 。当 X 是某个 FD 的行列式表T,我们说X是/的行列式T.每一个平凡的FD的表在它成立。
A superkeyof a table T is a set of columns that functionally determines every column. A candidate key(CK) is a superkey that contains no smaller superkey.We can pick one CK as primary key(PK) and then call the other CKs alternate keys(AKs). A column is primewhen it is in some CK.
甲超密钥一个表T的是一组列在功能上确定每一列。一个候选键(CK)是不包含更小的超密钥一个超密钥。我们可以选择一个 CK 作为主键(PK),然后调用其他 CK 的备用键(AKs)。当一个列在某个 CK 中时它是素数。
Note that a determinant can be of a FDor, sloppily, of (a FD that holds in) a table. Every CK is a determinant of its table.(But then, in a table everyset of columns is a determinant: of itself, trivially. And similarly everycolumn.)
请注意,行列式可以是FD 的,或者草率地属于(包含在表中的 FD)。每个 CK 都是其表的决定因素。(不过,在一个表中的每列集是一个决定因素:本身,平凡而同样每列。)
(These definitions do not depend on normalization. FDs and CKs of a table are used in normalizing it. A table is in BCNF when every determinant of a non-trivial FDthat holds in it is a superkey.)
(这些定义不依赖于归一化。的FD和表的CKS是在归一化其使用。表是在当BCNF每行列式非平凡FD的是,在它保持为超密钥。)
SQL tables are not relations and SQL operators are not their relational/mathematical counterparts.Among other things, SQL has duplicate rows, nulls & a kind of 3-valued logic. But although you can borrow terms and give them SQL meanings, you can't just substitute those meanings into other RM definitions or theorems and get something sensible or true. So we must convert an SQL design to a relational design, apply relational notions, then convert back to SQL. There are special cases where we can do certain things directly in SQL because we know what would happen if we did convert, apply & convert back.
SQL 表不是关系,SQL 运算符也不是它们的关系/数学对应物。除其他外,SQL 具有重复行、空值和一种 3 值逻辑。但是,尽管您可以借用术语并赋予它们 SQL 含义,但您不能仅仅将这些含义替换为其他 RM 定义或定理并获得一些合理或正确的信息。因此,我们必须将 SQL 设计转换为关系设计,应用关系概念,然后再转换回 SQL。在某些特殊情况下,我们可以直接在 SQL 中执行某些操作,因为我们知道如果我们进行了转换、应用和转换回来会发生什么。
回答by displayName
- A primary key or any candidate key is also a determinant while the opposite is not true.
- A determinant can uniquely determine one or more attributes in the row.
- A candidate key can uniquely determine the entire row.
- 主键或任何候选键也是决定因素,反之则不然。
- 行列式可以唯一地确定行中的一个或多个属性。
- 一个候选键可以唯一地确定整行。
Taking an example from here, let there be a table with following columns:
以这里为例,假设有一个包含以下列的表格:
Customer #, Name, Address, Credit, Sales Rep #, Sales Rep Name
客户编号、姓名、地址、信用、销售代表编号、销售代表姓名
and let's say that the Sales Rep #
can uniquely determine the Sales Rep Name
. Thus, Sales Rep #
is a determinant for Sales Rep Name
but is not a candidate key for this table.
并且假设Sales Rep #
可以唯一地确定Sales Rep Name
。因此,Sales Rep #
是Sales Rep Name
该表的决定因素但不是候选键。