Skip to content
Related Articles

Related Articles

Improve Article
DynamoDB – Primary Key
  • Last Updated : 03 Jul, 2020

Primary keys are used for uniquely identifying each item in a table. No two-item can have the same primary key. In DynamoDB the primary key must be specified along with the table name while creating a table.
DynamoDB supports two different kinds of primary keys:

  • Partition key
  • Partition key and sort key

Partition key –

It is a simple primary key, composed of one attribute.DynamoDB utilizes the partition key’s value as input to an internal hash function. The output from that hash function sets the partition (physical storage internal to DynamoDB) where the item is to be stored. Table having only the partition key is not allowed to have more than one item with the same partition key value. For instance, look into the below-depicted table:

The People table has a simple primary key (PersonID). You can access any item in the People table directly by providing the PersonId value for that item.

Partition key and sort key –

Also referred to as composite primary key, this key comprises of two attributes, namely, partition key and sort key.DynamoDB uses the partition key value as input to an internal hash function. The output from the hash function sets the partition in which the item will be stored. Each item with the same partition key value are stored together, in sorted order by sort key value. In a table that has a partition key and a sort key, it’s allowed for two items to have the same partition key value assuming those items have different sort key values. for instance, look into the database of Music depicted below:

The Music table above is an example of a table with a composite primary key (Artist and SongTitle). You can access any item in the Music table directly, if you provide the Artist and SongTitle values for that item. A composite primary key provides users with additional flexibility when querying data. For example, if you provide only the value for Artist, DynamoDB retrieves all of the songs by that artist. To get only a subset of songs by a particular artist, you can provide a value for Artist along with a range of values for SongTitle.

My Personal Notes arrow_drop_up
Recommended Articles
Page :