Updated June 6, 2023
Introduction to SQL Super Key
The following article provides an outline for SQL Super Key. Super Key in relational databases is an attribute or a set of attributes uniquely identifying a row or a record in the database table. By attribute, we mean a field or column in the table. There can be more than one super key in the database, and it can also be NULL. The super key can be considered similar to the super set in relational algebra.
Example:
Consider a relation with attributes of A, B, C, D, E, and F.
R = {A, B, C, D, E, F}
Now, consider the following functional dependencies.
Functional Dependencies | Super Key |
A → B, C | No |
AB → C, D, E, F | Yes |
CD → A, B, F | No |
D → E, F | No |
CF → ABDE | Yes |
If the given functional dependencies, AB and CF are super keys because they can determine all the attributes in the relation.
A relation with n attributes can have a maximum of 2^n super keys. Newbies to relational databases often confuse the candidate and super keys. A candidate key is a subset of the super key. It is the minimal key that determines all other attributes in the relation.
For illustration, consider a relation R = {A, B, C, D} and functional dependencies as A→C and B → D. Here, A and B alone cannot determine all the attributes in the relationship, but when we club A and B together, we can determine all the attributes. Hence, AB is a super key. It is minimal also because A and B alone are not super keys. Ergo, AB qualifies for candidate key too. Now you must be wondering about the difference between a candidate and primary keys. Both the keys uniquely identify a record, but a primary key cannot have NULL fields, but a candidate key can.
Examples of SQL Super Key
Given below are the examples of SQL Super Key:
Example #1
To further illustrate Super Keys in relational databases, let us take the help of a dummy table called “student_record”.
Here is the CREATE TABLE statement for the “student_record” table.
Code:
CREATE TABLE student_record (
id INT NOT NULL,
name CHARACTER VARYING (50) NOT NULL,
gender CHARACTER VARYING (50) NOT NULL,
phone CHARACTER VARYING (50) NOT NULL,
state CHARACTER VARYING (50) NOT NULL,
class INT
);
The table has been successfully created. The next task is to insert a few records to work with. You may use the given INSERT statement for this purpose.
Code:
INSERT INTO student_record(
id, name, gender, phone, state, class)
VALUES (1,'Rohit','M','989898989','MH',1),
(2,'Indira','F','942505001','MP',2),
(3,'Rohit','M','888888989','KA',1),
(4,'Priya','F','989898989','MH',1),
(5,'Steve','M','789007890','KA',2),
(6,'Priya','F','544554451','MP',2);
The command was executed successfully, and all six records have been inserted into the table. Let’s have a look at the table using a SELECT statement.
Code:
SELECT *
FROM student_record;
Output:
Now let’s try to see and find super keys based on the table above.
- {id}
- {id, name}
- {id,gender}
- {id,phone}
- {id,state}
- {id,class}
- {name,phone}
- {name,state}
- {gender,phone}
- {id,name,gender}
- {id,name,phone}
- {id, name,state}
- {id,name,class}
- {id,gender,phone}
- {id,gender,state}
- {id,gender,class}
- {id,state,class}
- {name,phone,gender}
- {name,gender,state}
- .
- .
- .
- {id,name,gender,phone.state,class}
All attributes mentioned above can be super keys, provided they uniquely determine a record. For example, consider {name, phone} key. The attribute’s name and phone number individually cannot determine all other attributes in the relation. But when they are clubbed together, they uniquely identify a record in the student_record table. Since name and phone number are not super keys, {name, phone}, key qualifies as a candidate key.
One good methodology to find super keys, which we found helpful, is listing all the possible sets of attributes and eliminating the ones that do not determine all the attributes in the set.
Let’s see one more example to illustrate it more.
Example #2
In this example, we will illustrate super keys with the help of the “student_list” table.
Code:
CREATE TABLE student_list(
student_name VARCHAR(50),
class VARCHAR(50),
Subject VARCHAR(50)
);
INSERT INTO student_list(
student_name, class, subject)
VALUES ('Rohan','V','Maths'),
('Rohan','VI','Maths'),
('Steve','VI','Maths'),
('Peter','II','Science');
The data in the student_list table looks something as follows :
Code:
SELECT * FROM student_list;
Output:
Step 1: List all possible sets of attributes.
- {}
- {student_name}
- {class}
- {subject}
- {student_name, class}
- {student_name,subject}
- {class, subject}
- {student_name, class, subject}
Step 2: Eliminate sets that do not determine all the attributes in the table/relation.
- {student_name, class} Super Key, Candidate Key
- {student_name, class, subject} Super Key
All other sets, for example, {student_name, subject}, do not uniquely determine a row. Hence, they are not super keys.
Conclusion
In this article we saw about super keys. They are a set of one or more attributes that uniquely determines a record in the relational database table.
Recommended Articles
We hope that this EDUCBA information on “SQL Super Key” was beneficial to you. You can view EDUCBA’s recommended articles for more information.