0
0
DynamoDBquery~30 mins

Composite primary key in DynamoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
Create a DynamoDB Table with Composite Primary Key
📖 Scenario: You are building a simple database for an online bookstore. Each book can have multiple editions, and you want to store information about each edition separately. To uniquely identify each edition, you will use a composite primary key in DynamoDB.
🎯 Goal: Create a DynamoDB table named Books with a composite primary key consisting of ISBN as the partition key and Edition as the sort key.
📋 What You'll Learn
Create a DynamoDB table named Books
Use ISBN as the partition key (string type)
Use Edition as the sort key (number type)
Add an attribute Title (string type) to store the book title
💡 Why This Matters
🌍 Real World
Composite primary keys help uniquely identify items in databases where a single attribute is not enough, such as different editions of the same book.
💼 Career
Understanding composite keys is essential for designing efficient NoSQL databases like DynamoDB, commonly used in cloud applications.
Progress0 / 4 steps
1
Define the Table Name and Partition Key
Create a variable called table_name and set it to the string 'Books'. Then create a dictionary called key_schema with one entry: a dictionary with AttributeName set to 'ISBN' and KeyType set to 'HASH'.
DynamoDB
Need a hint?

The partition key is called the HASH key in DynamoDB. Use a list with one dictionary for key_schema.

2
Add the Sort Key to the Key Schema
Add a second dictionary to the key_schema list with AttributeName set to 'Edition' and KeyType set to 'RANGE' to define the sort key.
DynamoDB
Need a hint?

The sort key is called the RANGE key in DynamoDB. Add it as the second item in the key_schema list.

3
Define Attribute Definitions
Create a list called attribute_definitions with two dictionaries: one for ISBN with AttributeType set to 'S' (string), and one for Edition with AttributeType set to 'N' (number).
DynamoDB
Need a hint?

Attribute types use 'S' for string and 'N' for number in DynamoDB.

4
Add Table Creation Parameters
Create a dictionary called table_params with keys: TableName set to table_name, KeySchema set to key_schema, AttributeDefinitions set to attribute_definitions, and BillingMode set to 'PAY_PER_REQUEST'.
DynamoDB
Need a hint?

Use PAY_PER_REQUEST billing mode for simplicity.