YouTip LogoYouTip

Mysql Drop Database

# MySQL Delete Database * * * When logging into a MySQL server with a regular user, you may need specific privileges to create or delete MySQL databases. Therefore, we will use the root user to log in here, as the root user has the highest privileges. During the process of deleting a database, you must be extremely cautious, because after executing the delete command, all data will be lost. ## Deleting a Database with the DROP Command The syntax for the DROP command is: DROP DATABASE ; -- Directly delete the database without checking if it exists or DROP DATABASE ; **Parameter Description:** * `IF EXISTS` is an optional clause that indicates the delete operation should only be executed if the database exists, avoiding errors caused by the database not existing. * `database_name` is the name of the database you want to delete. For example, to delete a database named : -- Directly delete the database without checking if it exists mysql> DROP DATABASE ;-- Delete the database if it exists DROP DATABASE IF EXISTS ; **Note:** Before executing the delete database operation, please ensure you truly want to delete the database and all its data, as this operation is irreversible. To avoid accidental deletion, it is usually recommended to back up the database before executing the delete. ## Deleting a Database with mysqladmin You can also use the MySQL `mysqladmin` command in the terminal to execute the delete command. Here is the command to delete a database using `mysqladmin`: mysqladmin -u your_username -p drop your_database **your_username** is the MySQL username, and **your_database** is the name of the database to be deleted. After executing this command, the system will prompt for a password. Enter the password and press the Enter key to delete the database. The following example deletes the database (which was created in the previous chapter):[root@host]# mysqladmin -u root -p drop Enter password:****** After executing the above delete database command, a confirmation prompt will appear to confirm whether you really want 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 '' database [y/N] y Database "" dropped * * * ## Deleting a Database Using a PHP Script PHP uses the `mysqli_query` function to create or delete MySQL databases. This function takes two parameters and returns TRUE on success, or FALSE on failure. ### Syntax mysqli_query(connection,query,resultmode); | Parameter | Description | | --- | --- | | _connection_ | Required. Specifies the MySQL connection to use. | | _query_ | Required. Specifies the query string. | | _resultmode_ | Optional. A constant. Can be one of the following values: * MYSQLI_USE_RESULT (Use this if you need to retrieve a large amount of data) * MYSQLI_STORE_RESULT (default) | ### Example The following example demonstrates using the PHP `mysqli_query` function to delete a database: ## Delete Database <?php$dbhost = 'localhost'; // MySQL server host address$dbuser = 'root'; // MySQL username$dbpass = '123456'; // MySQL user password$conn = mysqli_connect($dbhost, $dbuser, $dbpass); if(! $conn){die('Connection failed: ' . mysqli_error($conn)); }echo'Connected successfully
'; $sql = 'DROP DATABASE '; $retval = mysqli_query($conn, $sql); if(! $retval){die('Failed to delete database: ' . mysqli_error($conn)); }echo"Database deleted successfullyn"; mysqli_close($conn); ?> After successful execution, the result is: !(#) **Note:** When using a PHP script to delete a database, there will be no confirmation prompt asking if you want to delete. It will directly delete the specified database, so be especially careful when deleting databases.
← Mysql Select DatabaseMysql Create Database β†’