kartik
kartik

Reputation: 602

Some basic doubts about AWS DynamoDB 'Keys'

I've seen these terms everywhere -


But I seem to get confused with the names :

Also, at the time of creating creating a DynamoDB table, I added 'Order ID' as the Primary Key - now, I need to replace it with 'User ID', and make 'Order ID' as the secondary key.

So, Order ID would be the 'Secondary Key', and User ID would be the 'Primary Key' How can this be achieved?

Any help is appreciated! :) Thanks!

Upvotes: 1

Views: 63

Answers (1)

Bruno Reis
Bruno Reis

Reputation: 37822

Are the Partition Key and Primary Key the same thing? What are they?

No.

A Primary Key is what uniquely identifies each item in the table.

A Partition Key is one possible kind of Primary Key, the other being Partition Key + Sort Key (i.e., a "composite" primary key, as you'd call it in other DB systems).

Does the same go with Sort Key and Secondary Index? What role would secondary key play if I add it?

Sort key and Secondary Index are also different things.

A Sort Key can be used as a "second element" in a "composite key" in a Primary Key. In other words, a Primary Key can be of type "Partition Key" or "Partition Key + Sort Keu".

A Secondary Index is something else entirely. A secondary index allows you to perform different kinds of optimized queries on your table. It needs to know how you want to partition and sort your items: for a Local Secondary Index, you only specify a Sort Key since it will implicitly use the same Partition Key as the Primary Key. For a Global Secondary Index, you must specify a Partition Key and you can, optionally, specify a Sort Key (so in a sense a GSI is defined in a similar way to a Primary Key).

Take a look at this: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.CoreComponents.html

It explains all that with a lot of examples.

at the time of creating creating a DynamoDB table, I added 'Order ID' as the Primary Key - now, I need to replace it with 'User ID', and make 'Order ID' as the secondary key.

You can't do it.

It's not possible to change the Primary Key of a DynamoDB table.

Here's the relevant docs: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateTable.html

I've seen these terms everywhere - [...]

As a final comment, just be aware that you may also see a few other terms, too. In the old days, DynamoDB used different terms for "Partition Key" and "Sort Key". They used to be called, respectively, "Hash Key" and "Range Key". You'll probably see a lot of reference to those names in SDKs, CLI parameters, etc.

Upvotes: 6

Related Questions