DynamoDB: Primary Key and Composite Key
8 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

In DynamoDB, what is the primary purpose of the partition key in both a simple primary key and a composite key?

  • To uniquely identify each item in the entire table.
  • To distribute data across different partitions for scalability. (correct)
  • To define the order in which items are stored within a partition.
  • To provide an alternative identifier for querying items.

A DynamoDB table uses UserID as the partition key. Which of the following scenarios warrants the addition of a sort key to form a composite key?

  • When the table size is expected to exceed DynamoDB's partition limit.
  • When all items in the table must be uniquely identifiable by `UserID` alone.
  • When frequent range-based queries on `UserID` are required.
  • When multiple items can have the same `UserID`, requiring further differentiation. (correct)

When designing a DynamoDB table to store user activity logs, where each user can have multiple log entries, which key structure is most appropriate?

  • Simple primary key using a timestamp as the partition key.
  • Composite key with timestamp as the partition key and `UserID` as the sort key.
  • Simple primary key using `UserID` as the partition key.
  • Composite key with `UserID` as the partition key and timestamp as the sort key. (correct)

Consider a DynamoDB table with a composite key consisting of GameID (partition key) and Score (sort key). How does DynamoDB ensure uniqueness in this table?

<p>The combination of <code>GameID</code> and <code>Score</code> must be unique for each item. (B)</p> Signup and view all the answers

In DynamoDB, what is a key difference in how data is stored and accessed when using a simple primary key versus a composite key?

<p>Simple primary keys offer direct access to an item using only the partition key, while composite keys can enable more complex query patterns within a partition. (B)</p> Signup and view all the answers

A developer needs to store configuration settings for multiple applications in DynamoDB. Each application has a unique ID, and settings are versioned. Which key structure should they use?

<p>Composite key with <code>ApplicationID</code> as the partition key and <code>SettingVersion</code> as the sort key. (C)</p> Signup and view all the answers

You are designing a DynamoDB table for an e-commerce platform to store product reviews. Each product can have multiple reviews. Which key structure is most suitable?

<p>Composite key with <code>ProductID</code> as the partition key and <code>ReviewID</code> as the sort key. (B)</p> Signup and view all the answers

In DynamoDB, you define a table with a composite key where CustomerID is the partition key and OrderDate is the sort key. What type of queries can be efficiently performed?

<p>Retrieve a specific customer by their ID, and all their orders sorted by date. (D)</p> Signup and view all the answers

Flashcards

Primary Key

Identifies items in a DynamoDB table, can be simple or composite.

Simple Primary Key

Primary key using only a partition key (hash key).

Partition Key (Hash Key)

The main key, its value must be unique across the table, and its value determines the item's partition.

Composite Key

A primary key that combines a partition key and a sort key.

Signup and view all the flashcards

Sort Key (Range Key)

An additional key that uniquely identifies items within the same partition.

Signup and view all the flashcards

Simple Key Use Case

Uses only the partition key to uniquely identify each item

Signup and view all the flashcards

Composite Key Use Case

Uses both partition and sort keys to uniquely identify each item.

Signup and view all the flashcards

Simple Key Example

User ID as the partition key.

Signup and view all the flashcards

Study Notes

  • Primary Key and Composite Key are used in AWS DynamoDB to uniquely identify items in a table

Primary Key

  • Can be either simple or composite
  • Simple Primary Key consists of a partition key (hash key)
  • Each item in the table has a unique value for the partition key
  • DynamoDB uses the partition key to distribute data across different partitions
  • Partition Key (hash key) is the main key, and must be unique across the table
  • DynamoDB uses the partition key to compute the partition where the item is stored
  • Example: UserID

Composite Key

  • Is a type of primary key that consists of two attributes - partition key and sort key
  • Partition Key is the hash key
  • Sort Key (range key) helps to uniquely identify items within the same partition
  • Items with the same partition key can have different sort key values
  • Allows for more complex querying and sorting
  • The partition key and sort key combination creates a unique key for each item in the table
  • Able to have multiple items with the same partition key but a different sort key
  • Example: UserID (Partition Key) and OrderID (Sort Key)

Key Differences

  • Simple Primary Key uses only the partition key (hash key) to uniquely identify each item
  • Use case: Simple tables where each item needs unique identification via a single key (e.g., user ID)
  • Composite Key uses both a partition key (hash key) and a sort key (range key) to uniquely identify each item
  • Use case: When storing multiple items with the same partition key but needing an additional level of uniqueness (e.g., storing multiple orders for the same user)

Example Use Case

  • Primary Key (Simple): Storing user details in a table, where UserID is the partition key.
  • Composite Key: Storing orders placed by a user, where UserID is the partition key and OrderID is the sort key, allowing multiple orders for each user.

Summary

  • Primary Key (simple) = Partition Key
  • Composite Key = Partition Key + Sort Key
  • If there is a composite key, then the simple primary key can have duplicates
  • Keys are only defined at the creation of the table

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Description

The primary key in DynamoDB can be simple (partition key) or composite (partition and sort key). The partition key distributes data across partitions, while the sort key helps to uniquely identify items within the same partition, enabling complex queries.

More Like This

DynamoDB Fundamentals
280 questions

DynamoDB Fundamentals

FastGrowingBaltimore5920 avatar
FastGrowingBaltimore5920
DynamoDB On-Demand Capacity Mode
16 questions
DynamoDB Accelerator (DAX)
18 questions

DynamoDB Accelerator (DAX)

RationalStanza9319 avatar
RationalStanza9319
DynamoDB Streams: Capturing Table Changes
22 questions
Use Quizgecko on...
Browser
Browser