What is Hash and Range Primary Key in DynamoDB?

When you create a table, in addition to the table name, you must specify the primary key of the table. The primary key uniquely identifies each item in the table, so that no two items can have the same key.

DynamoDB supports two different kinds of primary keys:

Simple Primary Key (Hash Key): A simple primary key, composed of one attribute known as the Hash key (partition key). DynamoDB uses the hash key’s value as input to an internal hash function. The output from the hash function determines the partition (physical storage internal to DynamoDB) in which the item will be stored. In a table that has only a hash key, no two items can have the same hash key value.

Example:

Composite Primary Key (Hash Key + Range Key): Referred to as a composite primary key, this type of key is composed of two attributes. The first attribute is the Hash Key (partition key), and the second attribute is the Range Key (sort key). DynamoDB uses the hash key value as input to an internal hash function. The output from the hash function determines the partition (physical storage internal to DynamoDB) in which the item will be stored. All items with the same hash key value are stored together, in sorted order by range key (sort key) value. In a table that has a hash key and a range key, it’s possible for multiple items to have the same hash key value. However, those items must have different range key values.

Example:

Leave a Reply