What is the primary key in SQL?

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

A primary key in SQL is a fundamental concept in relational database design. It serves as a unique identifier for each record (row) in a table, ensuring that no two rows have the same primary key value. Primary keys are essential for maintaining data integrity, establishing relationships between tables, and optimizing database performance.

Key Characteristics of a Primary Key

  1. Uniqueness:

    • Each value in the primary key column(s) must be unique across all records in the table. This uniqueness ensures that every record can be distinctly identified.
  2. Non-Null:

    • Primary key columns cannot contain NULL values. Every record must have a valid primary key value to ensure its existence and identity within the table.
  3. Immutable:

    • Ideally, primary key values should not change over time. Changing primary key values can disrupt relationships and referential integrity within the database.
  4. Single or Composite Key:

    • A primary key can consist of a single column or multiple columns (composite key). A composite primary key uses a combination of columns to ensure uniqueness.

Purpose of a Primary Key

  • Unique Identification:

    • The primary key uniquely identifies each record in a table, allowing for precise data retrieval and manipulation.
  • Establishing Relationships:

    • Primary keys are used to create relationships between tables through foreign keys. This linkage is crucial for maintaining referential integrity in relational databases.
  • Indexing and Performance:

    • Most database systems automatically create an index on the primary key, enhancing the speed and efficiency of data retrieval operations.

Defining a Primary Key

When creating a table, you can define a primary key using the PRIMARY KEY constraint. This can be done either inline with the column definition or as a table-level constraint.

1. Single-Column Primary Key:

Defining Primary Key Inline:

CREATE TABLE Employees ( EmployeeID INT PRIMARY KEY, FirstName VARCHAR(50), LastName VARCHAR(50), Email VARCHAR(100) );

Defining Primary Key as Table Constraint:

CREATE TABLE Employees ( EmployeeID INT, FirstName VARCHAR(50), LastName VARCHAR(50), Email VARCHAR(100), PRIMARY KEY (EmployeeID) );

2. Composite Primary Key:

A composite primary key involves multiple columns to ensure uniqueness across the combination of their values.

CREATE TABLE OrderDetails ( OrderID INT, ProductID INT, Quantity INT, PRIMARY KEY (OrderID, ProductID) );

In this example, the combination of OrderID and ProductID uniquely identifies each record in the OrderDetails table.

Example of a Primary Key

Consider a Customers table where each customer must have a unique identifier:

CREATE TABLE Customers ( CustomerID INT PRIMARY KEY, FirstName VARCHAR(50), LastName VARCHAR(50), Email VARCHAR(100) UNIQUE );
  • CustomerID is the primary key, ensuring that each customer can be uniquely identified.
  • Email is marked as UNIQUE, which enforces uniqueness but allows NULL values unless specified otherwise. Unlike primary keys, unique keys can accept a single NULL value depending on the database system.

Primary Key vs. Unique Key

FeaturePrimary KeyUnique Key
UniquenessEnforces uniquenessEnforces uniqueness
NullabilityCannot contain NULL valuesCan contain NULL values (varies by DBMS)
Number per TableOnly one primary key per tableMultiple unique keys per table
PurposeUniquely identifies each rowEnsures distinctiveness of specific columns

Primary Key in Table Relationships

Primary keys play a crucial role in establishing relationships between tables. When one table references another, it uses a foreign key that corresponds to the primary key of the referenced table.

Example:

CREATE TABLE Departments ( DepartmentID INT PRIMARY KEY, DepartmentName VARCHAR(100) ); CREATE TABLE Employees ( EmployeeID INT PRIMARY KEY, FirstName VARCHAR(50), LastName VARCHAR(50), DepartmentID INT, FOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID) );
  • DepartmentID in the Departments table is the primary key.
  • DepartmentID in the Employees table is a foreign key that references the Departments table, establishing a relationship between employees and their respective departments.

Best Practices for Primary Keys

  1. Choose Meaningless Keys:

    • Prefer surrogate keys (e.g., auto-incrementing integers) over natural keys (e.g., Social Security Numbers) to avoid issues if the natural key values change.
  2. Keep It Simple:

    • Use a single column as the primary key when possible to simplify relationships and indexing.
  3. Use Appropriate Data Types:

    • Select data types that are efficient for indexing and storage. Integers are commonly used due to their performance benefits.
  4. Avoid Using Mutable Columns:

    • Ensure that primary key values remain constant to maintain referential integrity and avoid cascading changes.
  5. Document Constraints:

    • Clearly document the purpose and constraints of primary keys within your database schema for better maintainability and understanding.

Handling Primary Key Violations

Attempting to insert duplicate values or NULL values into a primary key column will result in an error, preventing the operation to maintain data integrity.

Example:

-- This will succeed INSERT INTO Employees (EmployeeID, FirstName, LastName, DepartmentID) VALUES (1, 'John', 'Doe', 10); -- This will fail due to duplicate EmployeeID INSERT INTO Employees (EmployeeID, FirstName, LastName, DepartmentID) VALUES (1, 'Jane', 'Smith', 20); -- This will fail due to NULL in primary key INSERT INTO Employees (EmployeeID, FirstName, LastName, DepartmentID) VALUES (NULL, 'Alice', 'Johnson', 30);

Conclusion

A primary key is a cornerstone of relational database design in SQL, ensuring that each record within a table can be uniquely identified. By enforcing uniqueness and non-nullability, primary keys maintain data integrity and facilitate efficient data retrieval and relationship management. Properly defining and utilizing primary keys is essential for building robust, scalable, and reliable database systems.

TAGS
Coding Interview
System Design Interview
CONTRIBUTOR
Design Gurus Team

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
How many rounds of interview at Apple?
Does LeetCode have system design questions?
What is the system design interview for engineering managers?
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Image
Grokking Data Structures & Algorithms for Coding Interviews
Image
Grokking Advanced Coding Patterns for Interviews
Image
One-Stop Portal For Tech Interviews.
Copyright © 2024 Designgurus, Inc. All rights reserved.