MySQL Delete Database

Use ordinary users to log in to the MySQL server. You may need specific permissions to create or delete MySQL databases, so we use the root user to log in here, and the root user has the highest permissions.

In the process of deleting the database, you must be very careful, because after executing the delete command, all data will disappear.

drop command to delete database

drop command format:

 drop database ;

For example, delete the database named RUNOOB:

mysql> drop database RUNOOB;

Use mysqladmin to delete the database

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

The following instance deletes the database RUNOOB (the database was created in the previous chapter):

[[emailprotected]]# mysqladmin -u root -p drop RUNOOB Enter password:****** 

After executing the above delete database command, a prompt box will appear to confirm whether 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 'RUNOOB' dat abase [y/N] y Database "RUNOOB" dropped  span> 

Use PHP script to delete database

PHP uses mysqli_query function to create or delete MySQL database.

This function has two parameters, it returns TRUE when the execution is successful, otherwise it returns FALSE.

Syntax

mysqli_query(connection,query,resultmode); span>
Parameter Description
connection Required. Specifies the MySQL connection to be used.
query Required, specifies the query string.
resultmode

Optional. A constant. Can be any of the following values:

  • MYSQLI_USE_RESULT (if you need to retrieve large amounts of data, please use this)
  • MYSQLI_STORE_RESULT (default)

Example

The following example demonstrates the use of PHP mysqli_query function to delete a database:

Delete database

$dbhost = localhost:3306; // mysql server host Address $dbuser = root; // mysql username $dbpass = 123456; // mysql username and password $conn = mysqli_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die( failed to connect: . mysqli_error( $conn)< span class="hl-brackets">); } echo successfully connected
; $sql = DROP DATABASE RUNOOB; $retval = mysqli_query( $conn, $sql ); if(! $retval ) { die( failed to delete the database: . mysqli_error($conn)); } echo The database RUNOOB was deleted successfully
; mysqli_close($conn); ?>< /span>
< /span>
span>
span>
span>

After successful execution, count The result is:

share picture

Note : When you delete a database using a PHP script, there will be no confirmation message to delete, and the specified database will be deleted directly, so you must be careful when deleting the database.

Delete database

$dbhost = localhost:3306; // mysql server host address $dbuser = root; // mysql username $dbpass = 123456‘< span class="hl-code">; // mysql username and password $conn = mysqli_connect($dbhost , $dbuser, $dbpass); if(! $conn ) { die( failed to connect : . mysqli_error($conn)); } echo Successful connection
; $sql = DROP DATABASE RUNOOB; $retval = mysqli_query( $conn, $sql ); if(! $retval ) { die( failed to delete the database : . mysqli_error($conn)); } echo The database RUNOOB is deleted successfully
; mysqli_close(< span class="hl-var">$conn); ?>< /span>
< /span>
< /span>
< /span>
< /span>

< span class="hl-var">$dbhost = localhost:3306; // 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 successfully connected
; $sql = DROP DATABASE RUNOOB; $retval = mysqli_query( $conn, $sql ); if(! $retval ) { die( Failed to delete the database: . mysqli_error($conn)); < span class="hl-brackets">} echo The database RUNOOB was successfully deleted
; mysqli_close($conn); ?>
span>
span>
span>
span>

$dbhost = localhost:3306; // 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 successfully connected
; $sql = DROP DATABASE RUNOOB; $retval = mysqli_query( $conn, $sql ); if(! $retval ) { die(< span class="hl-quotes">‘ failed to delete the database: . mysqli_error($conn)); } echo Database RUNOOB deleted successfully
; mysqli_close($conn); ?>< /span>
< /span>
< /span>
< /span>
< /span>

Leave a Comment

Your email address will not be published.