This homework assumes you have installed the software MySQL to your computer. If not please do that first!
- Practice the concepts
- MySQL exercises
- Code along
Let's start this week off with some interactive exercises. Check out the following to get started writing your first queries! In the following online course, do sections 1 (Manipulation) and sections 2 (Queries).
Also make time to do lesson 1-5 from the following:
Exercise 1: Create and insert queries
Write a JavaScript file (to be executed with Node.js) that creates and makes a connection to a MySQL database. Do so using the mysql
package (https://www.npmjs.com/package/mysql).
Before working on the exercise, first get a working version of the example going. This will help you understand the basic structure of making a database connection and query. When doing this, make sure you replace the
user
,password
anddatabase
values with the ones you've created for this module:hyfuser
,hyfpassword
anduser_db
.
When you feel familiar with the library, you can go on with the exercise. Inside of your JavaScript file, make queries that:
- Create a database called
company
- Make a connection to your database, using your MySQL
hyfuser
login credentials - Create a table called
Employees
with the following fields (emp_no
,emp_name
,salary
andreports_to
). - Create a table called
Departments
with the following fields (dept_no
,dept_name
andmanager
) - Create a table called
Projects
with the following fields (proj_no, proj_name, starting_date, ending_date
) - Insert 10 rows into each table with relevant fields. Find a way to create the data for those fields
- Test your code by executing
node <FILE_NAME>
in the terminal. Then check your MySQL database and see if everything has been created as expected
Exercise 2 : Select queries on the "world" database
For this part of the homework, use the
world.sql
file in theweek1/databases
folder. Before you go on, execute the file to create a database instance of theworld
database. Test to see if it's created. Make sure all the tables (city
,country
andcountrylanguage
) and the containing data are there.
Write a JavaScript file (to be executed with Node.js) that queries (using select statements) the world
database. The results given back should answer following questions:
- What are the names of countries with population greater than 8 million?
- What are the names of countries that have “land” in their names?
- What are the names of the cities with population in between 500,000 and 1 million?
- What's the name of all the countries on the continent ‘Europe’?
- List all the countries in the descending order of their surface areas.
- What are the names of all the cities in the Netherlands?
- What is the population of Rotterdam?
- What's the top 10 countries by Surface Area?
- What's the top 10 most populated cities?
- What is the population number of the world?
After you've written your queries, test to see if they work by executing node <FILE_NAME>
.
In this week's code along you won't be building a complete app, but get familiar with using MySQL together with Node.js.
After you've finished your todo list it's time to show us what you got! The homework that needs to be submitted is the following:
- MySQL exercises
Upload your code to your forked Databases repository in GitHub. Make a pull request to HackYourFuture's forked repository.
Forgotten how to upload your homework? Go through the guide to learn how to do this again.
Deadline Saturday 23.59 CET