Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

MySQL deletes the database


May 15, 2021 MySQL


Table of contents


MySQL deletes the database


Use mysqladmin to delete the database

Using a regular user to log on to a MySQL server, you may need specific permissions to create or delete the MySQL database.

So we use root user login here, root user has the highest permissions, you can use MySQL mysqladmin command to delete the database.

It is important to be careful when deleting a database, as all data disappears after the delete command is executed.

The following example removes the database TUTORIALS, which was created in the previous section:

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

After executing the above delete database command, a prompt box appears to confirm that the database is actually 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 'W3CSCHOOL' database [y/N] y
Database "W3CSCHOOL" dropped

Use the PHP script to delete the database

PHP uses mysql_query function to create or delete mySQL databases.

The function has two arguments that return TRUE if executed successfully, otherwise FALSE is returned.

Grammar

bool mysql_query( sql, connection );
Parameters Describe
Sql Necessary. S pecify the SQL query to send. Note: The query string should not end with a sign.
connection Optional. S pecifies the SQL connection identifier. If not specified, the last open connection is used.

Instance

The following example demonstrates using PHP mysql_query functions to delete the database:

<html>
<head>
<meta charset="utf-8">
<title>删除 MySQL 数据库</title>
</head>
<body>
<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
   die('连接失败: ' . mysql_error());
 }
echo '连接成功<br />';
$sql = 'DROP DATABASE W3CSCHOOL';
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('删除数据库失败: ' . mysql_error());
}
echo "数据库 W3CSCHOOL 删除成功\n";
mysql_close($conn);
?>
</body>
</html>

Note: When you delete a database using a PHP script, there is no confirmation that the database is deleted, and the specified database is deleted directly, so you should be especially careful when deleting the database.