DynamoDB Data Model Tables
Syntax
To create a table in DynamoDB, use the following syntax:
aws dynamodb create-table --table-name TABLE_NAME --attribute-definitions ATTRIBUTE_DEFINITIONS --key-schema KEY_SCHEMA --provisioned-throughput PROVISIONED_THROUGHPUT
Where:
TABLE_NAME
is the name of the table you want to create.ATTRIBUTE_DEFINITIONS
is a list of attribute definitions for the table.KEY_SCHEMA
is a list of key schema elements for the table.PROVISIONED_THROUGHPUT
is the provisioned throughput for the table.
Example
Here's an example of creating a users
table in DynamoDB with a userId
attribute as the primary key:
aws dynamodb create-table --table-name users --attribute-definitions AttributeName=userId,AttributeType=S --key-schema AttributeName=userId,KeyType=HASH --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5
Output
The above command will output the following if successful:
{
"TableDescription": {
"AttributeDefinitions": [
{
"AttributeName": "userId",
"AttributeType": "S"
}
],
"TableName": "users",
"KeySchema": [
{
"AttributeName": "userId",
"KeyType": "HASH"
}
],
"TableStatus": "CREATING",
"CreationDateTime": "2021-10-05T06:18:57.543000+00:00",
"ProvisionedThroughput": {
"NumberOfDecreasesToday": 0,
"ReadCapacityUnits": 5,
"WriteCapacityUnits": 5
},
"TableSizeBytes": 0,
"ItemCount": 0,
"TableArn": "arn:aws:dynamodb:us-east-1:xxxxxxxxxxxx:table/users",
"LocalSecondaryIndexes": [],
"GlobalSecondaryIndexes": []
}
}
Explanation
The above example creates a users
table with userId
as the primary key. The AttributeDefinitions
specifies the userId
attribute is of type S
(string). The KeySchema
specifies that userId
is the HASH
key type, which means it's the primary key for the table. The ProvisionedThroughput
specifies the amount of read and write capacity required for the table.
Use
Tables in DynamoDB are used to store data that can be accessed using the primary key or secondary indexes. You can also create Global Secondary Indexes (GSIs) and Local Secondary Indexes (LSIs) to allow querying the table using non-primary key attributes.
Important Points
- Each table in DynamoDB must have a primary key, which can be either a partition key or a composite partition-sort key.
- DynamoDB supports two types of indexes: Global Secondary Indexes (GSIs) and Local Secondary Indexes (LSIs).
- Tables in DynamoDB have provisioned throughput, which specifies the amount of read and write capacity to allocate to the table.
- DynamoDB is a NoSQL database and does not require a fixed schema.
Summary
DynamoDB Data Model Tables is used to create tables to store data in DynamoDB. Tables must have a primary key, and provisioned throughput must be allocated to the table. DynamoDB supports Global Secondary Indexes and Local Secondary Indexes to allow querying using non-primary key attributes.