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.
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] 数据库名;
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;
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
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.
[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!