Grokking SQL for Tech Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Keys in SQL
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

SQL Keys

In MySQL, keys are fundamental for maintaining data integrity and establishing relationships between tables. SQL keys play vital roles, contributing to an optimized database structure and enhanced performance. Let's explore these keys in detail.

Candidate Key

A candidate key in a relational database is a set of one or more columns that can uniquely identify a record in a table. It could potentially be used as a primary key.

There can be more than one candidate key but only one primary key.

Image

In the employee table, the columns id and email can uniquely identify a record.

Primary Key

The primary key is the key that uniquely identifies each record in a table. It is a unique key. There may be multiple candidate keys in a relationship, from which one primary key can be selected.

It has no duplicate values, and it cannot be null.

Image

Foreign Key

A foreign key is a key that establishes a link between data in two tables. The foreign key in one table refers to the primary key in another table, creating a relationship between the two tables.

Image

Composite Key

A composite key is a key that consists of two or more columns in a database table. Unlike a single-column key (such as a primary key or a unique key), a composite key uses a combination of columns to identify a record in the table uniquely.

Image

In the above table, (email, phone) is a composite key.

Unique Key

A unique key is a set of one or more columns that uniquely identifies each record in a table. It is similar to the primary key but can accept a null value.

More than one unique key can exist in a table, unlike the primary key, which can exist only once.

Image

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible