Creating a Table with a Composite Primary Key
Start course
1h 32m

Please note this course is outdated and has been replaced with the following courses:


This course provides an introduction to working with Amazon DynamoDB, a fully-managed NoSQL database service provided by Amazon Web Services. We begin with a description of DynamoDB and compare it to other database platforms. The course continues by walking you through designing tables, and reading and writing data, which is somewhat different than other databases you may be familiar with. We conclude with more advanced topics including secondary indexes and how DynamoDB handles very large tables.

Course Objectives

You will gain the following skills by completing this course:

  • How to create DynamoDB tables.
  • How to read and write data.
  • How to use queries and scans.
  • How to create and query secondary indexes.
  • How to work with large tables. 

Intended Audience

You should take this course if you have:

  • An understanding of basic AWS technical fundamentals.
  • Awareness of basic database concepts, such as tables, rows, indexes, and queries.
  • A basic understanding of computer programming. The course includes some programming examples in Python.


See the Intended Audience section.

This Course Includes

  • Expert-guided lectures about Amazon DynamoDB.
  • 1 hour and 31 minutes of high-definition video. 
  • Expert-level instruction from an industry veteran. 

What You'll Learn

Video Lecture What You'll Learn
DynamoDB Basics A basic and foundational overview of DynamoDB.
Creating DynamoDB Tables How to create DynamoDB tables and understand key concepts.
Reading and Writing Data How to use the AWS Console and API to read and write data.
Queries and Scans How to use queries and scans with the AWS Console and API.
Secondary Indexes How to work with Secondary Indexes.
Working with Large Tables How to use partitioning in large tables.

If you have thoughts or suggestions for this course, please contact Cloud Academy at


In the last video, we created a table with a single primary key attribute called the partition key. DynamoDB also lets you create tables that use two attributes as the unique identifier. One field is the partition key, also known as the hash key, and the other is the sort key, sometimes called the range key. This structure can be useful if your data has a hierarchy.

Here's one example of when you might want to use a compound primary key. Say you've created a table that's logging some actions performed by users on your site, like every time they logged in to the site. Every action has a username and a numeric timestamp, representing when the action happened. It might make sense to use a composite key to uniquely identify each action. The partition key would be the username and the sort key would be the timestamp. You can have many items with the same username and many items with the same timestamp, as long as you don't have any records with the same username and the same timestamp. By using a composite primary key here, it would be easy to find a single action and to query for all the actions taken by a single user.

Here's another example, keeping track of the high scores that users have gotten on some mobile games. Here's a situation where there are many users and there are a handful of games, but each user should only have one high score for each game. In this example, you could use the user ID as the partition key and the game title as the sort key. The unique identifier for each item would be the combination of the user ID and the game title.

Here's one last example, which is the one that we're going to use for this demonstration. Each time a customer places an order from our online store, they might purchase many different items. Each of those line items will become an entry in the Order Line Items table. The partition key will be the order ID and the sort key will be the line number for that order.

Here's another way to visualize that table. You can see that order 630178 has two line items for two different products and both of them are unshipped. The next order ID, 607161, only has one line item. Next, order 672102 has three line items. Some of them are shipped and some of them are unshipped. This is the structure that we'll design in the AWS console.

Let's go back to the DynamoDB console and create this table. Now that we already have one table in our account, the DynamoDB console loads with a dashboard of summary information. Let's click Create Table again to build the table for our order line items. The table name, Order Line Items, makes sense here. As we just discussed, we're going to use a composite primary key, so we'll need to check the checkbox for Add Sort Key. Now, you can see that the form asks for two primary key fields, the partition key and the sort key. Just like in the Orders table, our partition key will be order ID, which is a number, and the sort key will be the line number within each order. That's also a number. This time we can go ahead and use the default settings. Since we are not going to make any other changes to the configuration here, let's click Create. We're taken back to the list of tables, and you can see that the table is being created. After a minute or so, the table status will change to active and we'll be able to start using it.

About the Author

Ryan is the Storage Operations Manager at Slack, a messaging app for teams. He leads the technical operations for Slack's database and search technologies, which use Amazon Web Services for global reach.

Prior to Slack, Ryan led technical operations at Pinterest, one of the fastest-growing social networks in recent memory, and at Runscope, a debugging and testing service for APIs.

Ryan has spoken about patterns for modern application design at conferences including Amazon Web Services re:Invent and O'Reilly Fluent. He has also been a mentor for companies participating in the 500 Startups incubator.