finding largest number of candidate keys that a relation has?

喜夏-厌秋 提交于 2019-12-11 04:58:04

问题


I am trying to solve this question which has to do with candidate keys in a relation. This is the question:

    Consider table R with attributes A, B, C, D, and E. What is the largest number of
candidate keys that R could simultaneously have?

the answer is 10 but i have no clue how it was done, nor how does the word simultaneously plays into effect when calculating the answer.


回答1:


Sets that are not subsets of other sets.
For example {A-B} and {A,B,C} can't be candidates keys simultaneously, because {A,B} is a subset of {A,B,C}.
Combinations of 2 attributes or 3 attributes generates the maximum number of simultaneous candidates keys.
See how the 3 attributes sets are actually complements of the 2 attributes sets, e.g. {C,D,E} is the complement of {A,B}.

         2               3    
     attributes      attributes
       sets            sets

   1.  {A,B}    -     {C,D,E}
   2.  {A,C}    -     {B,D,E}
   3.  {A,D}    -     {B,C,E}
   4.  {A,E}    -     {B,C,D}
                -     
   5.  {B,C}    -     {A,D,E}
   6.  {B,D}    -     {A,C,E}
   7.  {B,E}    -     {A,C,D}
                -     
   8.  {C,D}    -     {A,B,E}
   9.  {C,E}    -     {A,B,D}
                -     
   10. {D,E}    -     {A,B,C}

If I would take sets of a single attribute I would have only 4 options

{A},{B},{C},{D}

Any set with more than 1 element will contain one of the above and therefore will not be qualified.

If I would take sets of 4 attributes I would have only 4 options

{A,B,C,D},{A,B,C,E},{A,B,D,E},{B,C,D,E}

Any set with more than 4 element will contain one of the above and therefore will not be qualified. Any set with less than 4 element will be contained by one of the above and therefore will not be qualified.

etc.




回答2:


For 5 keys, it is probably best to do this by brute force. Understanding the ideas is more important than the calculation (DuDu/David gives a good example of 10 candidate keys, showing that a set of 10 keys is possible so the maximum is at least this large).

What is the idea? A candidate key is a combination of attributes that is unique. So, if A is unique, then A with any other column is also unique. One set of candidate keys is simply:

  • A
  • B
  • C
  • D
  • E

If each of these are unique, then any combination of keys is going to contain at least one of these attributes and the combination will also be unique. Hence, the uniqueness of these five would imply the uniqueness of any other combination.

5 is not the largest number of candidate keys with this property.

It gets a bit more complicated. If {A, B, C, D, E} is unique (and no subset is a candidate key), then there is exactly 1 candidate key. Rearranging the columns doesn't change the set (sets are unordered).

One thing we might postulate is that the biggest set of candidate keys has keys all of the same length. This is in fact true. Why? Well, if we have a set of keys that are of different lengths, we can lengthen the shorter ones by adding arbitrary attributes and still have a maximal set.

So, you only need to consider subsets of 1, 2, 3, 4, and 5 keys, exactly. When you work it out, you will find that the maximum numbers are:

5 10 10 5 1

You can add a "1" to the beginning and you may recognize the pattern. This is a row from Pascal's Triangle. This observation (well, and the related proof) actually makes it easy to determine the maximum value for any given n.

Incidentally, the sets of length 3 are:

A B C
A B D
A B E
A C D
A C E
A D E
B C D
B C E
B D E
C D E


来源:https://stackoverflow.com/questions/41107632/finding-largest-number-of-candidate-keys-that-a-relation-has

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