UpdateItem lets you change data in a DynamoDB table without replacing the whole item. It saves time and keeps your data accurate.
UpdateItem basics in DynamoDB
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
DynamoDB
UpdateItem {
TableName: "TableName",
Key: { "PrimaryKey": {S: "KeyValue"} },
UpdateExpression: "SET #attr = :val",
ExpressionAttributeNames: {"#attr": "AttributeName"},
ExpressionAttributeValues: {":val": {S: "NewValue"}}
}UpdateExpression tells DynamoDB what to change.
Use ExpressionAttributeNames and ExpressionAttributeValues to safely use attribute names and values.
Examples
DynamoDB
UpdateItem {
TableName: "Users",
Key: { "UserID": {S: "123"} },
UpdateExpression: "SET #name = :newName",
ExpressionAttributeNames: {"#name": "Name"},
ExpressionAttributeValues: {":newName": {S: "Alice"}}
}DynamoDB
UpdateItem {
TableName: "Products",
Key: { "ProductID": {S: "A1"} },
UpdateExpression: "ADD Stock :inc",
ExpressionAttributeValues: {":inc": {N: "5"}}
}DynamoDB
UpdateItem {
TableName: "Orders",
Key: { "OrderID": {S: "789"} },
UpdateExpression: "REMOVE DeliveryDate"
}Sample Program
This updates the Position of employee E001 to "Manager".
DynamoDB
UpdateItem {
TableName: "Employees",
Key: { "EmployeeID": {S: "E001"} },
UpdateExpression: "SET #pos = :newPos",
ExpressionAttributeNames: {"#pos": "Position"},
ExpressionAttributeValues: {":newPos": {S: "Manager"}}
}Important Notes
UpdateItem only changes specified attributes; others stay the same.
If the item does not exist, UpdateItem can create it if you use the right parameters.
Always use ExpressionAttributeNames and ExpressionAttributeValues to avoid errors with reserved words.
Summary
UpdateItem changes parts of an item without replacing it all.
Use UpdateExpression to specify what to change.
It is useful for adding, changing, or removing attributes safely.
Practice
1. What does the
UpdateItem operation in DynamoDB do?easy
Solution
Step 1: Understand UpdateItem purpose
The UpdateItem operation is designed to modify parts of an existing item, not the entire item.Step 2: Compare with other operations
Deleting an item or creating a table are different operations, so they don't match UpdateItem's function.Final Answer:
It changes specific attributes of an item without replacing the whole item. -> Option DQuick Check:
UpdateItem modifies parts of an item [OK]
Hint: UpdateItem changes parts, not whole items [OK]
Common Mistakes:
- Thinking UpdateItem deletes items
- Confusing UpdateItem with CreateTable
- Assuming UpdateItem reads data only
2. Which of the following is the correct way to specify an update expression in DynamoDB's UpdateItem?
easy
Solution
Step 1: Identify valid UpdateExpression syntax
The 'SET' keyword is used to assign a new value to an attribute, correctly written as 'SET #name = :value'.Step 2: Check other options for syntax errors
'ADD' requires a space before the value and no '=' sign; 'REMOVE' does not take a value; 'DELETE' syntax is incorrect here.Final Answer:
UpdateExpression: 'SET #name = :value' -> Option CQuick Check:
Use SET with '=' for updates [OK]
Hint: Use 'SET #attr = :val' for updates [OK]
Common Mistakes:
- Using '=' with ADD or REMOVE
- Missing '#' for attribute names
- Incorrect DELETE syntax
3. Given the following UpdateItem call:
What will happen to the Age attribute of the item with UserId '123'?
{
TableName: 'Users',
Key: { 'UserId': { S: '123' } },
UpdateExpression: 'SET Age = Age + :inc',
ExpressionAttributeValues: { ':inc': { N: '1' } }
}What will happen to the Age attribute of the item with UserId '123'?
medium
Solution
Step 1: Analyze the UpdateExpression
The expression 'SET Age = Age + :inc' means increase the current Age by the value of ':inc', which is 1.Step 2: Understand the effect on the item
This will add 1 to the existing Age attribute, not replace or delete it.Final Answer:
Age will be incremented by 1. -> Option BQuick Check:
SET with addition increments attribute [OK]
Hint: SET with '+ :value' adds to number attributes [OK]
Common Mistakes:
- Thinking Age resets to 1
- Assuming Age is deleted
- Expecting syntax error from addition
4. You wrote this UpdateItem call:
But the update fails. What is the likely error?
{
TableName: 'Products',
Key: { 'ProductId': { S: 'p001' } },
UpdateExpression: 'SET Count = :newCount',
ExpressionAttributeValues: { ':newCount': { N: '20' } },
ExpressionAttributeNames: { '#Count': 'Count' }
}But the update fails. What is the likely error?
medium
Solution
Step 1: Check usage of ExpressionAttributeNames
You defined '#Count' in ExpressionAttributeNames but did not use '#Count' in UpdateExpression; you used 'Count' directly instead.Step 2: Understand attribute name substitution
When you define ExpressionAttributeNames, you must use the placeholder '#Count' in UpdateExpression to avoid reserved word conflicts.Final Answer:
You used ExpressionAttributeNames but did not use '#Count' in UpdateExpression. -> Option AQuick Check:
Use '#name' in UpdateExpression if defined [OK]
Hint: Use '#attr' in UpdateExpression if defined [OK]
Common Mistakes:
- Ignoring ExpressionAttributeNames usage
- Assuming numeric values must be strings
- Thinking SET can't update numbers
5. You want to remove the attribute
Discount from an item with OrderId '789' using UpdateItem. Which is the correct UpdateExpression and parameters?hard
Solution
Step 1: Identify correct syntax to remove attribute
The 'REMOVE' keyword followed by the attribute name removes that attribute from the item.Step 2: Check other options for errors
UpdateExpression: 'SET Discount = :null', ExpressionAttributeValues: { ':null': { NULL: true } } tries to set attribute to null which does not remove it; UpdateExpression: 'DELETE Discount', Key: { 'OrderId': { S: '789' } } uses DELETE incorrectly; UpdateExpression: 'REMOVE :Discount', ExpressionAttributeValues: { ':Discount': { S: 'Discount' } } uses placeholder incorrectly with REMOVE.Final Answer:
UpdateExpression: 'REMOVE Discount', Key: { 'OrderId': { S: '789' } } -> Option AQuick Check:
Use REMOVE with attribute name to delete attribute [OK]
Hint: Use REMOVE attributeName to delete attribute [OK]
Common Mistakes:
- Using SET with NULL to remove attribute
- Using DELETE keyword incorrectly
- Using placeholders with REMOVE without #
