Mariadb 简明教程

MariaDB - Drop Database

在 MariaDB 中创建或删除数据库需要拥有权限,通常只授予 root 用户或管理员。 在这些帐户下,您可以使用两种方法来删除数据库:mysqladmin 二进制文件和 PHP 脚本。

Creation or deletion of databases in MariaDB requires privileges, typically, only given to root users or admins. Under these accounts, you have two options for deleting a database: the mysqladmin binary and a PHP script.

请注意,已删除的数据库无法恢复,所以在执行此操作时要小心。此外,用于删除的 PHP 脚本在删除前会 not 要求您确认。

Note that deleted databases are irrecoverable, so exercise care in performing this operation. Furthermore, PHP scripts used for deletion do not prompt you with a confirmation before the deletion.

mysqladmin binary

以下示例演示如何使用 mysqladmin 二进制文件删除现有数据库 −

The following example demonstrates how to use the mysqladmin binary to delete an existing database −

[root@host]# mysqladmin -u root -p drop PRODUCTS
Enter password:******
mysql> DROP PRODUCTS
ERROR 1008 (HY000): Can't drop database 'PRODUCTS'; database doesn't exist

PHP Drop Database Script

PHP 使用 mysql_query 函数删除 MariaDB 数据库。该函数使用两个参数,一个可选参数,并在成功时返回 “true” 值,在不成功时返回 “false” 值。

PHP employs the mysql_query function in deleting MariaDB databases. The function uses two parameters, one optional, and returns either a value of “true” when successful, or “false” when not.

Syntax

查看以下删除数据库脚本语法 −

Review the following drop database script syntax −

bool mysql_query( sql, connection );

参数说明如下 −

The description of the parameters is given below −

Sr.No

Parameter & Description

1

sql This required parameter consists of the SQL query needed to perform the operation.

2

connection When not specified, this optional parameter uses the most recent connection used.

尝试以下示例代码来删除数据库 −

Try the following example code for deleting a database −

<html>
   <head>
      <title>Delete a MariaDB Database</title>
   </head>

   <body>
      <?php
         $dbhost = 'localhost:3036';
         $dbuser = 'root';
         $dbpass = 'rootpassword';
         $conn = mysql_connect($dbhost, $dbuser, $dbpass);

         if(! $conn ) {
            die('Could not connect: ' . mysql_error());
         }
         echo 'Connected successfully<br />';

         $sql = 'DROP DATABASE PRODUCTS';
         $retval = mysql_query( $sql, $conn );

         if(! $retval ){
            die('Could not delete database: ' . mysql_error());
         }

         echo "Database PRODUCTS deleted successfully\n";
         mysql_close($conn);
      ?>
   </body>
</html>

成功删除后,您将看到以下输出 −

On successful deletion, you will see the following output −

mysql> Database PRODUCTS deleted successfully