Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

MySQL Drop Database

MySQL Drop Database


When logging into the MySQL server as a regular user, you may need specific privileges to create or drop a MySQL database, so here we log in as the root user, who has the highest privileges.

When dropping a database, you must be very careful, because after executing the drop command, all data will disappear.

Dropping a Database with the drop Command

The drop command format:



DROP DATABASE ;        -- 直接删除数据库,不检查是否存在

或

DROP DATABASE [IF EXISTS] ;

Parameter Description:

For example, to drop the database named RUNOOB:



-- Drop the database directly, without checking if it exists

mysql> DROP DATABASE RUNOOB;



-- Drop the database if it exists

DROP DATABASE IF EXISTS RUNOOB;

Note: Before performing a drop database operation, make sure you really want to delete the database and all its data, because the operation is irreversible. To avoid mistakes, it is usually recommended to back up the database before dropping it.

Dropping a Database with mysqladmin

You can also use the MySQL mysqladmin command in the terminal to execute the drop command.

The following is the command to drop a database with mysqladmin:

mysqladmin -u your_username -p drop your_database

your_username is the MySQL username,your_database is the name of the database to drop.

After executing this command, the system will prompt for a password. Enter the password and press Enter to drop the database.

The following example drops the database RUNOOB (which was created in the previous chapter):



[root@host]# mysqladmin -u root -p drop RUNOOB

Enter password:******

After executing the above drop database command, a prompt appears to confirm whether you really want to drop 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' database [y/N] y

Database "RUNOOB" dropped


Dropping a Database with a PHP Script

PHP uses the mysqli_query function to create or drop a MySQL database.

This function takes two parameters and returns TRUE on success, or FALSE otherwise.

Syntax



mysqli_query(connection,query,resultmode);

Parameters Description
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 dropping a database using the PHP mysqli_query function:

    Drop the database

    ' ; $sql = ' DROP DATABASE RUNOOB ' ; $retval = mysqli_query ( $conn , $sql ) ; if ( ! $retval ) { die ( ' Failed to drop database: ' . mysqli_error ( $conn ) ) ; } echo " Database RUNOOB dropped successfully \n " ; mysqli_close ( $conn ) ; ?>

    After successful execution, the result is:

    Note: When dropping a database with a PHP script, no confirmation message appears; the specified database is deleted directly, so you must be especially careful when dropping a database.