Sql 简明教程
SQL - DROP Database
SQL DROP DATABASE 语句用于删除现有数据库以及其中的所有数据,如表、视图、索引、存储过程和约束。
The SQL DROP DATABASE statement is used to delete an existing database along with all the data such as tables, views, indexes, stored procedures, and constraints.
SQL DROP Database Statement
在删除现有数据库之前请记住以下重要事项 -
Following are the important points to remember before you delete an existing database −
-
Make sure you have taken proper backup of the database before you delete it.
-
Make sure no other application is connected and using this database.
-
Make sure you have the necessary privilege to delete the database. Usually an admin can delete the databaase.
Syntax
以下是在 SQL 中删除数据库的语法 -
Following is the syntax to delete a database in SQL −
DROP DATABASE DatabaseName;
这里, DatabaseName 是你想要删除的数据库名称。数据库名称在 RDBMS 中总是唯一的。
Here, the DatabaseName is the name of the database that you want to delete. A database name is always unique within the RDBMS.
Example
首先,让我们使用以下 SQL 查询在数据库系统中创建多个数据库 -
First of all, let us create multiple databases into database system using the following SQL queries −
CREATE DATABASE testDB1;
CREATE DATABASE testDB2;
CREATE DATABASE testDB3;
CREATE DATABASE testDB4;
以下语句让我们验证数据库是否已创建:
Let us verify whether the databases are created or not using the following query −
SHOW DATABASES;
该语句会列出所有可用数据库:
This will list down all the available databases:
Database |
information_schema |
mysql |
performance_schema |
testDB1 |
testDB2 |
testDB3 |
testDB4 |
接下来,我们尝试使用 SQL DROP DATABASE 语句删除 testDB1 数据库:
Now, let us try to delete the testDB1 database using the SQL DROP DATABASE statement −
DROP DATABASE testDB1;
一旦我们删除 testDB1 数据库,我们可以使用 SQL SHOW DATABASES 语句验证其是否已经删除:
Once we have deleted the testDB1 database, we can verify whether it is deleted or not using the SQL SHOW DATABASES statement −
SHOW DATABASES;
该语句会列出所有可用数据库:
This will list down all the available databases:
Database |
information_schema |
mysql |
performance_schema |
testDB2 |
testDB3 |
testDB4 |
这样就可以了!我们已经成功在 SQL 中删除了一个数据库。
That’s it! we have successfully deleted a database in SQL.
SQL DROP DATABASE IF EXISTS Statement
SQL DROP DATABASE IF EXISTS 语句包含一个条件,在尝试删除数据库之前检查数据库是否存在。如果数据库不存在于数据库系统中,“DROP DATABASE IF EXISTS” 语句不会引发错误,但只是在不执行任何操作的情况下终止。
The SQL DROP DATABASE IF EXISTS statement includes a condition to check whether the database exists before trying to delete it. If the database does not exist in the database system, the "DROP DATABASE IF EXISTS" statement does not raise an error, but it simply terminates without taking any action.
Syntax
以下是 SQL 中 DROP DATABASE IF EXISTS 语句的语法:
Following is the syntax of the DROP DATABASE IF EXISTS statement in SQL −
DROP DATABASE IF EXISTS DatabaseName;
这里, DatabaseName 是您要删除的数据库的名称。
Here, the DatabaseName is the name of the database that you want to delete.
Example
我们尝试使用以下 SQL 语句删除数据库系统中存在的数据库 testDB2 :
Let us try to delete an existing database testDB2 in the database system using the following SQL statement −
DROP DATABASE IF EXISTS testDB2;
执行上述 SQL 语句时,会获得以下输出:
When we execute the above SQL statement, the output is obtained as follows −
Query OK, 0 rows affected, 3 warnings (0.024 sec)
Dropping the Database that doesn’t Exist
我们尝试使用以下 SQL 语句删除一个 doesn’t exist 的数据库 testDB2 :
Let us try to drop a database testDB2 that doesn’t exist in the database system using the following SQL statement −
DROP DATABASE IF EXISTS testDB2;
执行上述 SQL 语句时,会获得以下输出:
When we execute the above SQL statement, the output is obtained as follows −
Query OK, 0 rows affected, 1 warning (0.000 sec)