📖 Scenario: You are building a simple inventory system for a small store. Instead of using multiple tables, you will use a single DynamoDB table to store different types of items like products and categories.
🎯 Goal: Create a single DynamoDB table called StoreInventory that holds both product and category data using a single-table design. You will set up the initial data, add a configuration for item types, write the core logic to insert items with proper keys, and complete the table setup.
📋 What You'll Learn
Create a DynamoDB table named StoreInventory with a partition key called PK and a sort key called SK.
Add a configuration variable item_type to distinguish between products and categories.
Insert product and category items using the single-table design pattern with composite keys.
Complete the table setup with proper attribute definitions and key schema.
💡 Why This Matters
🌍 Real World
Single-table design in DynamoDB is used to efficiently store and query multiple related entity types in one table, reducing complexity and improving performance.
💼 Career
Understanding single-table design is essential for AWS developers and database engineers working with DynamoDB to build scalable and cost-effective applications.
Progress0 / 4 steps
1
Create the DynamoDB table structure
Create a DynamoDB table named StoreInventory with a partition key called PK and a sort key called SK. Use the AWS CLI command aws dynamodb create-table with the exact attribute names and key types.
DynamoDB
Hint
Use the AWS CLI create-table command with --attribute-definitions and --key-schema options.
2
Add item type configuration
Create a variable called item_type and set it to the string "PRODUCT". This will help identify the type of item stored in the table.
DynamoDB
Hint
Just assign the string "PRODUCT" to the variable item_type.
3
Insert product and category items using single-table design
Write two AWS CLI commands to insert one product and one category item into the StoreInventory table. Use PK and SK as composite keys. For the product, set PK to "PRODUCT#123" and SK to "METADATA#123". For the category, set PK to "CATEGORY#456" and SK to "METADATA#456". Include the item_type attribute with values "PRODUCT" and "CATEGORY" respectively.
DynamoDB
Hint
Use aws dynamodb put-item with JSON for the --item parameter. Make sure keys and item_type match exactly.
4
Complete the table setup with attribute definitions and key schema
Add the final attribute definitions and key schema lines to the aws dynamodb create-table command to complete the StoreInventory table setup. Ensure the partition key is PK of type string and the sort key is SK of type string.
DynamoDB
Hint
Make sure the create-table command includes both attribute definitions and key schema for PK and SK.
Practice
(1/5)
1. What is the main advantage of using single-table design in DynamoDB?
easy
A. It requires multiple tables for each data type to keep data separate.
B. It allows storing different types of related data in one table for faster queries.
C. It automatically creates indexes for all attributes without setup.
D. It stores data only in JSON format for easy reading.
Solution
Step 1: Understand single-table design purpose
Single-table design stores multiple related data types in one table to reduce the number of queries needed.
Step 2: Compare options with this purpose
It allows storing different types of related data in one table for faster queries. matches this purpose. Options B, C, and D describe incorrect or unrelated features.
Final Answer:
It allows storing different types of related data in one table for faster queries. -> Option B
Quick Check:
Single-table design = faster queries with one table [OK]
Hint: Single-table design means one table for related data [OK]
Common Mistakes:
Thinking single-table means multiple tables
Assuming automatic index creation
Confusing data format with design method
2. Which of the following is the correct way to define a partition key in a DynamoDB table using single-table design?
easy
A. PartitionKey: orderId, SortKey: userId
B. PartitionKey: userId#orderId
C. PartitionKey: userId, SortKey: orderId
D. PartitionKey: orderId#userId
Solution
Step 1: Recall key structure in single-table design
Single-table design uses a partition key and a sort key to organize data efficiently.
Step 2: Identify correct key assignment
PartitionKey: userId, SortKey: orderId correctly assigns userId as partition key and orderId as sort key, which is common for grouping orders by user.
Final Answer:
PartitionKey: userId, SortKey: orderId -> Option C
Quick Check:
Partition and sort keys = userId and orderId [OK]
Hint: Partition key groups items; sort key orders them [OK]
Common Mistakes:
Using combined keys as partition key only
Swapping partition and sort keys incorrectly
Using concatenated keys without sort key
3. Given a DynamoDB table with partition key PK and sort key SK, which query will fetch all orders for user with ID 'user123' if orders are stored with PK = 'USER#user123' and SK = 'ORDER#orderId'?
medium
A. Query with KeyConditionExpression: PK = 'ORDER#orderId' AND SK = 'USER#user123'
B. Query with KeyConditionExpression: PK = 'USER#user123' AND SK = 'ORDER#orderId'
C. Scan with FilterExpression: PK = 'USER#user123'
D. Query with KeyConditionExpression: PK = 'USER#user123' AND SK begins_with 'ORDER#'
Solution
Step 1: Understand key structure for orders
Orders have PK as 'USER#user123' and SK starting with 'ORDER#', so all orders share the same PK but different SKs.
Step 2: Identify query to fetch all orders for user
Query with KeyConditionExpression: PK = 'USER#user123' AND SK begins_with 'ORDER#' queries PK equal to 'USER#user123' and SK that begins with 'ORDER#', which fetches all orders for that user.
Final Answer:
Query with KeyConditionExpression: PK = 'USER#user123' AND SK begins_with 'ORDER#' -> Option D
Quick Check:
PK fixed, SK prefix filter = all user orders [OK]
Hint: Use begins_with on sort key to get related items [OK]
Common Mistakes:
Swapping PK and SK in query
Using scan instead of query
Filtering SK with exact match instead of prefix
4. You wrote this query to get all comments for a post in a single-table design DynamoDB table:
KeyConditionExpression: PK = 'POST#123' AND SK = 'COMMENT#'
Why does this query return no results?
medium
A. Because SK uses '=' instead of 'begins_with' to match multiple comments.
B. Because PK should be 'COMMENT#' and SK should be 'POST#123'.
C. Because the table does not support queries on SK.
D. Because the query syntax is missing a FilterExpression.
Solution
Step 1: Analyze the KeyConditionExpression
The query uses SK = 'COMMENT#', which looks for an exact match on SK.
Step 2: Understand why no results appear
Comments likely have SK starting with 'COMMENT#' plus an ID, so exact match fails. Using 'begins_with' on SK is needed to get all comments.
Final Answer:
Because SK uses '=' instead of 'begins_with' to match multiple comments. -> Option A
Quick Check:
Use begins_with for multiple SK matches [OK]
Hint: Use begins_with for partial sort key matches [OK]
Common Mistakes:
Using '=' instead of begins_with on SK
Swapping PK and SK in query
Assuming FilterExpression fixes key condition
5. You want to design a single DynamoDB table to store users, their orders, and order items. Which key design best supports fetching all orders and items for a user efficiently?
hard
A. PartitionKey: USER#userId, SortKey: ORDER#orderId for orders; PartitionKey: ORDER#orderId, SortKey: ITEM#itemId for items
B. PartitionKey: userId, SortKey: orderId#itemId for all data types
C. PartitionKey: USER#userId, SortKey: ORDER#orderId#ITEM#itemId for all items and orders
D. PartitionKey: ITEM#itemId, SortKey: ORDER#orderId for all data
Solution
Step 1: Understand data grouping needs
We want to fetch all orders and items for a user efficiently, so grouping orders by user and items by order is key.
Step 2: Evaluate key designs
PartitionKey: USER#userId, SortKey: ORDER#orderId for orders; PartitionKey: ORDER#orderId, SortKey: ITEM#itemId for items uses USER#userId as partition key for orders and ORDER#orderId as partition key for items, allowing efficient queries by user and order. Other options mix keys or combine too much, making queries inefficient.
Final Answer:
PartitionKey: USER#userId, SortKey: ORDER#orderId for orders; PartitionKey: ORDER#orderId, SortKey: ITEM#itemId for items -> Option A
Quick Check:
Group orders by user, items by order = PartitionKey: USER#userId, SortKey: ORDER#orderId for orders; PartitionKey: ORDER#orderId, SortKey: ITEM#itemId for items [OK]
Hint: Group data by parent keys for efficient queries [OK]
Common Mistakes:
Using one partition key for all data types
Combining all keys into one long sort key
Using item ID as partition key for all data
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.