May 16, 2021 MariaDB
Creating or deleting a database in MariaDB requires privileges and is usually granted only to root users or administrators. U nder these accounts, you have two options for deleting the database: mysqladmin binary files and PHP scripts.
Note that the deleted database is not recoverable, so do this with caution. A dditionally, the PHP script used to delete does not prompt you for confirmation before deleting.
The following example shows how to use the mysqladmin script 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 uses the same function when deleting the MariaDB mysql_query database. T he function uses two arguments, one optional, and returns the value "true" if successful, otherwise "false".
Check out the following delete database script syntax -
bool mysql_query( sql, connection );
The description of the parameters is given below -
S.No | Parameters and descriptions |
---|---|
1 |
Sql This required parameter consists of the SQL query required to perform the operation. |
2 |
connection
When not specified, this optional parameter uses the most recently used connection. |
Try the following sample code to delete the 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 "; mysql_close($conn); ?> </body> </html>
After successful deletion, you will see the following output -
mysql> Database PRODUCTS deleted successfully