| Did this page help you? Yes No Tell us about it... |
Amazon DynamoDB data model concepts include tables, items and attributes.
In Amazon DynamoDB, a database is a collection of tables. A table is a collection of items and each item is a collection of attributes.
In a relational database, a table has a predefined schema such as the table name, primary key, list of its column names and their data types. All records stored in the table must have the same set of columns. Amazon DynamoDB is a NoSQL database. That is, except for the required primary key, an Amazon DynamoDB table is schema-less. Individual items in an Amazon DynamoDB table can have any number of attributes, although there is a limit of 64 KB on the item size. An item size is the sum of lengths of its attribute names and values (binary and UTF-8 lengths).
Each attribute in an item is a name-value pair. An attribute can be single-valued or multi-valued set. For example, a book item can have title and authors attributes. Each book has one title but can have many authors. The multi-valued attribute is a set, duplicate values are not allowed.
For example, consider storing catalog of products in Amazon DynamoDB. So you might create a table, ProductCatalog, with the Id attribute as its primary key.
ProductCatalog ( Id, ... )
You can store various kinds of product items in the table. The following table shows sample product items.
| Example items |
|---|
{
Id = 101
ProductName = "Book 101 Title"
ISBN = "111-1111111111"
Authors = [ "Author 1", "Author 2" ]
Price = -2
Dimensions = "8.5 x 11.0 x 0.5"
PageCount = 500
InPublication = 1
ProductCategory = "Book"
}
|
{
Id = 201
ProductName = "18-Bicycle 201"
Description = "201 description"
BicycleType = "Road"
Brand = "Brand-Company A"
Price = 100
Gender = "M"
Color = [ "Red", "Black" ]
ProductCategory = "Bike"
}
|
{
Id = 202
ProductName = "21-Bicycle 202"
Description = "202 description"
BicycleType = "Road"
Brand = "Brand-Company A"
Price = 200
Gender = "M"
Color = [ "Green", "Black" ]
ProductCategory = "Bike"
}
|
In the example, the ProductCatalog table has one book item and two bicycle items. Item 101 is a book with many attributes including the Authors multi-valued attribute. Item 201 and 202 are bikes and these items have Color multi-valued attribute. The Id is the only required attribute. Note that attribute values are shown using JSON-like syntax for illustration purposes.
Amazon DynamoDB does not allow null or empty string attribute values.
When you create a table, in addition to the table name, you must specify the primary key of the table. Amazon DynamoDB supports the following two types of primary keys:
Hash Type Primary Key—In this case the primary key is made of one attribute, a hash attribute. Amazon DynamoDB builds an unordered hash index on this primary key attribute. In the preceding example, the ProductCatalog has Id as its primary key. It is the hash attribute.
Hash and Range Type Primary Key—In this case, the primary key is made of two attributes. The first attributes is the hash attribute and the second one is the range attribute. Amazon DynamoDB builds an unordered hash index on the hash primary key attribute and a sorted range index on the range primary key attribute. For example, Amazon Web Services maintain several forums (see Discussion Forums). Each forum has many threads of discussion and each thread has many replies. You can potentially model this by creating the following three tables:
| Table Name | Primary Key Type | Hash Attribute Name | Range Attribute Name |
|---|---|---|---|
| Forum ( Name, ... ) | Hash |
Attribute Name: Name | - |
| Thread (ForumName, Subject, ... ) | Hash and Range |
Attribute Name: ForumName |
Attribute Name: Subject |
| Reply ( Id, ReplyDateTime, ... ) | Hash and Range |
Attribute Name: Id |
Attribute Name: ReplyDateTime |
In this example, both the Thread and Reply tables have primary key of the hash and range type. For the Thread table, each forum name can have one or more subjects. In this case, ForumName is the hash attribute and the Subject is the range attribute.
The Reply table has reply Id as the hash attribute and the ReplyDateTime as the range attribute. The reply Id identifies the thread to which the reply belongs. When designing Amazon DynamoDB tables you have to take into account the fact that Amazon DynamoDB does not support cross-table joins. For example, Reply table stores both the forum name and subject values in the Id attribute. If you have a thread reply item, you can then parse the Id attribute to find the forum name and subject and use the information to query the Thread or the Forum tables. This developer guide use these tables to illustrate the Amazon DynamoDB functionality. For information about these tables and sample data stored in these tables, see Example Tables and Data in Amazon DynamoDB.
Amazon DynamoDB supports the following data types:
Scalar data types—Number and String.
Multi-valued types—String Set and Number Set.
For example, in the ProductCatalog table, the Id is a Number type attribute and Authors is a String Set type attribute.
Strings are Unicode with UTF8 binary encoding. There is no limit to the string size when you assign it to an attribute except when the attribute is part of the primary key. For more information, see Limits in Amazon DynamoDB. Also, the length of the attribute is constrained by the item size limit. For the limit, see, Limits in Amazon DynamoDB.
String value comparison is used when returning ordered results in the Scan and Query API. Comparison is based on ASCII character code values. For example, "a" is greater that "A" , and "aa" is greater than "B". For a list of code values, see http://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters.
Numbers are positive or negative exact-value decimals and integers. A number can have up to 38 digits of precision after the decimal point, and can be between 10^-128 to 10^+126. The representation in Amazon DynamoDB is of variable length. Leading and trailing zeroes are trimmed.
Serialized numbers are sent to Amazon DynamoDB as String types, which maximizes compatibility across languages and libraries, however Amazon DynamoDB handles them as the Number type for mathematical operations.
Amazon DynamoDB also supports both Number Sets and String Sets. Multi-valued attributes such as Authors attribute in a book item and Color attribute of a product item are examples of string set type attributes. Note that, because it is a set, the values in the set must be unique. String Sets and Number Sets are not ordered; the order of the values returned in a set is not preserved.