0
0
DynamoDBquery~5 mins

Expression attribute names in DynamoDB

Choose your learning style9 modes available
Introduction
Expression attribute names help you use reserved words or special characters in your DynamoDB queries safely.
When your attribute name is a reserved word in DynamoDB, like 'AND' or 'FOR'.
When your attribute name contains special characters or spaces.
When you want to avoid conflicts with DynamoDB's reserved keywords.
When you want to write cleaner and safer update or filter expressions.
Syntax
DynamoDB
ExpressionAttributeNames = {"#key": "ActualAttributeName"}

Use #key in your expressions instead of ActualAttributeName.
Expression attribute names start with a # symbol and map to the real attribute names.
Use them inside your UpdateExpression, FilterExpression, or ProjectionExpression.
Examples
Use #N to represent the attribute 'AND' which is a reserved word.
DynamoDB
ExpressionAttributeNames = {"#N": "AND"}
UpdateExpression = "SET #N = :val"
Use #A to access an attribute with a space in its name.
DynamoDB
ExpressionAttributeNames = {"#A": "Address City"}
ProjectionExpression = "#A"
Use #D to filter items by the 'FOR' attribute safely.
DynamoDB
ExpressionAttributeNames = {"#D": "FOR"}
FilterExpression = "#D = :dateVal"
Sample Program
This updates the 'AND' attribute of the user with UserId '123' using an expression attribute name #N.
DynamoDB
import boto3

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('Users')

response = table.update_item(
    Key={'UserId': '123'},
    UpdateExpression='SET #N = :name',
    ExpressionAttributeNames={'#N': 'AND'},
    ExpressionAttributeValues={':name': 'Alice'}
)

print(response['ResponseMetadata']['HTTPStatusCode'])
OutputSuccess
Important Notes
Always use expression attribute names when your attribute names are reserved words or contain special characters.
Expression attribute names improve query safety and prevent syntax errors.
You can use multiple expression attribute names in one query by adding more mappings.
Summary
Expression attribute names let you safely use reserved words or special characters in DynamoDB queries.
They start with # and map to real attribute names.
Use them in UpdateExpression, FilterExpression, or ProjectionExpression to avoid errors.