如何找到候选密钥 [英] How to find a candidate key
问题描述
我有一个具有功能依赖性的关系A,B,C,D,E
I have a relation A,B,C,D,E with functional dependencies
1)A-> BC
2)CD-> E
3)B-> D
4)E-> A
使用1给出A,D,E,然后使用4给出D,E
Using 1 gives A,D,E and then using 4 will make it D,E
使用2给出A,B,C,D,然后使用3给出A,B,C,使用1给出A
Using 2 gives A,B,C,D and then using 3 gives A,B,C and using 1 gives A
使用2给出A,B,C,D,使用1给出A,D
Using 2 gives A,B,C,D and using 1 gives A,D
使用4给出B,C,D,E,使用2给出B,C,D,使用3给出B,C
Using 4 gives B,C,D,E and using 2 gives B,C,D and using 3 gives B,C
使用3给出A,B,C,E,使用1给出A,E,使用4给出E
Using 3 gives A,B,C,E and using 1 gives A,E and using 4 gives E
所以我会有5个超级键? (A,E,AD,BC,DE).从我的超级键中,我会选择唯一的键.
So I would have 5 super keys? (A, E, AD, BC, DE). And from my super keys I would pick the unique ones.
因为我可以从E中获得A,所以我可以删除A和AD(因为DE相同),并且因为我可以从A中获得BC,所以我可以将其删除,所以我就离开了
Since I can get A from E, I can remove A and AD(since DE is the same) and since I can get BC from A I can remove that so I am left with
E,DE
那是我的超级钥匙吗?还是只是E?
Would that be my super key? Or would it just be E?
推荐答案
根据定义,关系的候选键K是确定所有其他属性的一组属性,因此我们不能从中删除任何属性而不会丢失此属性属性.
By definition, a candidate key K of a relation is a set of attributes that determines all the others and such that we cannot remove any attribute from it without losing this property.
要查找关系的所有键,如果不遵循正式算法,则可以从检查FD的每个行列式开始,并通过计算其关闭来查看它是(超级还是候选)键.例如,从A开始,您可以找到:
To find all the keys of the relation, if you do not follow a formal algorithm, then you could start by checking from each determinant of the FDs and see if this is a (super or candidate) key, by calculating its closure. For instance, starting from A, you can find:
A+ = A
= ABC (by using 1)
= ABCD (by using 3)
= ABCDE (by using 2)
因此,A确定所有属性,因此是候选键(而不是严格的超级键,因为您无法从中删除任何属性!)
So, A determines all the attributes and for this reason is a candidate key (and not a strict superkey, since you cannot remove any attribute from it!)
计算其他行列式的闭包,您会发现:
Calculating the closures of the other determinants, you can find that:
CD+ = ABCDE (candidate key, since C+ and D+ do not contain all the attributes)
B+ = BD (not a key)
E+ = ABCDE (candidate key)
现在,您具有三个候选键A,E和CD.并且由于B仅确定D,因此我们可以尝试向其添加某些内容以查看其是否可以作为键的一部分.我们不添加A或E,因为它们已经是键,而我们不添加D,因为它已经由B决定(因此拥有它肯定会产生超级键).所以我们尝试C:
Now you have three candidate keys, A, E, and CD. And since B determines only D, we could try to add something to it to see if it can be part of a key. We do not add A, or E, since they are already keys, and we do not add D since it is already determinated by B (so that having it will produce surely a superkey). So we try C:
BC+ = ABCDE (candidate key, since B+ and C+ do not contain all the attributes)
因此,最后,我们可以说该关系具有四个(并且只有四个)候选键:
So, finally, we can say that the relation has four (and only four) candidate keys:
A
BC
CD
E
这篇关于如何找到候选密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!