English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

MySQL Delete Database

When logging into the MySQL server as a regular user, you may need specific permissions to create or delete a MySQL database, so we use the root user to log in here, as the root user has the highest permissions.

Be very careful during the database deletion process, as all data will be deleted after executing the delete command.

The drop command deletes a database

Format of drop command:

drop database <database name>;

For example, delete a database named w3codebox's database:

mysql> drop database w3codebox;

Delete database using mysqladmin

You can also use the mysql mysqladmin command in the terminal to execute the delete command.

The following example deletes the database w3codebox(该数据库在前一章节已创建):

[root@host]# mysqladmin -u root -p drop w3codebox
Enter password:******

After executing the above delete database command, a prompt box will appear to confirm whether the database is really to be deleted:

Dropping the database is potentially a very bad thing to do.
Any data stored in the database will be destroyed.
Do you really want to drop the 'w3codebox' database [y/N] y
Database "w3codebox" dropped

Delete database using PHP script

PHP uses the mysqli_query function to create or delete a MySQL database.

This function has two parameters and returns TRUE on success, otherwise FALSE.

Syntax

mysqli_query(connection, query, resultmode);
ParameterDescription
connectionRequired. Specifies the MySQL connection to use.
queryRequired. Specifies the query string.
resultmode

Optional. A constant. It can be any of the following values:

  • MYSQLI_USE_RESULT (use this if you need to retrieve a large amount of data)

  • MYSQLI_STORE_RESULT (default)

Online example

The following example demonstrates how to delete a database using the PHP mysqli_query function:

<?php
$dbhost = 'localhost';  // MySQL server host address
$dbuser = 'root';            // MySQL username
$dbpass = '123456';          // MySQL username and password
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
    die('Connection failed: ' . mysqli_error($conn));
}
echo 'Connection successful<br />';
$sql = 'DROP DATABASE w3codebox';
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
    die('Database deletion failed: ' . mysqli_error($conn));
}
echo "Database w3codebox deletion successful\n";
mysqli_close($conn);
?>

After execution, the result is:

Note:When deleting a database using PHP scripts, there will be no confirmation message whether to delete, and the specified database will be deleted directly, so you should be very careful when deleting databases.