首页Mysql删除数据库相关操作
Created At : 2021-11-13

Mysql删除数据库相关操作

删除数据库语法:

TIP

删除数据库语法:

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

操作演示:

MySQL root@(none):(none)> SHOW databases;
+--------------------+
| Database           |
+--------------------+
| db1                |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set
Time: 0.008s

# 当前已经数据库db1,执行删除操作
MySQL root@(none):(none)> DROP DATABASE db1;
You're about to run a destructive command.
Do you want to proceed? (y/n): y
Your call!
Query OK, 0 rows affected
Time: 0.022s

# 删除一个不存在数据库
MySQL root@(none):(none)> DROP DATABASE db1;
You're about to run a destructive command.
Do you want to proceed? (y/n): y
Your call!
(1008, "Can't drop database 'db1'; database doesn't exist")

# 删除数据库,如果数据库不存在,忽略操作
MySQL root@(none):(none)> DROP DATABASE IF EXISTS  db1;
You're about to run a destructive command.
Do you want to proceed? (y/n): y
Your call!
Query OK, 0 rows affected
Time: 0.002s

WARNING

1️⃣ 删除数据库, 则数据库中所有的数据都将删除。

2️⃣ 因此,使用此语句时要非常小心,以免错误删除。

3️⃣ 如果要使用 DROP DATABASE,需要获得数据库 DROP 权限。

4️⃣ 如果是sql脚本,请使用完整语法:DROP DATABASE IF EXISTS db_name