Home > Database > Mysql Tutorial > body text

What is the mysql database delete statement?

青灯夜游
Release: 2021-12-08 18:38:53
Original
10808 people have browsed it

Mysql delete database statement is "DROP DATABASE [IF EXISTS] database name", which can delete all tables in the database and delete the database at the same time; among them, the keyword "IF EXISTS" is an optional part, if set Prevents errors from occurring when the database does not exist.

What is the mysql database delete statement?

The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.

In MySQL, deleting a database means that all data and associated objects in the database will be permanently deleted and cannot be undone. Therefore, it is very important to perform this query with additional considerations. To delete a database, use the DROP DATABASE statement as follows:

DROP DATABASE [IF EXISTS] 数据库名;
Copy after login

The syntax is as follows:

  • Database name: Specify the name of the database to be deleted.

  • IF EXISTS: Optional part, used to prevent errors 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.

If you want to practice using the DROP DATABASE statement, you can create a new database and then delete it. Let’s look at the following query:

CREATE DATABASE IF NOT EXISTS tempdb;
SHOW DATABASES;
DROP DATABASE IF EXISTS tempdb;
Copy after login

The description of the three statements is as follows:

  • First, use the CREATE DATABASE statement to create a database named tempdb.

  • Second, use the SHOW DATABASES statement to display all databases.

  • Third, use the DROP DATABASE statement to delete the database named tempdb.

If there is no test_db_del database and you use the command to delete the database from the database list, the system will report an error as follows:

mysql> DROP DATABASE test_db_del;
ERROR 1008 (HY000): Can't drop database 'test_db_del'; database doesn't exist
Copy after login

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)
Copy after login

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.

[Related recommendations: mysql video tutorial]

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template