Superkey VS. Candidate Key

What are the differences between a superkey and a candidate key?

Candidate key is a super key from which you cannot remove any fields.

For instance, a software release can be identified either by major/minor version, or by the build date (we assume nightly builds).

Storing date in three fields is not a good idea of course, but let's pretend it is for demonstration purposes:

year  month date  major  minor
2008 01 13 0 1
2008 04 23 0 2
2009 11 05 1 0
2010 04 05 1 1

So (year, major, minor) or (year, month, date, major) are super keys (since they are unique) but not candidate keys, since you can remove year or major and the remaining set of columns will still be a super key.

(year, month, date) and (major, minor) are candidate keys, since you cannot remove any of the fields from them without breaking uniqueness.

Differences between key, superkey, minimal superkey, candidate key and primary key

Here I copy paste some of the information that I have collected

Key
A key is a single or combination of multiple fields. Its purpose is to access or retrieve data rows from table according to the requirement. The keys are defined in tables to access or sequence the stored data quickly and smoothly. They are also used to create links between different tables.

Types of Keys

Primary Key
The attribute or combination of attributes that uniquely identifies a row or record in a relation is known as primary key.

Secondary key
A field or combination of fields that is basis for retrieval is known as secondary key. Secondary key is a non-unique field. One secondary key value may refer to many records.

Candidate Key or Alternate key
A relation can have only one primary key. It may contain many fields or combination of fields that can be used as primary key. One field or combination of fields is used as primary key. The fields or combination of fields that are not used as primary key are known as candidate key or alternate key.

Composite key or concatenate key
A primary key that consists of two or more attributes is known as composite key.

Sort Or control key
A field or combination of fields that is used to physically sequence the stored data called sort key. It is also known s control key.

A superkey is a combination of attributes that can be uniquely used to identify a database record. A table might have many superkeys. Candidate keys are a special subset of superkeys that do not have any extraneous information in them.

Example for super key:
Imagine a table with the fields <Name>, <Age>, <SSN> and <Phone Extension>. This table has many possible superkeys. Three of these are <SSN>, <Phone Extension, Name> and <SSN, Name>. Of those listed, only <SSN> is a candidate key, as the others contain information not necessary to uniquely identify records.

Foreign Key
A foreign key is an attribute or combination of attribute in a relation whose value match a primary key in another relation. The table in which foreign key is created is called as dependent table. The table to which foreign key is refers is known as parent table.

Superkey vs. Candidate key

candidate key is a minimal superkey

Difference between super key and composite key

The accepted answer is not entirely accurate...

  • A superkey is any set of columns that, combined together, are unique. There are typically many superkeys per table and same column may be shared by many superkeys. They are not very useful by themselves, but are more of a mental tool for identifying candidate keys (see below).
  • A candidate key is a minimal superkey - if any column is removed it would no longer be unique. There are typically significantly fewer candidate keys than superkeys.
  • A key is just a synonym for a candidate key.
  • A composite1 key is a key that has more than one column. In other words, it's a minimal superkey that has multiple columns.

Few more points:

  • Every key is unique, so calling it "unique key" is redundant. Just "key" is enough.
  • At the DBMS level, a key is enforced through a PRIMARY KEY or UNIQUE2 constraint.
  • An index is usually present underneath the key (PRIMARY KEY or UNIQUE constraint), for performance reasons. But despite often going together, key and index are separate concepts: key is a logical concept (changes the meaning of data) and index is a physical concept (doesn't change the meaning of data, just performance).

1 Aka. compound, complex or concatenated.

2 On NOT NULL columns.

Superkey, candidate key & primary key

Since you don't want textbook definitions, loosely speaking, a super key is a set of columns that uniquely defines a row.

This set can have one or more elements, and there can be more than one super key for a table. You usually do this through functional dependencies.

In your example, I'm assuming:

StudentNumber    unique
FamilyName not unique
Degree not unique
Major not unique
Grade not unique
PhoneNumber not unique

In this case, a superkey is any combination that contains the student number.

So the following are superkeys

StudentNumber
StudentNumber, FamilyName
StudentNumber, FamilyName, Degree
StudentNumber, FamilyName, Degree, Major
StudentNumber, FamilyName, Degree, Major, Grade
StudentNumber, FamilyName, Degree, Major, Grade, PhoneNumber
StudentNumber, Degree
StudentNumber, Degree, Major
StudentNumber, Degree, Major, Grade
StudentNumber, Degree, Major, Grade, PhoneNumber
StudentNumber, Major
StudentNumber, Major, Grade
StudentNumber, Major, Grade, PhoneNumber
StudentNumber, Grade
StudentNumber, Grade, PhoneNumber
StudentNumber, PhoneNumber

Now assume, if PhoneNumber is unique (who shares phones these days), then the following are also superkeys (in addition to what I've listed above).

PhoneNumber
PhoneNumber, Grade,
PhoneNumber, Major, Grade
PhoneNumber, Degree, Major, Grade
PhoneNumber, FamilyName, Degree, Major, Grade
PhoneNumber, Major
PhoneNumber, Degree, Major
PhoneNumber, FamilyName, Degree, Major
PhoneNumber, StudentNumber, FamilyName, Degree, Major
PhoneNumber, Degree
PhoneNumber, FamilyName, Degree
PhoneNumber, StudentNumber, FamilyName, Degree
PhoneNumber, FamilyName
PhoneNumber, StudentNumber, FamilyName

A candidate key is simply the "shortest" superkey. Going back to the 1st list of superkeys (i.e. phone number isn't unique), the shortest superkey is StudentNumber.

The primary key is usually just the candidate key.

What are Compound Key and SuperKey?

A superkey is any combination of attributes that are required to be unique for all possible values of a relation.

A candidate key is a minimal superkey. Minimal means that if any attribute of the superkey was removed then the remaining set of attributes would no longer be a superkey, i.e. the uniqueness property would be lost. Put another way, every superkey consists of a candidate key plus zero or more other attributes.

A compound key - also called a composite key - is a candidate key which has more than one attribute.



Related Topics



Leave a reply



Submit