Scalar Data Types - DynamoDB Data Types and Indexes
Syntax
Scalar data types in DynamoDB can be defined using the following syntax:
{
"AttributeName": {
"DataType": "data_type"
}
}
Here, AttributeName
is the name of the attribute, and data_type
can be one of the following:
S
(String)N
(Number)B
(Binary)BOOL
(Boolean)NULL
(Null)SS
(String set)NS
(Number set)BS
(Binary set)
Example
Let's create a DynamoDB table with scalar data types:
aws dynamodb create-table \
--table-name MyTable \
--attribute-definitions AttributeName=PK,AttributeType=S AttributeName=SK,AttributeType=N \
--key-schema AttributeName=PK,KeyType=HASH AttributeName=SK,KeyType=RANGE \
--billing-mode PAY_PER_REQUEST
In this example, we have two scalar data types: S
for strings and N
for numbers.
Output
The output of the above command will create a DynamoDB table with two attributes: PK
(string) and SK
(number).
Explanation
S
(String): This data type is used for storing string values. You can use this data type for attributes such as name, email, address, etc.N
(Number): This data type is used for storing numeric values. You can use this data type for attributes such as age, price, quantity, etc.B
(Binary): This data type is used for storing binary data. You can use this data type for attributes such as images, videos, files, etc.BOOL
(Boolean): This data type is used for storing boolean values (true or false).NULL
(Null): This data type is used for storing null values.SS
(String set): This data type is used for storing a set of strings.NS
(Number set): This data type is used for storing a set of numbers.BS
(Binary set): This data type is used for storing a set of binary data.
Use
Scalar data types are widely used in DynamoDB tables for storing data. You can use these data types to create and define your attributes, and perform operations on them.
Important Points
- You should be careful while selecting the appropriate data type for your attributes, as it can affect the performance and cost of your table.
- DynamoDB supports a range of data types, so you should select the appropriate one based on your use case.
- You can define a data type for an attribute using JSON or through AWS CLI.
Summary
Scalar data types are an essential part of the DynamoDB database. They help you in defining and storing data in your tables. You can use different data types according to your use case to store data in the most efficient and cost-effective way.