A primary key uniquely identifies an item within a DynamoDB table. It contains either one attribute, called the partition key, or two attributes: a partition key and a sort key. Items can have different non-key attributes, but the key attributes and their types must be defined when the table is created. section 12.2.1 section 12.2
Choose the key for the lookups your application needs:
- For a users table, use a unique user ID (`uid`) as the partition key. It identifies one user for retrieval, updating, or deletion. section 12.2.1 section 12.7
- For a tasks table, use `uid` as the partition key and task ID (`tid`) as the sort key. The pair must be unique, but multiple tasks can share a `uid`. This lets you list one user’s tasks in sort-key order. section 12.2.2 section 12.15
Create a users table
With the AWS CLI, run `aws dynamodb create-table --table-name todo-user --attribute-definitions AttributeName=uid,AttributeType=S --key-schema AttributeName=uid,KeyType=HASH --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5`. Here `S` declares a string attribute and `HASH` marks it as the partition key. Check readiness with `aws dynamodb describe-table --table-name todo-user`; the table status should be `ACTIVE`. The key schema cannot be changed afterward. section 12.2.1
$ aws dynamodb create-table --table-name todo-task \ --attribute-definitions AttributeName=uid,AttributeType=S \ #1 AttributeName=tid,AttributeType=N \ --key-schema AttributeName=uid,KeyType=HASH \ AttributeName=tid,KeyType=RANGE \ #2 --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 #1 At least two attributes are needed for a partition key and sort key. #2 The tid attribute is the sort key.
Use this two-attribute table command when tasks must be grouped by user and identified individually. `HASH` assigns `uid` as the partition key; `RANGE` assigns `tid` as the sort key. section 12.2.2
Use `getItem` with every key attribute to fetch one item; it returns either one item or none. Use `query` with a partition-key condition to fetch multiple items that share a partition key, optionally narrowing the result by sort key. A filter is applied after matching items are found, so filtering a large result can waste resources. For another efficient lookup attribute, consider a global secondary index; scanning the whole table is flexible but inefficient. section 12.4.1 section 12.4.2 section 12.15
Check whether the proposed key can collide. If a task’s `tid` is a timestamp, the same user cannot add two tasks with that timestamp. A stable, client-generated ID can also help make retried inserts idempotent: an existing primary key signals that the entry was already created. section 12.2.2 section 16.2.2




















