Add new attribute column to existing DynamoDB item record

Last modified date

Comment: 1

Introduction

Imagine you have a table in DynamoDb that looks like this:

nameagecustomer-id
JakTech25asdf1234
name is the primary key

Now what if you wanted to add another attribute to a record in the table:

nameagecustomer-idaddress
JakTech25asdf1234London
name is the primary key

You need to use the UpdateExpression and SET function to add the extra address attribute to the record.

Adding New Attribute

To add a new attribute column, use the UpdateExpression along with SET to add the new attribute.

Assume that we have already done the Table setup.

SET attribute is used to add a new attribute to an existing item record.

There are other Update Expressions that can be found in the references section below.

Handling Reserved Characters

In order to add new attribute names that contain reserved characters like hyphens, placeholders have to be used instead.

Placeholders can be alphanumeric.

Use # placeholder for attribute name and : placeholder for values.

References

Update Item – https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.Java.03.html#GettingStarted.Java.03.03

Update Expressions – https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html

Expression Attribute Names – https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ExpressionAttributeNames.html

Expression Attribute Values – https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ExpressionAttributeValues.html

Update Item Spec API Docs – https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/dynamodbv2/document/spec/UpdateItemSpec.html#withNameMap-java.util.Map-

JakTech

1 Response

  1. Pingback: DynamoDB - JakTech

Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.