SQL
Database Keys
MUL vs PRI vs UNI
Primary Key
Unique Key

SQL keys, MUL vs PRI vs UNI

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Understanding SQL Keys: MUL vs PRI vs UNI

In the realm of databases, keys play a pivotal role in determining the structure, accessibility, and integrity of data. SQL, as a Structured Query Language, relies heavily on these keys to manage how data gets stored, referenced, and related. This article focuses on the core SQL keys: PRI, UNI, and MUL, explaining their significance and use-cases.

SQL Keys Overview

At its core, a key in SQL serves as a constraint ensuring that the database adheres to normalization standards and relations. Here are some fundamental types of SQL keys:

  1. Primary Key (PRI):
    • Ensures that each row within a table is unique.
    • Columns defined as primary keys cannot contain NULL values.
    • A table can have only one primary key, which can consist of one or multiple columns (composite key).
  2. Unique Key (UNI):
    • Similar to a primary key in terms of uniqueness.
    • Allows for one NULL value, since NULL is not considered equal to anything, even other NULLs.
    • A table can have multiple unique keys.
  3. Multiple Key (MUL):
    • Refers to indexing for columns that aren't necessarily unique.
    • Helps improve query performance by reducing data retrieval time.
    • Commonly used for columns that are frequently searched, filtered, or joined.

Technical Explanations

Primary Key (PRI)

A primary key uniquely identifies each record in a table. It's analogous to a passport number which is unique per individual. Defining a column (or a set of columns) as a primary key has the following implications:

  • Uniqueness: No two rows can have the same primary key.
  • Non-nullability: None of the values in the primary key columns can be null.

Example:

sql
1CREATE TABLE Employees (
2    EmployeeID INT PRIMARY KEY,
3    Name VARCHAR(100),
4    DepartmentID INT
5);

In the example above, EmployeeID serves as the primary key ensuring every employee's record is accessible and distinct.

Unique Key (UNI)

While primary keys ensure uniqueness, unique keys extend this feature to additional columns. This constraint is suitable for columns requiring distinct values but allowing for nullability.

Example:

sql
1CREATE TABLE Users (
2    UserID INT PRIMARY KEY,
3    Email VARCHAR(255) UNIQUE
4);

Here, Email must be unique across all entries, which is enforced by the unique key constraint.

Multiple Key (MUL)

Primary and unique keys are inherently indexed. A multiple key arises from indexes that do not enforce uniqueness but enhance database performance. It is a critical concept when dealing with large datasets, especially for columns involved in WHERE clauses or joins.

Example:

sql
1CREATE TABLE Orders (
2    OrderID INT PRIMARY KEY,
3    CustomerID INT,
4    OrderDate DATE,
5    INDEX (CustomerID)
6);

CustomerID here is indexed, enabling faster searches and retrievals without ensuring unique values.

Comparing SQL Keys: PRI, UNI, and MUL

The distinctions and purposes of these keys can best be summarized through a comparison table:

Key TypeUniquenessNullabilityNumber of Allowed per TablePurpose
PRIUniqueNon-nullOnly oneIdentifies each row uniquely
UNIUniqueAllows one NULLMultipleEnsures uniqueness (with null consideration)
MULNon-uniqueAllows NULLMultipleEnhances queries through indexing

Advanced Topics

Composite Keys

A composite key involves using multiple columns to enforce uniqueness. This is often necessary for tables where single-column uniqueness doesn't suffice.

Example:

sql
1CREATE TABLE Projects (
2    ProjectID INT,
3    DepartmentID INT,
4    PRIMARY KEY (ProjectID, DepartmentID)
5);

Foreign Keys and Their Relationship with PRI and UNI

Foreign keys maintain referential integrity between tables by ensuring the existing relationship between primary keys of one table to columns in another. They typically relate to a primary or unique key in a related table.

Example:

sql
1CREATE TABLE Orders (
2    OrderID INT PRIMARY KEY,
3    CustomerID INT,
4    FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
5);

In this setup, CustomerID in the Orders table references CustomerID in the Customers table.

Conclusion

Understanding the roles and differences of primary, unique, and multiple keys is essential for effective database design. They provide frameworks for data integrity, optimization, and structured data relationships, enabling robust SQL-based database management systems. Whether enforcing constraints via primary or unique keys or enhancing performance through multiple keys, each plays a vital role in ensuring data remains organized and accessible.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.