MySQL Delete Database
Log in to the MySQL server as a regular user, and you may need specific permissions to create or delete MySQL databases. Therefore, we will use the root user for login, as the root user has the highest privileges.
When deleting a database, it is crucial to be extremely cautious because all data will be lost after executing the delete command.
Drop Command to Delete Database
Drop command format:
drop database <database name>;
For example, to delete a database named "tutorialpro":
mysql> drop database tutorialpro;
Delete Database Using mysqladmin
You can also use the mysqladmin
command in the terminal to execute the delete command.
[root@host]# mysqladmin -u root -p drop tutorialpro
Enter password:******
After executing the above delete database command, a prompt will appear to confirm whether you really want to delete the database:
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 'tutorialpro' database [y/N] y
Database "tutorialpro" dropped
Delete Database Using PHP Script
PHP uses the mysqli_query
function to create or delete MySQL databases.
This function takes two parameters and returns TRUE on success or FALSE on failure.
Syntax
mysqli_query(connection, query, resultmode);
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use. |
query | Required. Specifies the query string. |
resultmode | Optional. A constant. Can be one of the following values: MYSQLI_USE_RESULT (use this if you need to retrieve large amounts of data) <br> MYSQLI_STORE_RESULT (default) |
Example
The following example demonstrates how to delete a database using the PHP mysqli_query
function:
Delete Database
<?php
$dbhost = 'localhost'; // mysql server host address
$dbuser = 'root'; // mysql username
$dbpass = '123456'; // mysql user password
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Connection failed: ' . mysqli_error($conn));
}
echo 'Connection successful<br />';
$sql = 'DROP DATABASE tutorialpro';
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
die('Failed to delete database: ' . mysqli_error($conn));
}
echo "Database tutorialpro deleted successfully\n";
mysqli_close($conn);
?>
After successful execution, the result will be:
Note: When deleting a database using a PHP script, there will be no confirmation prompt, and the specified database will be deleted directly. Therefore, you should be especially careful when deleting databases.