Keys in DBMS
Keys in DBMS
Keys in DBMS
An example might be a student table that contains the course_id the student is
attending. Another table lists the courses on offer with course_id being the primary
key. The 2 tables are linked through course_id and as such course_id would be a
foreign key in the student table.
3. Candidate Key
A candidate is a subset of a super key. A candidate key is a single field or the
least combination of fields that uniquely identifies each record in the table. The
least combination of fields distinguishes a candidate key from a super key.
Every table must have at least one candidate key but at the same time can have
several.
Once your candidate keys have been identified you can now select one to be
your primary key.
4. Alternative Key
Alternate Key or Secondary Key is the key that has not been selected to be the
primary key, but are candidate keys. However, it is considered a candidate key
for the primary key.
A candidate key not selected as a primary key is called alternate or secondary
key. Candidate key is an attribute or set of attributes that you can consider as a
Primary key.
Example:
5. Super key
Super Key is an attribute (or a set of attributes) that uniquely identify a tuple
i.e. an entity in entity set.
It is a superset of Candidate Key, since Candidate Keys are selected from super
key.
Example
Let us see an example −
<Student>
{Student_ID}
{Student_Enroll}
{Student_Email}
{Student_ID, Student_Enroll}
{Student_ID, Student_Name}
{Student_ID, Student_Email}
{Student_Name, Student_Enroll}
{Student_ID, Student_Enroll, Student_Name}
{Student_ID, Student_Enroll, Student_Emai}
{Student_ID, Student_Enroll, Student_Name, Student_Email}
The following would be the candidate key from the above −
{Student_ID}
{Student_Enroll} {Student_Email}
6. Composite key
A primary key having two or more attributes is called composite key. It is a
combination of two or more columns.
An example can be −
<Student>
StudentID StudentEnrollNo StudentMarks StudentPercentage
TUTOR table
2115 Java 30
2115 C 30
4997 Python 35
8663 C++ 38
8663 Go 38
Answer:
TUTOR_DETAIL table:
TUTOR_ID TUTOR_AGE
2115 30
4997 35
8663 38
TUTOR_COURSE table:
TUTOR_ID COURSE
2115 Java
2115 C
4997 Python
8663 C++
8663 Go
Example 2:
Decompose the following table into 2NF:
<Candidate_Courses>
Answer:
<Candidate_Info>
<Course_Info>
Course_ID Course_Name
A09 CSS
A07 PHP
A03 HTML
A05 Ruby