Introduction
In this project, you will learn how to work with MySQL databases, including starting the MySQL service, accessing the MySQL command-line interface, importing a database, and using the TRUNCATE statement to delete data from a table.
👀 Preview
MariaDB [world]> SELECT * FROM city;
Empty set (0.001 sec)
🎯 Tasks
In this project, you will learn:
- How to start the MySQL database service
- How to access the MySQL command-line interface using the
sudocommand - How to import a database into MySQL
- How to use the
TRUNCATEstatement to delete all data from a table
🏆 Achievements
After completing this project, you will be able to:
- Manage a MySQL database, including starting the service and accessing the command-line interface
- Import data into a MySQL database
- Use SQL statements like
TRUNCATEto manipulate data in a database
Start MySQL Database and Access MySQL
In this step, you will learn how to start the MySQL database and access it using the sudo command without a password.
- Start the MySQL database service:
sudo service mysql start
- Access the MySQL command-line interface using the
sudocommand without a password:
sudo mysql
You should now be able to access the MySQL prompt.
Import World Database
In this step, you will learn how to import the world.sql database file into the MySQL database.
- Import the
world.sqlfile into the MySQL database:
MariaDB [(none)]> SOURCE /home/labex/project/world.sql;
This will import the world database into your MySQL instance.
- Switch to the
worlddatabase:
MariaDB [(none)]> USE world;
You should now be in the world database.
Truncate City Table
In this step, you will learn how to use the TRUNCATE statement to delete all data from the city table.
- Open the
truncateCity.sqlfile. - Add the following SQL statement to the file:
TRUNCATE TABLE city;
This will delete all data from the city table.
- Run the
truncateCity.sqlscript in the MySQL prompt:
MariaDB [world]> SOURCE /home/labex/project/truncateCity.sql;
- Verify that the
citytable is empty:
MariaDB [world]> SELECT * FROM city;
You should see an empty result set, indicating that the city table has been truncated.
MariaDB [world]> SOURCE /home/labex/project/truncateCity.sql;
Query OK, 0 rows affected (0.004 sec)
MariaDB [world]> SELECT * FROM city;
Empty set (0.001 sec)
Summary
Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.



