A composite primary key helps you organize data by using two parts together to find each item uniquely.
Composite primary key in DynamoDB
Start learning this pattern below
Jump into concepts and practice - no test required
TableName: PartitionKey: <PartitionKeyName> (Type: String or Number) SortKey: <SortKeyName> (Type: String or Number) Example: PartitionKey: UserID (String) SortKey: OrderID (String)
The PartitionKey is like the main folder where data is stored.
The SortKey helps sort and find items inside that folder.
PartitionKey: CustomerID (String) SortKey: InvoiceID (String)
PartitionKey: ProductCategory (String) SortKey: ProductID (Number)
PartitionKey: UserID (String) SortKey: Timestamp (Number)
This example creates a table with a composite primary key using OrderID and ItemID. It adds two items to the same order and then queries all items for that order.
CreateTable {
TableName: "Orders",
KeySchema: [
{ AttributeName: "OrderID", KeyType: "HASH" }, # Partition Key
{ AttributeName: "ItemID", KeyType: "RANGE" } # Sort Key
],
AttributeDefinitions: [
{ AttributeName: "OrderID", AttributeType: "S" },
{ AttributeName: "ItemID", AttributeType: "S" }
],
ProvisionedThroughput: {
ReadCapacityUnits: 5,
WriteCapacityUnits: 5
}
}
# Insert items
PutItem TableName="Orders" Item={"OrderID": "123", "ItemID": "A1", "Product": "Book", "Quantity": 2}
PutItem TableName="Orders" Item={"OrderID": "123", "ItemID": "B2", "Product": "Pen", "Quantity": 5}
# Query items for OrderID = "123"
Query TableName="Orders" KeyConditionExpression="OrderID = :orderId" ExpressionAttributeValues={":orderId": {"S": "123"}}Composite primary keys let you store multiple related items under one main key.
PartitionKey decides the partition where data is stored; SortKey orders items inside that partition.
Using composite keys helps avoid duplicates and improves query speed for grouped data.
A composite primary key uses two attributes: PartitionKey and SortKey.
It helps organize and find related data easily.
Use it when you want to group items and still keep each item unique.
Practice
What is a composite primary key in DynamoDB?
Solution
Step 1: Understand primary key types in DynamoDB
DynamoDB supports simple primary keys (one attribute) and composite primary keys (two attributes).Step 2: Identify composite primary key components
A composite primary key consists of a PartitionKey and a SortKey to uniquely identify items.Final Answer:
A key made of two attributes: PartitionKey and SortKey -> Option CQuick Check:
Composite primary key = PartitionKey + SortKey [OK]
- Thinking composite key has only one attribute
- Confusing composite key with secondary indexes
- Believing composite key allows duplicates
Which of the following is the correct way to define a composite primary key in DynamoDB table creation?
{
"TableName": "Orders",
"KeySchema": [
{"AttributeName": "CustomerId", "KeyType": "HASH"},
{"AttributeName": "OrderDate", "KeyType": "RANGE"}
]
}Solution
Step 1: Recall KeyType meanings
In DynamoDB, HASH means PartitionKey and RANGE means SortKey.Step 2: Match KeyType to attributes
PartitionKey must be HASH and SortKey must be RANGE for composite keys.Final Answer:
Use KeyType HASH for PartitionKey and RANGE for SortKey -> Option DQuick Check:
PartitionKey=HASH, SortKey=RANGE [OK]
- Swapping HASH and RANGE roles
- Using same KeyType for both keys
- Confusing attribute names with KeyType
Given a DynamoDB table with composite primary key (UserId as PartitionKey, Timestamp as SortKey), what will this query return?
{
"TableName": "UserActivity",
"KeyConditionExpression": "UserId = :uid and Timestamp > :time",
"ExpressionAttributeValues": {
":uid": {"S": "user123"},
":time": {"N": "1609459200"}
}
}Solution
Step 1: Understand KeyConditionExpression
It filters items by PartitionKey equality and SortKey condition.Step 2: Analyze the query conditions
UserId = :uid restricts to user123; Timestamp > :time filters timestamps after 1609459200.Final Answer:
All activities of user123 with Timestamp greater than 1609459200 -> Option AQuick Check:
PartitionKey + SortKey condition filters items [OK]
- Thinking query returns all users' data
- Ignoring SortKey condition
- Assuming syntax error without SortKey equality
What is wrong with this DynamoDB query using composite primary key (UserId as PartitionKey, OrderId as SortKey)?
{
"TableName": "Orders",
"KeyConditionExpression": "OrderId = :oid",
"ExpressionAttributeValues": {
":oid": {"S": "order789"}
}
}Solution
Step 1: Recall query requirements for composite keys
Queries must specify PartitionKey equality in KeyConditionExpression.Step 2: Check the given query
Only SortKey OrderId is used; PartitionKey UserId is missing, causing error.Final Answer:
PartitionKey UserId is missing in KeyConditionExpression -> Option AQuick Check:
PartitionKey must be in query condition [OK]
- Using only SortKey in query condition
- Assuming SortKey alone can identify items
- Ignoring required PartitionKey in queries
You want to store blog posts in DynamoDB. Each post has a AuthorId and a PostDate. You want to quickly find all posts by an author sorted by date. Which composite primary key design is best?
Solution
Step 1: Identify query pattern
You want to find all posts by an author, sorted by date.Step 2: Choose PartitionKey and SortKey accordingly
PartitionKey should be AuthorId to group posts by author; SortKey should be PostDate to sort posts by date.Final Answer:
PartitionKey: AuthorId, SortKey: PostDate -> Option BQuick Check:
Group by author, sort by date = AuthorId + PostDate [OK]
- Swapping PartitionKey and SortKey roles
- Using only one key losing sorting ability
- Choosing SortKey that doesn't support query pattern
