如何使用当前数据库的名称在 MySQL 中删除数据库?
要获取当前数据库,你可以使用 SELECT DATABASE() −
select database();
以下为语法 −
set @anyVariableName = database(); select @anyVariableName; set @anyVariableName2 = concat('drop database ', @yourVariableName); prepare anyVariableName3 from @yourVariableName2; execute yourVariableName3;
让我们执行如上查询以获取当前数据库并将其删除 −
mysql> set @currentDatabase = database(); Query OK, 0 rows affected (0.00 sec) mysql> select @currentDatabase; +------------------+ | @currentDatabase | +------------------+ | employeeonboard | +------------------+ 1 row in set (0.00 sec) mysql> set @sqlQuery = concat('drop database ', @currentDatabase); Query OK, 0 rows affected (0.00 sec) mysql> prepare stmt from @sqlQuery; Query OK, 0 rows affected (0.04 sec) Statement prepared mysql> execute stmt; Query OK, 0 rows affected (0.56 sec)
要检查数据库是否存在,请使用 SELECT DATABASE() −
mysql> select database();
这将生成以下输出 −
+------------+ | database() | +------------+ | NULL | +------------+ 1 row in set (0.00 sec)
广告