Skip to content
Quest 02Beginner15 min

Simple Primary Key in Action

We'll store each Pokémon by its unique Name as the Partition Key.

What you'll learn

  • Choose a partition key that is unique and frequently queried.
  • Only key attributes have a declared type: every item's Name must be a String. Other attributes are schemaless.
  • Key values are case-sensitive: "Bulbasaur" and "bulbasaur" are two different items.

Section 01

Scenario

We'll store each Pokémon by its unique Name as the Partition Key.

Section 02

Table Definition

Define a DynamoDB table with Name as the partition key.

AWS CDK (TypeScript)TypeScript
const table = new dynamodb.Table(this, "PokemonTable", {
  tableName: "Pokemon",
  partitionKey: { name: "Name", type: dynamodb.AttributeType.STRING },
});

Section 03

Insert Item Example

With the document client you pass plain JavaScript values in Item. PutItem is a full replace: writing the same Name again overwrites the whole item (Quest 5 shows how to guard against that).

PutCommand input (document client)JavaScript
{
  TableName: "Pokemon",
  Item: {
    Name: "Bulbasaur",
    Type1: "grass",
    Type2: "poison",
    Height_m: 0.7,
    Weight_kg: 6.9
  }
}

Section 04

Get Item Example

Retrieve the Pokémon by supplying the same partition key value:

GetCommand input (document client)JavaScript
{
  TableName: "Pokemon",
  Key: { Name: "Bulbasaur" }
}
Your appAWS SDK v3DynamoDBtableP0P1P2P3CONSUMED0

Ready · 10 steps · Amp

This animation plays when you scroll to it. Press Play to start it now.

All steps as text
  1. Your app sends PutItem to the Pokemon table with Name = "Pikachu".
  2. DynamoDB hashes "Pikachu" to a6bb8b2f, which routes the request to partition P3.
  3. P3 stores Pikachu (Type Electric, Level 5).
  4. This call consumed 1 WCU.
  5. DynamoDB responds with success: Pikachu stored under its Name.
  6. Your app sends GetItem to the Pokemon table with Name = "Pikachu".
  7. DynamoDB hashes "Pikachu" to a6bb8b2f, which routes the request to partition P3.
  8. P3 returns Pikachu.
  9. This call consumed 0.5 RCU.
  10. DynamoDB responds with success: one key, one partition, one item.

Check

Check your understanding

Quick Quiz · one question, as many tries as you need.

Which attribute is used as the partition key here?

Choose one answer.