dynamo-db
  1. dynamo-db-primary-keys

Primary Keys - DynamoDB Data Model

Syntax

The syntax for defining a primary key in DynamoDB is as follows:

{
  "TableName": "table-name",
  "KeySchema": [
    {
        "AttributeName": "attribute-name",
        "KeyType": "HASH"
    },
    {
        "AttributeName": "sort-key-name",
        "KeyType": "RANGE"
    }
  ],
  "AttributeDefinitions": [
    {
        "AttributeName": "attribute-name",
        "AttributeType": "S"
    },
    {
        "AttributeName": "sort-key-name",
        "AttributeType": "N"
    }
  ]
}

Example

Suppose we want to create a table to store information about books. We can use the following code to define a primary key:

{
  "TableName": "book-table",
  "KeySchema": [
    {
        "AttributeName": "title",
        "KeyType": "HASH"
    },
    {
        "AttributeName": "published",
        "KeyType": "RANGE"
    }
  ],
  "AttributeDefinitions": [
    {
        "AttributeName": "title",
        "AttributeType": "S"
    },
    {
        "AttributeName": "published",
        "AttributeType": "N"
    }
  ]
}

Output

The output of the above example would be a table named "book-table" with a primary key consisting of two attributes - "title" and "published". The "title" attribute is the hash key and the "published" attribute is the range key.

Explanation

In DynamoDB, a primary key consists of one or two attributes. The first attribute is the hash key (also known as partition key) and the second attribute is the range key (also known as sort key).

The hash key is used to partition the data across multiple nodes in the DynamoDB cluster. Each item in the table must have a unique hash key. The range key is used to sort the data within each partition. Items with the same hash key are ordered based on the range key.

Use

The primary key is important to consider when designing the data model for a DynamoDB table because it impacts the scalability and performance of the table. A well-designed primary key can help distribute data evenly across nodes and improve query performance.

Important Points

  • A primary key can consist of one or two attributes.
  • The first attribute is the hash key (partition key) and the second attribute is the range key (sort key).
  • The hash key must be unique for each item in the table.
  • Items with the same hash key are sorted based on the range key.
  • A well-designed primary key can improve query performance and distribute data evenly across nodes.

Summary

In DynamoDB, the primary key is a fundamental part of the data model. It determines how the data is partitioned and sorted in the table. A well-designed primary key can improve the performance and scalability of the table.

Published on: