How to Create a AWS RDS MySQL Database and Connect to it using MySQL Workbench
- Mohammad Abu Mattar
- Cloud Computing
- 07 Nov, 2022
- 07 Mins read
Introduction
RDS is a managed service that makes it easy to set up, operate, and scale a relational database in the cloud. It provides cost-efficient and resizable capacity while automating time-consuming administration tasks such as hardware provisioning, database setup, patching and backups. It frees you to focus on your applications so you can give them the fast performance, high availability, security, and compatibility they need.
Prerequisites
- AWS Account
- MySQL Workbench
Create a AWS RDS MySQL Database
- Login to your AWS Console.
- Click on Services and then click on RDS.
- Click on Create Database.
- Select MySQL as the database engine.
- Select the MySQL version.
- Select the template.
- DB instance identifier.
- Master username and password.
- DB instance class.
- Storage.
- Public access
- VPC security group.
- Additional configuration.
- Click on Create database.
Connect to the AWS RDS MySQL Database
- Get the endpoint of the database.
- Open MySQL Workbench.
- Click on the + icon to add a new connection.
- Enter the connection details.
- Connection Name: Enter a name for the connection.
- Hostname: Enter the endpoint of the database.
- Username: Enter the username of the database.
- Password: Enter the password of the database.
-
Click on Test Connection to test the connection.
-
Click on OK to save the connection.
-
Click on the connection to connect to the database.
You have successfully connected to the AWS RDS MySQL database.
Exercise 1: Create a Table
Create a 3 tables in the database, the tables are:
SAILORS
- Contains the details of the sailors.SID
- The ID of the sailor.SName
- The name of the sailor.SRating
- The rating of the sailor.SAge
- The age of the sailor.
BOATS
- Contains the details of the boats.BID
- The ID of the boat.BName
- The name of the boat.BColor
- The color of the boat.
RESERVES
- Contains the details of the reservations.SID
- The ID of the sailor.BID
- The ID of the boat.Day
- The day of the reservation.
Click to expand the SQL solution.
Create the SAILORS table
Explanation:
Click to expand the explanation.
SID
- The ID of the sailor.SName
- The name of the sailor.SRating
- The rating of the sailor.SAge
- The age of the sailor.auto_increment
- The ID will be automatically incremented.primary key
- The ID is the primary key of the table.not null
- The name of the sailor cannot be null.default 0
- The rating of the sailor will be 0 by default.check (SRating >= 0 and SRating <= 10)
- The rating of the sailor must be between 0 and 10.default 18
- The age of the sailor will be 18 by default.check (SAge >= 18 and SAge <= 70)
- The age of the sailor must be between 18 and 70.
Create the BOATS table
Explanation:
Click to expand the explanation.
BID
- The ID of the boat.BName
- The name of the boat.BColor
- The color of the boat.auto_increment
- The ID will be automatically incremented.primary key
- The ID is the primary key of the table.not null
- The name of the boat cannot be null.
Create the RESERVES table
Explanation:
Click to expand the explanation.
SID
- The ID of the sailor.BID
- The ID of the boat.Day
- The day of the reservation.primary key (SID, BID, Day)
- The combination of the ID of the sailor, the ID of the boat and the day is the primary key of the table.not null
- The ID of the sailor, the ID of the boat and the day cannot be null.alter table
- Add a foreign key to the table.RESERVES_SAILORS_FK
- The name of the foreign key.foreign key (SID)
- The ID of the sailor is the foreign key.references
- References theSAILORS
table.RESERVES_BOATS_FK
- The name of the foreign key.foreign key (BID)
- The ID of the boat is the foreign key.references
- References theBOATS
table.
Insert data into the tables
Click to expand the SQL solution.
Exercise 2: Write a Querys
- Find all information of sailors who have reserved boat number 101.
- Find the names of sailors who have reserved a red boat, and list in the order of age.
- Find the names of sailors who have reserved at least one boat.
- Find the ids and names of sailors who have reserved two different boats on the same day.
- Find the ids of sailors who have reserved a red boat or a green boat
Click to expand the SQL solution.
Explanation:
Click to expand the explanation.
select *
- Select all columns.from
- From theSAILORS
table.where
- Where the ID of the sailor is in the list of IDs of sailors who have reserved boat number 101.SID in
- The ID of the sailor is in the list.select SID
- Select the ID of the sailor.from
- From theRESERVES
table.where
- Where the ID of the boat is 101.
Explanation:
Click to expand the explanation.
select SName
- Select the name of the sailor.from
- From theSAILORS
table.where
- Where the ID of the sailor is in the list of IDs of sailors who have reserved a red boat.SID in
- The ID of the sailor is in the list.select SID
- Select the ID of the sailor.from
- From theRESERVES
table.where
- Where the ID of the boat is in the list of IDs of red boats.BID in
- The ID of the boat is in the list.select BID
- Select the ID of the boat.from
- From theBOATS
table.where
- Where the color of the boat is red.order by
- Order by the age of the sailor.
Explanation:
Click to expand the explanation.
select SName
- Select the name of the sailor.from
- From theSAILORS
table.where
- Where the ID of the sailor is in the list of IDs of sailors who have reserved a boat.SID in
- The ID of the sailor is in the list.select SID
- Select the ID of the sailor.from
- From theRESERVES
table.
Explanation:
Click to expand the explanation.
select SID
- Select the ID of the sailor.from
- From theSAILORS
table.where
- Where the ID of the sailor is in the list of IDs of sailors who have reserved two boats on the same day.SID in
- The ID of the sailor is in the list.select SID
- Select the ID of the sailor.from
- From theRESERVES
table.group by
- Group by the ID of the sailor and the day.having
- Having the count of the number of boats reserved is 2.
Explanation:
Click to expand the explanation.
select SID
- Select the ID of the sailor.from
- From theSAILORS
table.where
- Where the ID of the sailor is in the list of IDs of sailors who have reserved a red boat or a green boat.SID in
- The ID of the sailor is in the list.select SID
- Select the ID of the sailor.from
- From theRESERVES
table.where
- Where the ID of the boat is in the list of IDs of red boats or green boats.BID in
- The ID of the boat is in the list.select BID
- Select the ID of the boat.from
- From theBOATS
table.where
- Where the color of the boat is red or green.
Conclusion
In this tutorial, you learned how to create a RDS instance, how to connect to it, and how to create a database and tables. You also learned how to insert data into the tables and how to write queries to retrieve data from the tables. You can use these skills to create a database for your own projects.