How to find a candidate key

谁说我不能喝 提交于 2019-12-02 13:31:56

问题


I have a relation A,B,C,D,E with functional dependencies

1) A->BC

2) CD->E

3) B->D

4) E->A

Using 1 gives A,D,E and then using 4 will make it D,E

Using 2 gives A,B,C,D and then using 3 gives A,B,C and using 1 gives A

Using 2 gives A,B,C,D and using 1 gives A,D

Using 4 gives B,C,D,E and using 2 gives B,C,D and using 3 gives B,C

Using 3 gives A,B,C,E and using 1 gives A,E and using 4 gives E

So I would have 5 super keys? (A, E, AD, BC, DE). And from my super keys I would pick the unique ones.

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

Would that be my super key? Or would it just be E?


回答1:


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.

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)

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)

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


来源:https://stackoverflow.com/questions/43465696/how-to-find-a-candidate-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!