Drop a Database using mysqladmin
You would need special privileges to create or to delete a MySQL database. So, assuming you have access to the root user, you can create any database using the mysql mysqladmin binary.
Be careful while deleting any database because you will lose your all the data available in your database.
Here is an example to delete a database(TUTORIALS) created in the previous chapter −
[root@host]# mysqladmin -u root -p drop mydb
Enter password:******
This will give you a warning and it will confirm if you really want to delete this database or not.
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 'mydb' database [y/N] y
Database "mydb" dropped
Drop Database using PHP Script
PHP uses mysql_query function to create or delete a MySQL database. This function takes two parameters and returns TRUE on success or FALSE on failure.
Syntax
bool mysql_query( sql, connection );
Sr.No | Parameter & Description |
---|---|
1 | sql
Required − SQL query to create or delete a MySQL database |
2 | connection
Optional − if not specified, then the last opened connection by mysql_connect will be used. |
sql Required − SQL query to create or delete a MySQL database
connection
Optional − if not specified, then the last opened connection by mysql_connect will be used.
Example
Try the following example to delete a database −
<!DOCTYPE html>
<html lang="en">
<head>
<title>Deleting MySQL Database</title>
</head>
<body>
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Create database
$sql = "DROP DATABASE mydb";
if ($conn->query($sql) === TRUE) {
echo "Database deleted successfully";
} else {
echo "Error deleting database: " . $conn->error;
}
$conn->close();
?>
<script type="text/javascript">window.NREUM||(NREUM={});NREUM.info={"beacon":"bam.nr-data.net","licenseKey":"93d0792ba9","applicationID":"1362537560","transactionName":"YVJbZEtUDEpQVBBcW1gYbEJQGhZUQRgTRRlXU1RZVxoSVkJDSkVcRg==","queueTime":0,"applicationTime":375,"atts":"TRVYEgNOH0Q=","errorBeacon":"bam.nr-data.net","agent":""}</script><script type="text/javascript">window.NREUM||(NREUM={});NREUM.info={"beacon":"bam.nr-data.net","licenseKey":"93d0792ba9","applicationID":"1362537560","transactionName":"YVJbZEtUDEpQVBBcW1gYbEJQGhZUQRgTRRlXU1RZVxoSVkJDSkVcRg==","queueTime":0,"applicationTime":340,"atts":"TRVYEgNOH0Q=","errorBeacon":"bam.nr-data.net","agent":""}</script></body>
</html>
WARNING − While deleting a database using the PHP script, it does not prompt you for any confirmation. So be careful while deleting a MySQL database.