0% found this document useful (0 votes)
8 views9 pages

Types of Keys in SQL

The document outlines various types of keys in SQL, including Super Key, Candidate Key, Primary Key, Alternate Key, Composite Key, Foreign Key, and Surrogate Key. Each key type serves a specific purpose in uniquely identifying records within a database table and establishing relationships between tables. The document provides examples using a Students table to illustrate how these keys function in practice.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views9 pages

Types of Keys in SQL

The document outlines various types of keys in SQL, including Super Key, Candidate Key, Primary Key, Alternate Key, Composite Key, Foreign Key, and Surrogate Key. Each key type serves a specific purpose in uniquely identifying records within a database table and establishing relationships between tables. The document provides examples using a Students table to illustrate how these keys function in practice.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

01/09

Types of keys
in SQL

SWIPE
02/09

1. Super Key
A super key is any set of columns in a table that can uniquely
identify each row. Even if one column alone is enough to make
each row unique, adding extra columns still forms a super key, as
long as the combination ensures no two rows are identical in
those columns.

Imagine a table called Students that includes columns like


student_id, name, email, phone_number, and dob. Each
student_id is unique, so it can identify a student on its own. The
email is also unique for each student. You can combine
student_id with email or email with phone_number to tell
students apart too. These combinations are called super keys
because they help make sure every student can be identified,
even if they have extra columns.

KEEP SWIPING
03/09

2. Candidate Key
A Candidate key is a minimal super key, which means it contains
no extra attributes that are not needed for uniqueness. In simpler
terms, it is the smallest combination of columns that can
uniquely identify a row in a table.

For instance, in the Students table, student_id can uniquely


identify each student on its own. Similarly, email serves as
another candidate key since each student has a unique email
address. Additionally, if every student has a unique phone
number, then phone_number can also act as a candidate key.

ONE MORE
04/09

3. Primary Key
A Primary key is a special type of candidate key chosen to
uniquely identify each row in a table. There can be only one
primary key in a table and it must be unique and cannot contain
null values.

In a students table with columns like student_id, email, and


phone_number, all three can uniquely identify a student, making
them candidate keys. However, student_id is the best choice for
the primary key because it’s a stable, unique identifier that won’t
change over time, unlike email or phone_number

ONE MORE
05/09

4. Alternate Key
An alternate key is a candidate key that is not used as the
primary key. While the primary key is the main way to uniquely
identify each row, alternate keys serve as additional unique
identifiers.

In the Students table, if student_id is chosen as the primary key,


then email and phone_number can be considered alternate
keys. They also uniquely identify students but are not used as the
main key for the table

ONE MORE
06/09

5. Composite Key
A composite key is a type of candidate key or primary key that
consists of two or more attributes. When no single attribute can
uniquely identify a record in a table, we can use a combination of
attributes to achieve uniqueness. This grouping of attributes
ensures that each row in the table can be uniquely identified.

The Students table includes columns such as student_id,


course_id, name, email, and dob. In this case, a composite key
can be created using student_id and course_id together to
uniquely identify each enrollment record. Since a single
student_id alone cannot distinguish between multiple courses a
student may take, combining student_id with course_id ensures
that each entry is unique, allowing us to accurately track which
courses each student is enrolled in.

ONE MORE
07/09

6. Foreign Key
A foreign key is a primary key from one table that is used to
establish a relationship with another table.

In this example, we have two tables: Students and Enrollments.


The Students table includes columns like student_id, name,
email, and dob, with student_id as the primary key uniquely
identifying each student. The Enrollments table contains
columns such as enrollment_id, student_id, course_id, and
enrollment_date, where enrollment_id is the primary key and
student_id serves as a foreign key referencing the student_id in
the Students table. This relationship ensures that every value in
the student_id column of the Enrollments table corresponds to a
valid student_id from the Students table, thus maintaining data
integrity and allowing us to accurately track which students are
enrolled in which courses.

ONE MORE
08/09

7. Surrogate Key
A surrogate key is a unique identifier for a record in a database
that is not based on the actual data in the record. Instead of
using a meaningful piece of information like an email address or
a name, a surrogate key is typically a simple number or code that
is automatically generated.

For example, In a Books table, instead of using the ISBN number


as the primary key (which can sometimes change or be reused),
you could use a surrogate key like book_id. This book_id would
be a unique, automatically generated number for each book,
ensuring that every entry in the table is distinctly identifiable.

ONE MORE
09/09

What are your


favorite SQL
functions for data
aggregation?

COMMENT BELOW

You might also like