codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🗄️

SQL & Databases

9 / 67 topics
6Creating a Database7Tables and Columns8Data Types9Primary Keys10Foreign Keys11Constraints
Tutorials/SQL & Databases/Primary Keys
🗄️SQL & Databases

Primary Keys

Updated 2026-05-15
10 min read

Primary Keys

Introduction

In the realm of database management, primary keys are fundamental components that ensure data integrity and uniqueness within a table. A primary key is a column or a set of columns in a table that uniquely identifies each row. This tutorial will guide you through understanding what primary keys are, why they are important, and how to implement them effectively.

Concept

What is a Primary Key?

A primary key is a special type of key in a database that uniquely identifies each record (row) in a table. Here are some key characteristics of primary keys:

  1. Uniqueness: Each value in the primary key column(s) must be unique across the entire table.
  2. Non-nullability: Primary key columns cannot contain NULL values.
  3. Immutability: Once a primary key is assigned, it should not change unless there's a valid reason to do so (e.g., re-identification).

Why Use Primary Keys?

Primary keys serve several critical purposes:

  1. Data Integrity: They ensure that each record in the table can be uniquely identified and accessed.
  2. Performance Optimization: Indexes are often created on primary keys, which speeds up data retrieval operations.
  3. Relationships: Primary keys are used to establish relationships between tables through foreign keys.

Examples

Creating a Table with a Primary Key

Let's walk through an example of creating a table in SQL and defining a primary key.

Example 1: Single Column Primary Key

Suppose we want to create a table named Employees where each employee is uniquely identified by their EmployeeID.

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

In this example, the EmployeeID column is designated as the primary key. This ensures that each employee has a unique identifier.

Example 2: Composite Primary Key

Sometimes, a single column might not be sufficient to uniquely identify a record. In such cases, you can use a composite primary key, which consists of multiple columns.

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

Here, the combination of OrderID and ProductID serves as the composite primary key for the Orders table.

Inserting Data

Let's insert some data into the Employees table to see how the primary key works.

INSERT INTO Employees (EmployeeID, FirstName, LastName, Email) VALUES
(1, 'John', 'Doe', 'john.doe@example.com'),
(2, 'Jane', 'Smith', 'jane.smith@example.com');

If you try to insert a duplicate EmployeeID, the database will raise an error because primary keys must be unique.

INSERT INTO Employees (EmployeeID, FirstName, LastName, Email) VALUES
(1, 'Alice', 'Johnson', 'alice.johnson@example.com'); -- This will fail

Querying Data

You can query data using the primary key to retrieve specific records efficiently.

SELECT * FROM Employees WHERE EmployeeID = 1;
Output
| EmployeeID | FirstName | LastName | Email                |
|------------|-----------|----------|----------------------|
| 1          | John      | Doe      | john.doe@example.com |

What's Next?

Now that you understand primary keys, the next step is to learn about Foreign Keys. Foreign keys are used to establish relationships between tables and ensure referential integrity. This will help you build more complex and interconnected database schemas.


By mastering primary keys, you'll be well on your way to creating robust and efficient databases. Remember, a solid understanding of primary keys is essential for maintaining data integrity and optimizing performance in any relational database system.


PreviousData TypesNext Foreign Keys

Recommended Gear

Data TypesForeign Keys