". Deleting a database clears the existing database from the disk space. After clearing, all data in the database will also be deleted."/> ". Deleting a database clears the existing database from the disk space. After clearing, all data in the database will also be deleted.">

Home  >  Article  >  Database  >  What is the command to delete a database?

What is the command to delete a database?

藏色散人
藏色散人Original
2020-05-27 10:49:309016browse

What is the command to delete a database?

What is the command to delete the database?

When the database is no longer in use, it should be deleted to ensure that valid data is stored in the database storage space. Deleting a database is to clear the existing database from the disk space. After clearing, all data in the database will also be deleted.

Recommended tutorial: mysql tutorial

In MySQL, when you need to delete a created database, you can use the DROP DATABASE statement. The syntax format is:

DROP DATABASE [ IF EXISTS ] <数据库名>

The syntax description is as follows:

51ede4c6d43f3b2169203bd49746727d: Specify the name of the database to be deleted.

IF EXISTS: Used to prevent errors from occurring when the database does not exist.

DROP DATABASE: Delete all tables in the database and delete the database at the same time. Be very careful when using this statement to avoid mistaken deletions. If you want to use DROP DATABASE, you need to obtain database DROP permission.

Note: After MySQL is installed, the system will automatically create two system databases named information_schema and mysql. The system database stores some database-related information. If these two databases are deleted, MySQL will not work properly. .

Example 1

The following creates a test database test_db_del in MySQL.

mysql> CREATE DATABASE test_db_del;
Query OK, 1 row affected (0.08 sec)
mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sakila             |
| sys                |
| test_db            |
| test_db_char       |
| test_db_del        |
| world              |
+--------------------+
9 rows in set (0.00 sec)

Use the command line tool to delete the database test_db_del from the database list. The entered SQL statement and execution results are as follows:

mysql> DROP DATABASE test_db_del;
Query OK, 0 rows affected (0.57 sec)
mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sakila             |
| sys                |
| test_db            |
| test_db_char       |
| world              |
+--------------------+
8 rows in set (0.00 sec)

The database test_db_del does not exist at this time. Execute the same command again and use DROP DATABASE test_db_del directly. The system will report an error, as shown below:

mysql> DROP DATABASE test_db_del;
ERROR 1008 (HY000): Can&#39;t drop database &#39;test_db_del&#39;; database doesn&#39;t exist

If you use the IF EXISTS clause, you can prevent the system from reporting such errors, as shown below:

mysql> DROP DATABASE IF EXISTS test_db_del;
Query OK, 0 rows affected, 1 warning (0.00 sec)

Be very careful when using the DROP DATABASE command. After executing this command, MySQL will not give any confirmation message. DROP DATABASE After deleting the database, all data tables and data stored in the database will also be deleted and cannot be restored. Therefore it is best to back up the database before deleting it.

The above is the detailed content of What is the command to delete a database?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:MYSQL table operationsNext article:MYSQL table operations