MySQL delete database


MySQL Delete Database

Use a normal user to log in to the MySQL server. You may need specific permissions to create or delete a MySQL database, so we use root here. User login, root user has the highest authority.

Be very careful when deleting the database, because all data will disappear after executing the delete command.

1. The drop command deletes the database

drop command format:

drop database <数据库名>;

For example, to delete the database named DEMO:

mysql> drop database DEMO;

Related video recommendations: SQL Basic Operation - Delete Database

2. Use mysqladmin to delete the database

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

The following example deletes the database DEMO (the database has been created in the previous chapter):

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

After executing the above delete database command, a prompt box will appear to confirm whether the database is really 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 'DEMO' database [y/N] yDatabase "DEMO" dropped

Recommended related video tutorials: How to use phpmyadmin to add, delete, modify and check databases and data tables

3. Use PHP script to delete the database

PHP uses the mysqli_query function to create or delete the MySQL database.

This function has two parameters and returns TRUE when executed successfully, otherwise it returns FALSE.

Syntax

mysqli_query(connection,query,resultmode);
ParametersDescription
connection Required. Specifies the MySQL connection to use.
query Required, specifies the query string.
resultmode

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

  • MYSQLI_USE_RESULT (use this if you need to retrieve large amounts of data)

  • MYSQLI_STORE_RESULT (default)

#Example

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

<?php
$dbhost = 'localhost'; // mysql server host address
$dbuser = 'root' ; ##{
die('Connection failed: ' . mysqli_error($conn));
}
echo 'Connection successful<br />';
$sql = 'DROP DATABASE DEMO ';
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
die('Failed to delete database: ' . mysqli_error($conn));
}
echo "Database DEMO deleted successfully\n";
mysqli_close($conn);
?>


After successful execution, the result is

Image 3.jpgNote: When using a PHP script to delete a database, no confirmation message will appear. The specified database will be deleted directly, so you are deleting the database. Be especially careful when doing so.


Related video recommendations:

PHP forum project--database addition, deletion, modification, entry file, config file, function file

Update Recommended multiple tutorials:

mysql video tutorial

mysql native manual

mysql database technical article

Introduction to database learning