Skip to content

[Edit] SQL: Primary Keys #7118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
d0dd9bd
[Edit] SQL: DATEDIFF()
mamtawardhani May 21, 2025
9f5c19b
Update datediff.md
mamtawardhani May 21, 2025
c32e9f3
Merge branch 'Codecademy:main' into main
mamtawardhani May 23, 2025
4170ba2
Merge branch 'Codecademy:main' into main
mamtawardhani May 23, 2025
8325585
Merge branch 'Codecademy:main' into main
mamtawardhani May 26, 2025
8f6f8e8
Merge branch 'Codecademy:main' into main
mamtawardhani May 27, 2025
e4c54e8
Merge branch 'Codecademy:main' into main
mamtawardhani May 28, 2025
7b3b9c0
Merge branch 'Codecademy:main' into main
mamtawardhani May 29, 2025
27ecefd
Merge branch 'Codecademy:main' into main
mamtawardhani May 29, 2025
0392da4
Merge branch 'Codecademy:main' into main
mamtawardhani May 30, 2025
d550fa7
Merge branch 'Codecademy:main' into main
mamtawardhani Jun 2, 2025
793be7d
Merge branch 'Codecademy:main' into main
mamtawardhani Jun 3, 2025
2f03b61
Merge branch 'Codecademy:main' into main
mamtawardhani Jun 3, 2025
25eb0ab
Merge branch 'Codecademy:main' into main
mamtawardhani Jun 3, 2025
73e0e3b
Merge branch 'Codecademy:main' into main
mamtawardhani Jun 4, 2025
44f4c63
Merge branch 'Codecademy:main' into main
mamtawardhani Jun 5, 2025
545a8da
Merge branch 'Codecademy:main' into main
mamtawardhani Jun 6, 2025
49d85cd
Merge branch 'Codecademy:main' into main
mamtawardhani Jun 9, 2025
f488437
Merge branch 'Codecademy:main' into main
mamtawardhani Jun 10, 2025
9b642e6
Merge branch 'Codecademy:main' into main
mamtawardhani Jun 11, 2025
afb1cf5
Merge branch 'Codecademy:main' into main
mamtawardhani Jun 12, 2025
dc740fb
Merge branch 'Codecademy:main' into main
mamtawardhani Jun 13, 2025
6a579a7
Merge branch 'Codecademy:main' into main
mamtawardhani Jun 16, 2025
a450c5b
[Edit] SQL: Primary Keys
mamtawardhani Jun 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
193 changes: 135 additions & 58 deletions content/sql/concepts/primary-keys/primary-keys.md
Original file line number Diff line number Diff line change
@@ -1,103 +1,180 @@
---
Title: 'Primary Keys'
Description: 'Primary keys are special columns used to uniquely identify each row of a table in SQL.'
Title: 'PRIMARY KEYS'
Description: 'Uniquely identifies each record in a table and ensures data integrity by preventing duplicate or `NULL` values in one or more specified columns.'
Subjects:
- 'Computer Science'
- 'Data Science'
Tags:
- 'Comments'
- 'Documentation'
- 'Database'
- 'Primary Key'
- 'Tables'
CatalogContent:
- 'learn-sql'
- 'paths/analyze-data-with-sql'
- 'paths/data-science'
---

**Primary keys** are special columns that are used to uniquely identify each row of a table in SQL.
The **`PRIMARY KEY`** constraint is a fundamental database constraint that uniquely identifies each record in a table. It serves as the main identifier for rows and ensures data integrity by preventing duplicate records and null values in the specified column or columns. It combines the functionality of both `NOT NULL` and `UNIQUE` constraints, making it essential for maintaining data consistency and establishing relationships between tables.

`PRIMARY KEY` constraints are used extensively in database design for creating unique identifiers, establishing table relationships through foreign keys, enabling efficient data retrieval through automatic indexing, and maintaining referential integrity across related tables. They are commonly implemented in user management systems for unique user IDs, inventory systems for product identification, order processing systems for transaction tracking, and any scenario where each record must be uniquely identifiable and accessible.

## Syntax

The PRIMARY KEY constraint can be defined in two ways:

### Method 1: Column-Level Definition

```pseudo
CREATE TABLE table_key (
id INTEGER PRIMARY KEY,
column_1 TEXT,
column_2 INTEGER
CREATE TABLE table_name (
column_name data_type PRIMARY KEY,
column2 data_type,
...
);
```

The `PRIMARY KEY` constraint is used to create columns that uniquely identify each row. A primary key column has a few requirements:
### Method 2: Table-Level Definition

- None of the values can be `NULL`.
- Each value must be unique (e.g., two rows in a `customers` table wouldn't have the same primary `customer_id`).
- A table cannot have more than one primary key.

Attempts to insert a row with an existing primary key will result in a constraint violation that prevents the new row from being added.
```pseudo
CREATE TABLE table_name (
column1 data_type,
column2 data_type,
...,
CONSTRAINT constraint_name PRIMARY KEY (column1)
);
```

If a table was created without a primary key, it can be added with the [`ALTER TABLE`](https://www.codecademy.com/resources/docs/sql/commands/alter-table) command. The statement below adds a primary `id` column, via the `PRIMARY KEY` constraint, to `table_name`:
### Adding PRIMARY KEY to Existing Table

```pseudo
ALTER TABLE table_name
ADD PRIMARY KEY (id);
ADD CONSTRAINT constraint_name PRIMARY KEY (column_name);
```

## Foreign Keys
**Parameters:**

- `table_name`: The name of the table where the PRIMARY KEY constraint will be applied
- `column_name`: The column or columns that will form the primary key
- `data_type`: The data type of the primary key column(s)
- `constraint_name`: Optional name for the PRIMARY KEY constraint (recommended for easier management)

**Return value:**

When the primary key for one table appears in a different table, it is called a foreign key. The most common types of [joins](https://www.codecademy.com/resources/docs/sql/joins) will be joining a foreign key from one table with the primary key from another table.
The PRIMARY KEY constraint itself does not return a value. However, it enforces uniqueness and creates an automatic index that improves query performance when searching by the primary key column(s).

Using the following `customers` table as an example:
## Example 1: Basic Table Creation

This example demonstrates creating a table with a single-column primary key for user management:

```sql
CREATE TABLE customers (
customer_id INTEGER NOT NULL,
first_name varchar(255),
last_name varchar(255)
-- Create a users table with UserID as primary key
CREATE TABLE Users (
UserID INT PRIMARY KEY,
Username VARCHAR(50) NOT NULL,
Email VARCHAR(100) NOT NULL,
DateCreated DATE
);

-- Insert sample data
INSERT INTO Users (UserID, Username, Email, DateCreated) VALUES
(1, 'john_doe', '[email protected]', '2024-01-15'),
(2, 'jane_smith', '[email protected]', '2024-01-16'),
(3, 'mike_wilson', '[email protected]', '2024-01-17');
```

The `orders` table is created and joined via `FOREIGN KEY` with the existing `customer` table through its `customer_id`:
Output of this code is:

```sql
CREATE TABLE orders (
order_id INTEGER NOT NULL,
total_cost FLOAT,
purchase_date DATE,
customer_id INTEGER NOT NULL,
PRIMARY KEY (order_id),
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);
```shell
Table 'Users' created successfully with UserID as PRIMARY KEY.
3 rows inserted successfully.
```

The displayed `orders` table, with its primary key (`order_id`) and foreign key (`customer_id`), may look like this:
The `UserID` column serves as the primary key, automatically ensuring that each user has a unique identifier. The database will reject any attempt to insert duplicate `UserID` values or `NULL` values in this column, maintaining data integrity for the user management system.

| order_id | customer_id | total_cost | purchase_date |
| -------- | ----------- | ---------- | ------------- |
| 1 | 1001 | 13.99 | 2022-01-01 |
| 2 | 1294 | 61.42 | 2022-01-01 |
| 3 | 1001 | 23.45 | 2022-01-02 |
## Example 2: E-commerce Order Management

## Composite Keys
This example shows implementing a composite primary key for order line items in an e-commerce system:

Sometimes, having one primary key per table is not enough to uniquely identify a row. In such cases, multiple columns would work as composite keys for the table. This requirement should be detected during the designing phase of a database.
```sql
-- Create order items table with composite primary key
CREATE TABLE OrderItems (
OrderID INT NOT NULL,
ProductID INT NOT NULL,
Quantity INT NOT NULL,
UnitPrice DECIMAL(10,2) NOT NULL,
LineTotal DECIMAL(12,2),
CONSTRAINT PK_OrderItems PRIMARY KEY (OrderID, ProductID)
);

For example, a database of car parts will have to uniquely identify a row of parts. Either the `engine_ID` or `body_ID` could be used. However, this may create ambiguity as cars could get their engines swapped.
-- Insert order line items
INSERT INTO OrderItems (OrderID, ProductID, Quantity, UnitPrice, LineTotal) VALUES
(1001, 501, 2, 29.99, 59.98),
(1001, 502, 1, 45.50, 45.50),
(1002, 501, 3, 29.99, 89.97),
(1002, 503, 1, 15.25, 15.25);

Depending on local regulations, a car may require an engine part ID and a body ID to be associated with a license plate. One solution might be adding more row information about the car, such as `left_door_ID`, `gearbox_ID`, etc. But then a specific car would have to be identified by two different aspects: its body and its engine.
-- Attempt to insert duplicate composite key (will fail)
INSERT INTO OrderItems (OrderID, ProductID, Quantity, UnitPrice, LineTotal) VALUES
(1001, 501, 1, 29.99, 29.99);
```

A composite key would be useful in this case. This is how a `vehicle_registry` table might look (extra parts/columns omitted for brevity):
The output of this code will be:

| engine_id | body_id | gearbox_id | purchase_date |
| --------- | ------- | ---------- | ------------- |
| 500 | abc | 001 | 2022-01-01 |
| 600 | def | 002 | 2022-01-01 |
| 700 | ghi | 003 | 2022-01-02 |
```shell
Table 'OrderItems' created successfully.
4 rows inserted successfully.
ERROR: Duplicate entry '1001-501' for key 'PRIMARY'
```

The statement below creates the `vehicle_registry` table with a composite key:
The composite primary key (`OrderID`, `ProductID`) ensures that each product can appear only once per order, preventing duplicate line items while allowing the same product to appear in different orders. This design maintains data integrity in e-commerce order processing.

## Example 3: Adding PRIMARY KEY to Existing Table

This example demonstrates adding a primary key constraint to an existing table and handling the challenges that may arise:

```sql
CREATE TABLE vehicle_registry (
engine_id INTEGER,
body_id TEXT,
gearbox_id INTEGER,
purchase_date DATE,
PRIMARY KEY(engine_id, body_id, purchase_date)
-- Create a products table without primary key initially
CREATE TABLE Products (
ProductCode VARCHAR(20) NOT NULL,
ProductName VARCHAR(100) NOT NULL,
Category VARCHAR(50),
Price DECIMAL(8,2)
);

-- Insert sample data
INSERT INTO Products (ProductCode, ProductName, Category, Price) VALUES
('LAPTOP001', 'Gaming Laptop', 'Electronics', 1299.99),
('MOUSE002', 'Wireless Mouse', 'Electronics', 29.99),
('DESK003', 'Standing Desk', 'Furniture', 399.99);

-- Add PRIMARY KEY constraint to existing table
ALTER TABLE Products
ADD CONSTRAINT PK_Products PRIMARY KEY (ProductCode);

-- Verify the constraint by attempting duplicate insertion
INSERT INTO Products (ProductCode, ProductName, Category, Price) VALUES
('LAPTOP001', 'Another Laptop', 'Electronics', 999.99);
```

The output generated by this code will be:

```shell
Table 'Products' created successfully.
3 rows inserted successfully.
PRIMARY KEY constraint 'PK_Products' added successfully.
ERROR: Duplicate entry 'LAPTOP001' for key 'PRIMARY'
```

Adding a `PRIMARY KEY` constraint to an existing table requires that all existing data in the specified column(s) be unique and non-null. The database will automatically create an index on the `ProductCode` column, improving query performance for product lookups in inventory management systems.

## Frequently Asked Questions

### 1. Can a table have multiple primary keys?

No, a table can have only one `PRIMARY KEY` constraint. However, that single primary key can consist of multiple columns (composite primary key). If you need additional unique constraints, use UNIQUE constraints instead.

### 2. What happens if I try to insert NULL values into a primary key column?

The database will reject the insertion with an error. `PRIMARY KEY` columns automatically have the NOT NULL constraint, so they cannot contain NULL values under any circumstances.

### 3. What's the difference between `PRIMARY KEY` and `UNIQUE` constraints?

A `PRIMARY KEY` enforces both `UNIQUE` and `NOT NULL` constraints on a column or group of columns. In contrast, `UNIQUE` allows `NULL` values (typically one per column, depending on the RDBMS). Also, a table can have only one `PRIMARY KEY` but can have multiple `UNIQUE` constraints. `PRIMARY KEY`s are also used in defining `FOREIGN KEY` relationships.