How to Create Table in DynamoDB: Syntax and Example
To create a table in DynamoDB, use the
CreateTable operation specifying the TableName, KeySchema, and AttributeDefinitions. You also set the ProvisionedThroughput to define read and write capacity units.Syntax
The CreateTable operation requires these main parts:
- TableName: The name of your table.
- KeySchema: Defines the primary key with
AttributeNameandKeyType(HASH for partition key, RANGE for sort key). - AttributeDefinitions: Lists attributes used in the key schema with their data types.
- ProvisionedThroughput: Sets read and write capacity units for the table.
json
{
"TableName": "string",
"KeySchema": [
{ "AttributeName": "string", "KeyType": "HASH" | "RANGE" }
],
"AttributeDefinitions": [
{ "AttributeName": "string", "AttributeType": "S" | "N" | "B" }
],
"ProvisionedThroughput": {
"ReadCapacityUnits": number,
"WriteCapacityUnits": number
}
}Example
This example creates a table named Users with a partition key UserId of type string and sets provisioned throughput.
javascript
const AWS = require('aws-sdk'); const dynamodb = new AWS.DynamoDB({ region: 'us-east-1' }); const params = { TableName: 'Users', KeySchema: [ { AttributeName: 'UserId', KeyType: 'HASH' } ], AttributeDefinitions: [ { AttributeName: 'UserId', AttributeType: 'S' } ], ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 } }; dynamodb.createTable(params, (err, data) => { if (err) { console.error('Error creating table:', err); } else { console.log('Table created successfully:', data.TableDescription.TableName); } });
Output
Table created successfully: Users
Common Pitfalls
Common mistakes when creating DynamoDB tables include:
- Not defining the
KeySchemacorrectly (missing HASH key or wrong KeyType). - Attribute types in
AttributeDefinitionsnot matching the key schema attributes. - Setting
ProvisionedThroughputtoo low or forgetting it (for on-demand tables, this is not needed). - Using reserved words as attribute names.
Always double-check your key schema and attribute definitions before creating the table.
javascript
/* Wrong: Missing HASH key in KeySchema */ const wrongParams = { TableName: 'WrongTable', KeySchema: [ { AttributeName: 'UserId', KeyType: 'RANGE' } // HASH key missing ], AttributeDefinitions: [ { AttributeName: 'UserId', AttributeType: 'S' } ], ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 } }; /* Right: Include HASH key */ const rightParams = { TableName: 'RightTable', KeySchema: [ { AttributeName: 'UserId', KeyType: 'HASH' } ], AttributeDefinitions: [ { AttributeName: 'UserId', AttributeType: 'S' } ], ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 } };
Quick Reference
| Parameter | Description | Example |
|---|---|---|
| TableName | Name of the table | Users |
| KeySchema | Defines primary key attributes and types | [{AttributeName: 'UserId', KeyType: 'HASH'}] |
| AttributeDefinitions | Attributes used in keys with data types | [{AttributeName: 'UserId', AttributeType: 'S'}] |
| ProvisionedThroughput | Read and write capacity units | {ReadCapacityUnits: 5, WriteCapacityUnits: 5} |
Key Takeaways
Always define a partition key (HASH) in the KeySchema when creating a DynamoDB table.
AttributeDefinitions must match the attributes used in the KeySchema with correct data types.
ProvisionedThroughput sets the read/write capacity and is required unless using on-demand mode.
Avoid using reserved words as attribute names to prevent errors.
Use AWS SDK methods like createTable with proper parameters to create tables programmatically.