DBMS Keys: Candidate, Primary, Super & Foreign Types

โšก Smart Summary

Keys in DBMS are attributes that uniquely identify a row in a table and establish relationships between tables. Eight key types, from super and primary to foreign and surrogate, enforce identity and integrity across a relational database.

  • ๐Ÿ—๏ธ Core Purpose: A key identifies a unique row and links related tables so data stays consistent.
  • ๐Ÿ” Super Key: Any attribute set that identifies rows, possibly with extra attributes beyond the minimum.
  • โญ Primary Key: The chosen candidate key, never null and never duplicated, one per table.
  • ๐ŸŽฏ Candidate Key: A minimal super key with no redundant attribute; the primary key is picked from these.
  • ๐Ÿ” Alternate Key: Any candidate key not selected as the primary key.
  • ๐Ÿ”— Foreign Key: A column referencing another table’s primary key to enforce referential integrity.
  • ๐Ÿงฉ Composite and Surrogate: Composite keys combine columns for uniqueness; a surrogate key is a generated stand-in.

DBMS Keys Candidate Super Primary

What Are Keys in DBMS?

Keys in DBMS are an attribute or set of attributes that help you identify a row (tuple) in a relation (table). They allow you to find the relationship between two tables. Keys help you uniquely identify a row in a table by a combination of one or more columns, and they are also helpful for finding a unique record in the table.

Example:

Employee ID FirstName LastName
11 Andrew Johnson
22 Tom Wood
33 Alex Hale

In the example above, Employee ID is a primary key because it uniquely identifies an employee record. In this table, no other employee can have the same employee ID.

Why Do We Need a Key?

Here are some reasons for using a key in a DBMS:

  • Keys help you identify any row of data in a table. In a real-world application, a table could contain thousands of records, and the records could be duplicated. Keys ensure that you can uniquely identify a table record despite these challenges.
  • They allow you to establish and identify relationships between tables.
  • They help you enforce identity and integrity in the relationship.

Types of Keys in DBMS

There are mainly eight different types of keys in DBMS, and each key has its own functionality:

  1. Super Key
  2. Primary Key
  3. Candidate Key
  4. Alternate Key
  5. Foreign Key
  6. Compound Key
  7. Composite Key
  8. Surrogate Key

The table below summarises all eight at a glance before each is explained in detail.

Key In one line Unique?
Super Key Any attribute set that identifies a row, extras allowed Yes
Candidate Key A minimal super key, no redundant attribute Yes
Primary Key The chosen candidate key, not null Yes
Alternate Key A candidate key not chosen as primary Yes
Foreign Key References a primary key in another table No
Compound Key Two or more attributes, each possibly non-unique Combined
Composite Key Two or more columns combined for uniqueness Combined
Surrogate Key A generated artificial identifier Yes

What is a Super Key?

A super key is a group of single or multiple keys that identifies rows in a table. A super key may have additional attributes that are not needed for unique identification.

Example:

EmpSSN EmpNum Empname
9812345098 AB05 Shown
9876512345 AB06 Roslyn
199937890 AB07 James

In the example above, EmpSSN and EmpNum are super keys.

What is a Primary Key?

PRIMARY KEY in DBMS is a column or group of columns in a table that uniquely identifies every row in that table. The primary key cannot be a duplicate, meaning the same value cannot appear more than once in the table. A table cannot have more than one primary key.

Rules for Defining a Primary Key

  • Two rows cannot have the same primary key value.
  • Every row must have a primary key value.
  • The primary key field cannot be null.
  • The value in a primary key column can never be modified or updated if any foreign key refers to that primary key.

Example: In the following example, StudID is a primary key.

StudID Roll No First Name LastName Email
1 11 Tom Price abc@gmail.com
2 12 Nick Wright xyz@gmail.com
3 13 Dana Natan mno@yahoo.com

What is an Alternate Key?

ALTERNATE KEY is a column or group of columns in a table that uniquely identifies every row. A table can have multiple choices for a primary key, but only one can be set as the primary key. All the keys that are not the primary key are called alternate keys.

Example: In this table, StudID, Roll No, and Email are qualified to become a primary key. But since StudID is the primary key, Roll No and Email become the alternate keys.

StudID Roll No First Name LastName Email
1 11 Tom Price abc@gmail.com
2 12 Nick Wright xyz@gmail.com
3 13 Dana Natan mno@yahoo.com

What is a Candidate Key?

CANDIDATE KEY is a set of attributes that uniquely identify tuples in a table. A candidate key is a super key with no repeated attributes. The primary key should be selected from the candidate keys. Every table must have at least a single candidate key, and a table can have multiple candidate keys but only a single primary key.

Properties of a candidate key:

  • It must contain unique values.
  • A candidate key may have multiple attributes.
  • It must not contain null values.
  • It should contain the minimum fields needed to ensure uniqueness.
  • It uniquely identifies each record in a table.

Example: In the table below, StudID, Roll No, and Email are candidate keys that help us uniquely identify a student record. Their relationship to super and primary keys is a common way to understand functional dependency.

StudID Roll No First Name LastName Email
1 11 Tom Price abc@gmail.com
2 12 Nick Wright xyz@gmail.com
3 13 Dana Natan mno@yahoo.com
Candidate Key in DBMS
Candidate Key in DBMS

What is a Foreign Key?

FOREIGN KEY is a column that creates a relationship between two tables. The purpose of a foreign key is to maintain data integrity and allow navigation between two different instances of an entity. It acts as a cross-reference between two tables, as it references the primary key of another table.

Example:

DeptCode DeptName
001 Science
002 English
005 Computer
Teacher ID Fname Lname
B002 David Warner
B017 Sara Joseph
B009 Mike Brunton

In this example, we have two tables, Teacher and Department, in a school. As they stand, there is no way to see which teacher works in which department.

By adding the foreign key DeptCode to the Teacher table, we can create a relationship between the two tables.

Teacher ID DeptCode Fname Lname
B002 002 David Warner
B017 002 Sara Joseph
B009 001 Mike Brunton

This concept is also known as referential integrity.

What is a Compound Key?

COMPOUND KEY has two or more attributes that allow you to uniquely recognize a specific record. It is possible that each column may not be unique by itself within the database. However, when combined, the columns become unique. The purpose of the compound key is to uniquely identify each record in the table.

Example:

OrderNo ProductID Product Name Quantity
B005 JAP102459 Mouse 5
B005 DKT321573 USB 10
B005 OMG446789 LCD Monitor 20
B004 DKT321573 USB 15
B002 OMG446789 Laser Printer 3

In this example, OrderNo and ProductID cannot each be a primary key, as neither uniquely identifies a record. However, a compound key of OrderNo and ProductID uniquely identifies each record.

What is a Composite Key?

COMPOSITE KEY is a combination of two or more columns that uniquely identify rows in a table. The combination of columns guarantees uniqueness, though individually uniqueness is not guaranteed. Hence, they are combined to uniquely identify records in a table.

The difference between a compound and a composite key is that any part of a compound key can be a foreign key, but a composite key may or may not be part of a foreign key.

What is a Surrogate Key?

SURROGATE KEY is an artificial key that aims to uniquely identify each record. This kind of key is unique because it is created when you do not have any natural primary key. Surrogate keys lend no meaning to the data in the table and are usually an integer generated right before the record is inserted.

Fname Lastname Start Time End Time
Anne Smith 09:00 18:00
Jack Francis 08:00 17:00
Anna McLean 11:00 20:00
Shown Willam 14:00 23:00

The example above shows the shift timings of different employees. Here, a surrogate key is needed to uniquely identify each employee. Surrogate keys in SQL are used when:

  • No property has the parameter of a primary key.
  • The primary key is too big or complicated.

Difference Between Primary Key and Foreign Key

The following is the main difference between a primary key and a foreign key:

Primary Key Foreign Key
Helps you uniquely identify a record in the table. It is a field in the table that is the primary key of another table.
A primary key never accepts null values. A foreign key may accept multiple null values.
The primary key is a clustered index, and data in the table are physically organized in the sequence of the clustered index. A foreign key cannot automatically create an index, clustered or non-clustered. You can create one manually.
You can have a single primary key in a table. You can have multiple foreign keys in a table.

FAQs

Every candidate key can uniquely identify a row. The primary key is the one candidate key the designer selects; the remaining candidate keys become alternate keys.

Both combine columns for uniqueness. In a compound key each column is itself a foreign key, while a composite key simply joins two or more attributes that need not be foreign keys.

Use a surrogate key when no natural attribute is reliably unique, or when the natural key is large or may change. A generated integer stays stable and keeps joins simple.

Yes. By profiling columns for uniqueness, null rate, and stability, AI can shortlist candidate keys and suggest a primary key or a surrogate, leaving the final choice to the designer.

Yes. Unlike a primary key, a foreign key may hold null, which represents a row that is not yet related to any parent record. A non-null foreign key must match an existing primary key.

Summarize this post with: