Create a table

must provide following parameters to CreateTable

  • TableName: string = {name of the table}
  • KeySchema: object[] = {attributes that are used for the primary key}
  • AttributeDefinitions: object[] = {Data types for the key schema attributes}
  • ProvisionedThroughput: object = {Number of reads and writes per second that you need for this table}

    {
        TableName : "Music",
        KeySchema: [
            { AttributeName: "Artist", KeyType: "HASH" }, //Partition key
            { AttributeName: "SongTitle", KeyType: "RANGE" } //Sort key
        ],
        AttributeDefinitions: [
            { AttributeName: "Artist", AttributeType: "S" },
            { AttributeName: "SongTitle", AttributeType: "S" }
        ],
        ProvisionedThroughput: { ReadCapacityUnits: 1, WriteCapacityUnits: 1 } // Only specified if using provisioned mode
    }
        

Describe Table

The only parameter is the table name.

  • TableName: string = {name of the table}

    { TableName: "Music" }
        

Insert Data into a Table

PartiQL

use the ExecuteStatement action to add an item to a table, using the Insert PartiQL statement.


    INSERT INTO Music VALUE { 'Artist': 'No One You Know', 'SongTitle': 'Call Me Today' }
        

Classic APIs

use the PutItem action to add an item to a table.


    {
        TableName: "Music",
        Item: { "Artist":"No One You Know", "SongTitle":"Call Me Today" }
    }
        
// Most SQL databases are transaction oriented. When you issue an INSERT statement, the data modifications are not permanent until you issue a COMMIT statement. With Amazon DynamoDB, the effects of a PutItem action are permanent when DynamoDB replies with an HTTP 200 status code (OK).
// In addition to PutItem, DynamoDB supports a BatchWriteItem action for writing multiple items at the same time.

Reading Data from a Table

Amazon DynamoDB provides the following operations for reading data:

  • ExecuteStatement: Retrieves a single or multiple items from a table using the PartiQL. DynamoDB also provides the BatchExecuteStatement operation, allowing you to retrieve multiple items from different tables in a single operation.
  • GetItem: Retrieves a single item from a table. (DynamoDB also provides the BatchGetItem operation, allowing you to perform up to 100 GetItem calls in a single operation.)
  • Query: Retrieves all of the items that have a specific partition key. Within those items, you can apply a condition to the sort key and retrieve only a subset of the data. It provides quick, efficient access to the partitions where the data is stored.
  • Scan: Retrieves all of the items in the specified table.
// This is the most efficient way to read a single item because it provides direct access to the physical location of the item.
// DynamoDB is a non-relational NoSQL database that does not support table joins. Instead, applications read data from one table at a time.

PartiQL

use ExecuteStatement to retrieve the items with statement similar to that of SQL


    SELECT * FROM Music WHERE Artist='No One You Know';
        

Classic APIs

GetItem


    /* optional param 'ProjectionExpression' to return only some attributes */

    {
        TableName: "Music",
        Key: { "Artist": "No One You Know", "SongTitle": "Call Me Today" },
        "ProjectionExpression": "AlbumTitle, Year, Price"
    }
        

Query: You must specify an equality condition for the partition key, and you can optionally provide another condition for the sort key.


    /* Return a single song, by primary key */
    {
        TableName: "Music",
        KeyConditionExpression: "Artist = :a and SongTitle = :t",
        ExpressionAttributeValues: { ":a": "No One You Know", ":t": "Call Me Today" }
    }

    /* Return all of the songs by an artist, matching first part of title */
    {
        TableName: "Music",
        KeyConditionExpression: "Artist = :a and begins_with(SongTitle, :t)",
        ExpressionAttributeValues: { ":a": "No One You Know", ":t": "Call" }
    }
        
// You can use an optional FilterExpression to remove certain items from the results before they are returned to you.
// you must use ExpressionAttributeValues as placeholders in expression parameters (such as KeyConditionExpression and FilterExpression). This is analogous to the use of bind variables in relational databases, where you substitute the actual values into the SELECT statement at runtime.

Scan


    { TableName:  "Music" }
        
// ProjectionExpression can be used to get certain attributes.
// The Scan action also provides a FilterExpression parameter, which you can use to discard items that you do not want to appear in the results. A FilterExpression is applied after the scan is performed, but before the results are returned to you.

Update Table

UpdateItem action modifies a single item. In case of modifying multiple items, use multiple UpdateItem operations.

Following params are used:

  • Key: Key attributes of the item to be modified
  • UpdateExpression: to specify attribute values
  • ConditionExpression(optional): similar to 'where' in SQL
  • ReturnValues(optional): to return a value
// UpdateItem behaves like an "upsert" operation: The item is updated if it exists in the table, but if not, a new item is added (inserted).

    /* update label and price, when certain conditions are met */
    {
        TableName: "Music",
        Key: { "Artist":"No One You Know", "SongTitle":"Call Me Today" },
        UpdateExpression: "SET RecordLabel = :label",
        ConditionExpression: "Price >= :p",
        ExpressionAttributeValues: { ":label": "Global Records", ":p": 2.00 }
    }

    /* increment nof plays */
    {
        TableName: "Music",
        Key: { "Artist":"No One You Know", "SongTitle":"Call Me Today" },
        UpdateExpression: "SET Plays = Plays + :incr",
        ExpressionAttributeValues: { ":incr": 1 },
        ReturnValues: "UPDATED_NEW"
    }
        

Delete Data from a Table

use DeleteItem to delete data from a table, one at a time.

// DynamoDB supports a BatchWriteItem action for deleting multiple items at the same time.

Following params are required:

  • Key: Key attributes of the item to delete.
  • ConditionExpression (optional): item is deleted only if this expression results to true

    {
        TableName: "Music",
        Key: { Artist: "The Acme Band", SongTitle: "Look Out, World" },
        ConditionExpression: "attribute_exists(RecordLabel)"
    }
        

Removing a Table

use DeleteTable to permanently remove a table