0
0
DynamoDBquery~20 mins

PutItem (creating items) in DynamoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PutItem Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What happens when you put an item with an existing primary key?

You have a DynamoDB table with a primary key id. You run a PutItem operation with an item that has the same id as an existing item but different attribute values. What will be the result?

DynamoDB
PutItem({TableName: 'Users', Item: {id: {S: '123'}, name: {S: 'Alice'}}})
PutItem({TableName: 'Users', Item: {id: {S: '123'}, name: {S: 'Bob'}}})
AThe item with id '123' is replaced with the new item having name 'Bob'.
BThe operation fails with a ConditionalCheckFailedException.
CThe new item is added alongside the old item, so two items with id '123' exist.
DThe operation is ignored and the old item remains unchanged.
Attempts:
2 left
💡 Hint

Think about how PutItem works with existing keys in DynamoDB.

📝 Syntax
intermediate
1:30remaining
Which PutItem request is syntactically correct?

Choose the correct DynamoDB PutItem request syntax to add an item with id as a string and age as a number.

A{TableName: 'People', Item: {id: '001', age: 30}}
B{TableName: 'People', Item: {id: S:'001', age: N:'30'}}
C{TableName: 'People', Item: {id: {S: '001'}, age: {N: '30'}}}
D{TableName: 'People', Item: {id: {String: '001'}, age: {Number: 30}}}
Attempts:
2 left
💡 Hint

Remember DynamoDB expects attribute values to be typed with S for string and N for number, both as strings.

optimization
advanced
2:30remaining
How to avoid overwriting existing items when using PutItem?

You want to add a new item only if an item with the same primary key does not exist. Which option correctly achieves this using PutItem?

AUse <code>ConditionExpression: 'attribute_not_exists(id)'</code> in the PutItem request.
BUse <code>ReturnValues: 'ALL_OLD'</code> in the PutItem request.
CUse <code>UpdateItem</code> instead of <code>PutItem</code>.
DUse <code>PutItem</code> without any condition; it will not overwrite existing items.
Attempts:
2 left
💡 Hint

Think about how to tell DynamoDB to only put if the item does not exist.

🔧 Debug
advanced
2:00remaining
Why does this PutItem request fail with ValidationException?

Consider this PutItem request:

{
  TableName: 'Orders',
  Item: {
    orderId: {S: 'A123'},
    amount: {N: 100}
  }
}

Why does it fail with a ValidationException?

AThe table name 'Orders' does not exist.
BThe primary key attribute name is incorrect; it should be <code>id</code> not <code>orderId</code>.
CThe attribute <code>amount</code> cannot be a number.
DThe number value must be a string, so <code>{N: '100'}</code> is required instead of <code>{N: 100}</code>.
Attempts:
2 left
💡 Hint

Check the data types required by DynamoDB for number attributes.

🧠 Conceptual
expert
3:00remaining
What is the effect of using ReturnValues='ALL_OLD' in PutItem?

You perform a PutItem operation with ReturnValues set to 'ALL_OLD'. What will the response contain if the item already existed?

AThe response contains the new item that was just inserted.
BThe response contains the entire old item that was replaced by the new item.
CThe response contains only the primary key of the old item.
DThe response is empty regardless of whether the item existed.
Attempts:
2 left
💡 Hint

Think about what ReturnValues='ALL_OLD' means in DynamoDB PutItem.