Superkey,Minimal key,Primary key和Candidate key
A superkey is a nonempty subset X of relational schema R = (A1 , … , An ) such that for any two rows t1 , t 2in a relational table created over a relational schema R t 1[X] ≠ t2 [X]
简而言之,如果X是R的一个超键,那么X→ A1 , … , An.
A minimal key is a superkey K with an additional property such that removal of any attribute from K will cause K not to be a superkey
最小键是超键的子集,它比起超键有一个附加条件就是如果在超键中删除任何一个属性就会导致它不再是超键。
例如,一个关系模式:DRIVER(licence#, employee#, first-name, last-name)
有两个最小键(licence#) and (employee#)
但是它可以有很多个超键:
==(licence#) ,(employee#) , (licence#, employee#) , (licence#, first-name), (licence#, last-name) , (licence#, first-name, last-name)==等等。
A primary key is an arbitrarily selected minimal key.
A candidate key is any other minimal key which is not a primary key.
举例:
DRIVER(licence#, employee#, first-name, last-name)
has a primary key (licence#) and a candidate key (employee#) 或者反过来也成立。
Attributes
属性
A prime attribute is an attribute from relational schema R which is a member of at least one candidate key in R.
A nonprime attribute is an attribute which is not prime.
举例而言,
licence# and employee# are prime attributes in a relational schema.
DRIVER(licence#, employee#, first-name, last-name)
而first-name and last-name are nonprime attributes in a relational schema.
Full and partial functional dependencies
完全和部分函数依赖关系
如果存在一个完全依赖函数X→Y,在X中删除任何属性都会导致X无法→Y
部分函数依赖为 不是完全函数依赖的函数依赖。
ENROLMENT(student#, subject#, edate, grade)
student#, subject#, edate →grade是完全依赖函数因为不能从函数依赖项的左侧删除任何属性student#、subject#、date,使其仍然有效。
DRIVER(licence#, employee#, first-name, last-name)
licence#, employee# →first-name, last-name 是部分函数依赖,因为 licence#或employee# 两者删除任何一个属性都会是函数依然成立,因为 licence#和employee# 都是最小键。
0NT & 1NF
1NF: A relational table that has no multivalued attributes and composite attributes
没有多值属性和复合属性的关系表
2NF
如果R中的每个nonprime属性A在功能上完全依赖于模式R的主键,那么关系模式R就属于第二范式(2NF)。
Example
INVENTORY(part, quantity, warehouse,warehouse address)
warehouse → warehouse-address
part, warehouse → quantity
通过依赖关系变换,我们得到
part, warehouse → warehouse-address, quantity
它在INVENTORY中是有效的,并且覆盖了整个关系模式的所有属性,因此它的左手边是一个最小键(part, warehouse)
因此关系模式
INVENTORY(part, quantity, warehouse,warehouse address) 不是在2NF因为 nonprime属性warehouse-address 依赖于部分主键(part, warehouse)中的
// warehouse → warehouse-address.
如果关系模式中的所有最小键只包含一个属性(单个属性键),那么这样的模式总是在2NF中。
这是因为架构中的任何非主属性都不依赖于键的一部分(因为每个键都包含
只有一个属性!
把不满足2NF的范式分解为2NF
3NF
回顾传递函数依赖
3NF定义
如果关系模式R属于2NF,并且R的nonprime属性没有传递依赖于主键,那么它就属于第三范式(3NF)。
Relational schema R is in 3NF if it is in 2NF and no nonprime attribute of R is transitively dependent on the primary key
Example
把不满足3NF的范式分解为3NF
一个zip-code可以唯一确定一个city
但是city不能唯一确定zip-code
BCNF
假设函数依赖项都在X→A在R中有效,并且无论何时都满足X是R的超键,那么关系模式R就是Boyce-Codd范式(BCNF)。
Example
把不满足BCNF的范式分解为BCNF
至多包含两个属性的关系架构满足?NF
Every relational schema, which consists of at most 2 attributes is always in BCNF、3NF、2NF、1NF。
进行范式分解的步骤
非主属性city不是完全函数依赖于(s#,p#),因为删除p#后仍然有s#→city
也可以这么分解
另一种方法:
总结
NF | 定义 | 记忆点 |
---|---|---|
1NF | A relational table that has no multivalued attributes and composite attributes | 不能有嵌套属性 |
2NF | Every nonprime attribute A in R is fully functionally dependent on a primary key of schema R | 要完全依赖 |
3NF | It is in 2NF and no nonprime attribute of R is transitively dependent on the primary key | 不能有传递依赖 |
BCNF | A relational schema R is in BCNF if whenever functional dependency x →A holds in R then X is a superkey in R | FD的左侧要是超键 |