0

hi am recently learning about SQL and keys.

i am wondering if there is n attributes to a relation table how many possible Candidate keys would there be?

boredj
  • 1
  • 2

1 Answers1

0

Each subset of the attributes of the relations can be a candidate key. So the number is equal to the number of the subsets of a set of n elements, that is 2n

Edited

The original answer was 2n-1, but it significant also to have the empty (or niladic) key. A key of such kind implies that the relation has only one tuple. Thanks to @ErwinSmout for the clarification.

Renzo
  • 26,848
  • 5
  • 49
  • 61
  • Unfortunately, we must NOT exclude the empty set. SQL does not support empty keys, but that merely means SQL is not fully relational. It might even be that some SQL products effectively do support empty (niladic) keys. – Erwin Smout Mar 21 '16 at 11:00
  • for this question would a candidate key be the number of super keys aswell? i thought that a candidate key would just be any of the attributes themselves, because that is minimal number of keys required to identify the row. (or would that be a assumption?) – boredj Mar 21 '16 at 21:37
  • @boredj, If we are talking about the number of potential candidate keys, this is equal to 2^n-1 or 2^n according to a comment. If we are talking about the maximum number of candidate keys that can be present at the same time in the same table, this is a different question. In the first case, a candidate key can have any number of attributes. – Renzo Mar 21 '16 at 21:47
  • @ErwinSmout , it seems to me that a niladic key implies a relation with duplicates, and this is permitted in SQL, but not allowed in the Relational Data Model. – Renzo Mar 21 '16 at 21:50
  • No, a niladic key does not permit duplicates. How on earth did you arrive at such an insane conclusion ? – Erwin Smout Mar 22 '16 at 08:11
  • @ErwinSmout, ok. my mistake, I was confusing niladic keys with no keys, you are right, a niladic key implies that the relation can have only a tuple. So it is possible both in Relational Model as in SQL. However I'm not aware of any database system that has this capability, a part from a discussion in a [PostgreSQL forum](http://www.postgresql.org/message-id/515091A8.9040900@darrenduncan.net) – Renzo Mar 22 '16 at 09:28