Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Superkey, candidate key & primary key

Can any kind soul clarify my doubts with a simple example below and identify the superkey, candidate key and primary key?

I know there are a lot of posts and websites out there explaining the differences between them. But it looks like all are generic definitions.

Example:

Student (StudentNumber, FamilyName, Degree, Major, Grade, PhoneNumber) 

So from the above example, I can know StudentNumber is a primary key.

But as for superkey, I'm a bit confused what combination of attributes could be grouped into the superkey?

As for candidate key, I'm confused by the definition given as any candidate key can qualify as a primary key.

Does it mean that attributes such as PhoneNumber are a candidate key and can be a primary key? (Assuming that a PhoneNumber only belongs to one student)

Thanks for any clarification!

like image 534
JLearner Avatar asked Dec 01 '11 06:12

JLearner


People also ask

What is Superkey candidate key & primary key?

Super Key – A super key is a group of single or multiple keys which identifies rows in a table. Primary Key – is a column or group of columns in a table that uniquely identify every row in that table. Candidate Key – is a set of attributes that uniquely identify tuples in a table.

What is candidate key with example?

Candidate Key: The minimal set of attributes that can uniquely identify a tuple is known as a candidate key. For Example, STUD_NO in STUDENT relation. It is a minimal super key. It is a super key with no repeated data is called a candidate key.

Is Superkey a 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.


2 Answers

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.

like image 51
Cambium Avatar answered Oct 23 '22 14:10

Cambium


A superkey is any set of attributes for which the values are guaranteed to be unique for all possible sets of tuples in a table at all times.

A candidate key is a "minimal" superkey - meaning the smallest subset of superkey attributes which are unique. Removing any attribute from a candidate key would therefore make it non-unique.

A primary key is just a candidate key. There is no difference between a primary key and any other candidate key.

It's not really useful to make assumptions about keys based only on a list of attribute names. You need to know what dependencies are supposed to hold between the attributes. Having said that, my guess is that you are right - StudentNumber is likely a candidate key in your example.

like image 28
nvogel Avatar answered Oct 23 '22 16:10

nvogel