In this exercise, let's say you are a music producer wanting to store information about iconic 2000's artists and songs. Because there's going to be a lot of artists in this, you want to make sure there is some way you can search within the database you make.
You will create a table in DynamoDB to store information about a music library. You will query the music library and then delete the DynamoDB table.
By the end of this exercise, you should be able to do the following:
In this task, you will create a new table named Music in DynamoDB. Each table requires a partition key (or a primary key) that is used to partition data across DynamoDB servers. A table can also have a sort key. The combination of a partition key and a sort key uniquely identifies each item in a DynamoDB table.
In this task, you will add data to the Music table. You want this table to be a go-to space for people to check artists and details about their songs!
A table is a collection of data on a particular topic.
Each table contains multiple items. An item is similar to a row in a spreadsheet. Each item is uniquely identifiable by a primary key, which is composed of one or two attributes (more on attributes below). Specifically, the primary key could be a single-attribute partition key or a combination of a partition key and a sort key.
Each item consists of one or more attributes. An attribute is a fundamental data element, something that does not need to be broken down any further. For example, an item in a Music table contains attributes such as song and artist. Attributes in DynamoDB are similar to columns in other database systems, but each item (row) can have different attributes (columns).
These are the only required attributes, which means these are the only things every single item in the Music table must have. But, you know a thing or two extra about Bruno Mars' song. How can we record this? You will now add additional attributes.
5. To create an additional attribute, choose Add new attribute. This is a dropdown in the top right corner of the Attributes section.
6. In the dropdown list, select String. A new attribute row will be added:
Let's say we want to add a new attribute - this time we want to say the year the song was produced.
Which of the following attributes should we use... String, Number, Boolean or List? Take your pick before we move on and find out!
7. To add another new attribute, choose Add new attribute.
8. In the dropdown list, choose Number:
9. Choose Create item.
The item has now been added to the Music table.
Let's create another item, using the following attributes:
This item has an additional attribute called Genre. This is an example of each item in a non-relational database being capable of having different attributes. There is no need for you to pre-set the attributes to fill in a non-relational database!
To create a third item, use the following attributes:
Once again, this item has a new LengthSeconds attribute identifying the length of the song. We love the flexibility of a NoSQL database. 👑
Oops, turns out the album by Taylor Swift was launched in the year 2023, not 2022. Let's correct this data!
3. Click on Taylor Swift
4. Change the Year from 2022 to 2023.
5. Choose Save and close. Nice, the Taylor Swift item is now updated.
Let's say you have thousands and thousands of artists and songs in your database. How do we quickly find our Taylor Swift item? This is where querying comes in.
There are two ways to query a DynamoDB table: query and scan. A query is the most efficient way to retrieve data from a DynamoDB table. Alternatively, you can scan for an item. This option involves looking through every item in a table, so it is less efficient and can take significant time for larger tables.
A query operation finds items based on the primary key and optionally the sort key. It is fully indexed, so it runs very fast.
Fields for the Artist (Partition key) and Song (Sort key) are now displayed.
2. Enter the following details:
3. Choose Run.
The song quickly appears in the list. You might need to scroll down to see this result.
4. Scroll up on the page, and choose Scan.
5. Expand Filters, and enter the following values:
6. For the Attribute name, enter Year
7. For Type, choose Number.
8. For Value, enter 2007
9. Choose Run.
Magic! Only the song released in 2007 is displayed:
In this task, you will delete your Music table, which will also delete all the data in the table.
1. In the DynamoDB dashboard, under Tables, choose Update settings.
2. Choose the Music table if it is not already selected.
3. Choose Actions, and then choose Delete table.
4. On the confirmation panel, enter confirm and choose Delete.
The table was successfully deleted.
Congratulations! You now have successfully: