Introduction
In this project, you will learn how to access MySQL, import data, and query cities with a population over one million from the city table.
👀 Preview
MariaDB [world]> SOURCE /home/labex/project/getMillion.sql;
+------+--------------+-------------+----------+------------+
| ID | Name | CountryCode | District | Population |
+------+--------------+-------------+----------+------------+
| 1907 | Shijiazhuang | CHN | Hebei | 2041500 |
| 1924 | Tangshan | CHN | Hebei | 1040000 |
+------+--------------+-------------+----------+------------+
2 rows in set (0.001 sec)
🎯 Tasks
In this project, you will learn:
- How to start the MySQL service and access MySQL using the
sudocommand - How to import data from a SQL file into the MySQL database
- How to query the
citytable to retrieve information for cities in the Hebei region with a population over one million
🏆 Achievements
After completing this project, you will be able to:
- Manage a MySQL database, including starting the service and importing data
- Write SQL queries to retrieve specific information from a database table
- Demonstrate your understanding of working with MySQL and SQL queries
Access MySQL and Import Data
In this step, you will learn how to access MySQL using the sudo command without any password, and import the data from /home/labex/project/world.sql into MySQL.
- Open a terminal on the server.
- Start the MySQL service:
sudo service mysql start
- Access MySQL using the
sudocommand:
sudo mysql
- Import the data from
/home/labex/project/world.sqlinto MySQL and switch to theworlddatabase:
MariaDB [(none)]> SOURCE /home/labex/project/world.sql;
Query Cities with Over One Million Population
In this step, you will learn how to query all information regarding Hebei region cities with a population over one million from the city table.
- Open the
getMillion.sqlfile. - Add the following code to the
getMillion.sqlfile:
-- Query to retrieve city information for cities in Hebei district with a population over 1 million
SELECT *
FROM city
WHERE District = 'Hebei' AND Population > 1000000;
- Save the file.
- Run the
getMillion.sqlscript in MySQL:
MariaDB [world]> SOURCE /home/labex/project/getMillion.sql;
This will display the information for the cities in the Hebei region with a population over one million.
MariaDB [world]> SOURCE /home/labex/project/getMillion.sql;
+------+--------------+-------------+----------+------------+
| ID | Name | CountryCode | District | Population |
+------+--------------+-------------+----------+------------+
| 1907 | Shijiazhuang | CHN | Hebei | 2041500 |
| 1924 | Tangshan | CHN | Hebei | 1040000 |
+------+--------------+-------------+----------+------------+
2 rows in set (0.001 sec)
Summary
Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.



