DynamoDB supports many different data types for attributes within a table. They can be categorized as follows:
- Scalar Types – A scalar type can represent exactly one value. The scalar types are number, string, binary, Boolean, and null.
- Document Types – A document type can represent a complex structure with nested attributes, such as you would find in a JSON document. The document types are lists and maps.
- Set Types – A set type can represent multiple scalar values. The set types are string set, number set, and binary set.
When you create a table or a secondary index, you must specify the names and data types of each primary key attribute (partition key and sort key). Furthermore, each primary key attribute must be defined as type string, number, or binary.
DynamoDB is a NoSQL database and is schemaless. This means that, other than the primary key attributes, you don’t have to define any attributes or data types when you create tables. By comparison, relational databases require you to define the names and data types of each column when you create a table.
The following are descriptions of each data type, along with examples in JSON format.
Scalar Types
The scalar types are number, string, binary, Boolean, and null.
Number:
Numbers can be positive, negative, or zero. Numbers can have up to 38 digits of precision. Exceeding this results in an exception.
- Positive range: 1E-130 to 9.9999999999999999999999999999999999999E+125
- Negative range: -9.9999999999999999999999999999999999999E+125 to -1E-130
In DynamoDB, numbers are represented as variable length. Leading and trailing zeroes are trimmed.
All numbers are sent across the network to DynamoDB as strings, to maximize compatibility across languages and libraries. However, DynamoDB treats them as number type attributes for mathematical operations.
Note: If number precision is required the numbers can be passed to DynamoDB as a string type which can be later converted into number type.
You can use the number data type to represent a date or a timestamp. One way to do this is by using epoch time ie, the number of seconds since 00:00:00 UTC on 1 January 1970.
String:
Strings are Unicode with UTF-8 binary encoding. The minimum length of a string can be zero, if the attribute is not used as a key for an index or table, and is constrained by the maximum DynamoDB item size limit of 400 KB.
The following additional constraints apply to primary key attributes that are defined as type string:
- For a simple primary key, the maximum length of the first attribute value (the partition key) is 2048 bytes.
- For a composite primary key, the maximum length of the second attribute value (the sort key) is 1024 bytes.
DynamoDB collates and compares strings using the bytes of the underlying UTF-8 string encoding. For example, “a” (0x61) is greater than “A” (0x41), and “¿” (0xC2BF) is greater than “z” (0x7A).
You can use the string data type to represent a date or a timestamp. One way to do this is by using ISO 8601 strings, as shown in these examples:
- 2020-06-12
- 2020-06-12T17:42:34Z
- 2020612T174234Z
Binary:
Binary type attributes can store any binary data, such as compressed text, encrypted data, or images. Whenever DynamoDB compares binary values, it treats each byte of the binary data as unsigned.
The length of a binary attribute can be zero, if the attribute is not used as a key for an index or table, and is constrained by the maximum DynamoDB item size limit of 400 KB.
If you define a primary key attribute as a binary type attribute, the following additional constraints apply:
- For a simple primary key, the maximum length of the first attribute value (the partition key) is 2048 bytes.
- For a composite primary key, the maximum length of the second attribute value (the sort key) is 1024 bytes.
Your applications must encode binary values in base64-encoded format before sending them to DynamoDB. Upon receipt of these values, DynamoDB decodes the data into an unsigned byte array and uses that as the length of the binary attribute.
The following example is a binary attribute, using base64-encoded text.
dGhpcyB0ZXh0IGlzIGJhc2U2NC1lbmNvZGVk
Boolean:
A Boolean type attribute can store either true or false.
Null:
Null represents an attribute with an unknown or undefined state.
Document Types
The document types are list and map. These data types can be nested within each other, to represent complex data structures up to 32 levels deep.
There is no limit on the number of values in a list or a map, as long as the item containing the values fits within the DynamoDB item size limit (400 KB).
An attribute value can be an empty string or empty binary value if the attribute is not used for a table or index key. An attribute value cannot be an empty set (string set, number set, or binary set), however, empty lists and maps are allowed. Empty string and binary values are allowed within lists and maps.
List:
A list type attribute can store an ordered collection of values. Lists are enclosed in square brackets: [ … ]
A list is similar to a JSON array. There are no restrictions on the data types that can be stored in a list element, and the elements in a list element do not have to be of the same type.
The following example shows a list that contains two strings and a number.
FavoriteStuff: ["Cola", "Coffee", 3.14159]
Map:
A map type attribute can store an unordered collection of name-value pairs. Maps are enclosed in curly braces: { … }
A map is similar to a JSON object. There are no restrictions on the data types that can be stored in a map element, and the elements in a map do not have to be of the same type.
Maps are ideal for storing JSON documents in DynamoDB. The following example shows a map that contains a string, a number, and a nested list that contains another map.
{
Day: "Monday",
UnreadEmails: 42,
ItemsOnMyDesk: [
"Coffee Cup",
"Telephone",
{
Pens: { Quantity : 3},
Pencils: { Quantity : 2},
Erasers: { Quantity : 1}
}
]
}
Sets:
DynamoDB supports types that represent sets of number, string, or binary values. All the elements within a set must be of the same type. For example, an attribute of type Number Set can only contain numbers; String sets can only contain strings; and so on.
There is no limit on the number of values in a set, as long as the item containing the values fits within the DynamoDB item size limit (400 KB).
Each value within a set must be unique. The order of the values within a set is not preserved. Therefore, your applications must not rely on any particular order of elements within the set. DynamoDB does not support empty sets, however, empty string and binary values are allowed within a set.
The following example shows a string set, a number set, and a binary set:
["Black", "Green", "Red"]
[42.2, -19, 7.5, 3.14]
["U3Vubnk=", "UmFpbnk=", "U25vd3k="]
Similar Reads
DynamoDB - Creating Items
Creating an item in a DynamoDB table is a vital operation in application development that allows you to upload data. We will explore how we can create items for tables using different methods used for different purposes. Amazon DynamoDB is a fully managed NoSQL database service that provides fast an
5 min read
DynamoDB - Create Table
Tables are the backbone of any Relational Database Management System. Tables are used to store data in an organized form so operation on the data and retrieval of data becomes seamless. The AWS DynamoDB provides both RDBMS and Non-relational databases. This article mainly focuses on the relational p
2 min read
Delete Table In DynamoDB
DynamoDB allows users to create databases capable of storing and retrieving any amount of data and comes in handy while serving any amount of traffic. It dynamically manages each customerâs request and provides high performance by automatically distributing data and traffic over servers. It is a ful
4 min read
AWS DynamoDB - Web Based Setup
To use the Web-based services of AWS- DynamoDB the following steps need to be followed: Create an AWS account by navigating to the following link:- https://portal.aws.amazon.com/billing/signup#/startGet the AWS access Key (used to access DynamoDB programmatically).Configure your credentials (used to
2 min read
AWS DynamoDB - Update Data in a Table
Amazon DynamoDB is a NoSQL managed database that supports semi-structured data i.e. key-value and document data. A DynamoDB table stores data in the form of an item. While creating a table in DynamoDB, a partition key needs to be defined which acts as the primary key for the table and no schema. Eac
2 min read
AWS DynamoDB - Primary Key
Primary keys are used for uniquely identifying each item in a table. No two-item can have the same primary key. In DynamoDB the primary key must be specified along with the table name while creating a table. DynamoDB supports two different kinds of primary keys: Partition keyPartition key and sort k
2 min read
AWS DynamoDB - Write Data to a Table
In this article, we will write data to a table in DynamoDB. DynamoDB is a NoSQL database that supports semi-structured data i.e. key-value and document data structures. A DynamoDB table contains items. An item is a collection of attributes and each item has a primary key that should be not null. Als
2 min read
AWS DynamoDB - Query Data in a Table
Amazon DynamoDB is a NoSQL managed database that stores semi-structured data i.e. key-value pair and document data. A table in DynamoDB stores data in form of an item and each item has a primary key. Example: { "Color": true, "Director": "Christopher Nolan", "MovieID": 1, "Name": "Inception", "Ratin
2 min read
DynamoDB - SQL and NOSQL
If you are an application developer, you might have some experience using a relational database management system (RDBMS) and Structured Query Language (SQL). As you begin working with Amazon DynamoDB, you will encounter many similarities, but also many things that are different. This article descri
6 min read
AWS DynamoDB - Read Data from a Table
AWS DynamoDB is a NoSQL managed database that stores semi-structured data i.e. key-value and document data. It stores data in form of an item. An item consists of attributes. Upon table creation in DynamoDB, it only requires a primary key to differentiate between items and no schema is to be defined
3 min read