Skip to content
Related Articles

Related Articles

AWS DynamoDB – Primary Key

View Discussion
Improve Article
Save Article
  • Difficulty Level : Expert
  • Last Updated : 19 Aug, 2021

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:

A partition key is a type of primary key which is used by DynamoDB as input values for internal hash functions. These hash functions are further responsible for generating partitions where the data items can be stored. 

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 :

Start Your Coding Journey Now!