When we work with the database there are very rare cases when we want to drop the complete database. However, sometimes it’s required to do a complete cleanup and fresh database setup.
当我们使用数据库时,在极少数情况下我们要删除整个数据库。 但是,有时需要进行完整的清理和全新的数据库设置。
The meaning of dropping a database is that all the tables in the database will be deleted along with the data that is stored in the table.
删除数据库的意思是将删除数据库中的所有表以及表中存储的数据。
Now we will learn how to drop the database for the below-mentioned databases.
现在我们将学习如何删除下面提到的数据库的数据库。
- MySQL Drop Database MySQL Drop数据库
- PostgreSQL Drop Database PostgreSQL Drop数据库
- SQL Server Drop Database SQL Server删除数据库
We will go in detail for all the above mentioned DBs.
我们将详细介绍上述所有数据库。
MySQL Drop数据库 (MySQL Drop Database)
We will now try to understand how to drop a database in MySQL.
现在,我们将尝试了解如何在MySQL中删除数据库。
Please note that the below-mentioned script should be run on MySQL shell prompt.
请注意,下面提到的脚本应在MySQL Shell提示符下运行。
Drop Database Syntax: –
删除数据库语法:–
DROP DATABASE [IF EXISTS] database_name;
It is important to add the IF EXISTS clause as MYSQL will give an error if we try to drop a database that does not exist.
添加IF EXISTS子句很重要,因为如果我们尝试删除不存在的数据库,则MYSQL将给出错误。
Example for Drop Database: –
删除数据库示例:–
DROP DATABASE IF EXISTS TestDB;
The above query will drop the database with the name as TestDB.
上面的查询将删除名称为TestDB的数据库。
PostgreSQL Drop数据库 (PostgreSQL Drop Database)
Assume that the database that we want to drop is TestDB.
假设我们要删除的数据库是TestDB 。
The drop database command can only be executed by the user having owner permission on the TestDB.
drop database命令只能由对TestDB具有所有者权限的用户执行。
Also, it is always better to test the connection to the database. Before executing the drop command it is good to make sure that the database is not connected.
同样,最好测试与数据库的连接。 在执行drop命令之前,最好确保未连接数据库。
Connecting to another database will ensure that the connection is disconnected from the target database.
连接到另一个数据库将确保连接与目标数据库断开连接。
Let us now try to drop TestDB.
现在让我们尝试删除TestDB 。
- Right-Click on the database and click on Delete/Drop option. 右键单击数据库,然后单击“删除/删除”选项。
- Drop database confirmation screen will appear. 出现删除数据库确认屏幕。
- Click on OK and the database will get dropped. 单击确定,数据库将被删除。
TestDB is successfully dropped.
TestDB已成功删除。
Please find below the script that can be used as an alternative to the UI.
请在下面找到可以替代UI的脚本。
dropdb -p 5000 -h admin -i -e demo
To destroy the database demo using the server on host admin, port 5000.
要使用主机admin端口5000上的服务器销毁数据库演示 。
It is useful to use the dropdb program from PostgreSQL which is a wrapper around drop database command.
使用PostgreSQLdropdb程序很有用,它是drop database命令的包装器。
SQL Server删除数据库 (SQL Server Drop Database)
We will not try to use DROP command to drop a database in SQL server.
我们不会尝试使用DROP命令在SQL Server中删除数据库。
The following steps need to be followed to drop a database in SQL Server.
要在SQL Server中删除数据库,需要遵循以下步骤。
- Launch SQLServer Management Studio and authenticate to log in. 启动SQLServer Management Studio并进行身份验证以登录。
- In the Object Explorer, select the database that you want to drop. We will be selecting TestDB for our purpose. Right click and click on delete. 在对象资源管理器中,选择要删除的数据库。 我们将根据目的选择TestDB。 右键单击,然后单击删除。
- A pop-up screen will appear. Click on OK and the database will get dropped. 将会出现一个弹出屏幕。 单击确定,数据库将被删除。
TestDB is dropped successfully.
TestDB成功删除。
翻译自: https://www.journaldev.com/24489/sql-drop-database-mysql-postgresql-sql-server