How to Delete a Database in MariaDB

Here, we’ll go through how to delete a database in MariaDB and how to do so using the DROP DATABASE statement on the MariaDB Server. To aid you in understanding the topic better, we will debate and draw conclusions about a number of examples. The topics we will cover are listed below.

  • How to Delete a database in MariaDB?
  • How to delete a database by command line in MariaDB?
  • How to delete the database and user in MariaDB?
  • How to delete a database in MariaDB in the Linux Operating System?

MariaDB How to Delete Database

Here, we will learn how to delete a database in the MariaDB Server by the query, which is explained with the help of syntax and an illustrated example.

  • To remove a database from the active MariaDB Server, use the DROP DATABASE statement in MariaDB. You should be aware that this decision cannot be reversed, thus we must be quite cautious when making it.

The syntax is given below.

SYNTAX:

DROP DATABASE [ IF EXISTS ] DATABASE_NAME;

In the syntax explanation:

  • DROP DATABASE: Following the DROP DATABASE keywords, first give the name of the database you wish to delete.
  • IF EXISTS: Use the IF EXISTS statement to conditionally drop the database only if it is present. Without the IF EXISTS option, dropping a non-existent database will result in a MariaDB error. Instead, MariaDB issues a note if the IF EXISTS option is used.
  • DATABASE_NAME: It is the name of the database that we want to delete.

If you wish to correctly execute the drop database statement, you must have the drop privilege on the database you want to delete.

The following is an example of a MariaDB DROP DATABASE statement on a MariaDB Server:

First, check the available databases on the server using the below query.

SHOW DATABASES;

The above query shows the list of all the databases on the server, make note of the database that needs to be deleted.

Now use the below query to delete the database.

DROP DATABASE usa_census;

The usa_census database has been removed from the MariaDB Server using the DROP DATABASE statement in the above query.

Again check whether the specified database was deleted or not from the MariaDB server.

SHOW DATABASES;
MariaDB How to Delete Database
MariaDB How to Delete Database

From the above output, we can see that the database “usa_census” is deleted successfully from the MariaDB server.

Read: MariaDB Alter Table Compound Primary Key

MariaDB Delete Database Command Line

It is useful to understand the command-line method of deleting a MariaDB database. In order to complete their tasks, system administrators might be required to delete databases. This section demonstrates how to do that, which is a useful ability.

Use the following steps to delete the database from the MariaDB server.

The below command enables root access to the MariaDB server via the command line. The -u flag designates the root user, whereas the -p flag requests a password.

mysql -u root -p

To log in, enter your current password for the user if the above command asks, once finished, a MySQL prompt appears.

Use the following query to get a list of databases.

SHOW DATABASES;

From the list of databases choose or remember the name of the database that you want to delete. Now use the below statement to delete the database.

DROP DATABASE customer;

The above query deletes the “customer” database from the MariaDB server using the statement DROP DATABASE.

MariaDB Delete Database Command Line
MariaDB Delete Database Command Line

From the above output, the statement DROP DATABASE customer; returns the message Query OK, 1 row affected which means the database customer is deleted successfully.

The DROP DATABASE statement on the MariaDB Server by the query should have helped you understand how to delete the database via the command Line. We have shown an example and provided a detailed definition to aid in understanding.

Read: MariaDB See If Table Exists

MariaDB Delete Database And User

Here we will learn how to delete a database and user from the MariaDB Server using the statements DROP DATABASE and DROP USER.

First, we will understand how to drop the user from the MariaDB server. Please take note that if you select a connected account, the account will not be removed until the connection is terminated. There will be no automated termination of the connection.

The syntax is given below.

DROP USER [IF EXISTS] user_name [, user_name] ...
  • DROP USER: One or more MariaDB users are deleted via the DROP USER statement. It eliminates the account’s privilege rows from all grant tables.
  • IF EXISTS: When using the IF EXISTS clause with the statement DROP USER, MariaDB will return a message rather than an error if the user doesn’t exist.
  • user_name: It is the name of the user on the MariaDB server that needs to be deleted.

To view a list of the users in a MariaDB database, enter the following query at the prompt.

SELECT User FROM mysql.user;

From the list of users choose or remember the name of the user that you want to delete. Now use the below statement to delete the user.

DROP USER IF EXISTS 'James'@'localhost';
MariaDB Delete Database And User
MariaDB Delete Database And User

In the above output, we have deleted the user “James” successfully. To know about how to delete the database, please refer to the first subsection “MariaDB How to Delete Database” in this tutorial.

Read: MariaDB Order By Decreasing 

How to Delete Database in MariaDB Linux

In this MariaDB section, we will learn how to delete a database in the MariaDB Server in the Linux Operating System, For example, here we are using the MariaDB installed on the Ubuntu Os.

To perform the deletion of the database in MariaDB Linux, First, open the terminal and login into the MariaDB server.

$ mysql -u root -p 

The above command enables root access to the MariaDB server via the terminal of Linux. The -u flag designates the root user, whereas the -p flag requests a password. if the above command asks for a password, then enter the password for the user root.

To check the list of the available databases on the MariaDB server, use the below query.

SHOW DATABASES;

Choose or keep in mind the name of the database you wish to delete from the list of databases. To delete the database, use the statement given below.

DROP DATABASE united_states;

The above query uses the DROP DATABASE statement to remove the “united_states” database from the MariaDB server.

How to Delete Database in MariaDB Linux
How to Delete Database in MariaDB Linux

The statement DROP DATABASE customer yields the message Query OK, 0 row affected in the output picture above, indicating that the database customer has been successfully deleted.

We hope that you have understood the subtopic “How to Delete Database in MariaDB Linux” by using the MariaDB DROP DATABASE statement in the MariaDB Server by the query. For a better explanation, we have used a sample example and explained it in deepness.

Also, take a look at some more MariaDB tutorials.

Here in this tutorial, we understood How to Delete a Database in MariaDB after reading this lesson. Moreover, we have also discussed a few instances to help you comprehend the concept. Below is a list of all the topics we’ve covered.

  • How to Delete a database in MariaDB?
  • How to delete a database by command line in MariaDB?
  • How to delete the database and user in MariaDB?
  • How to delete a database in MariaDB in the Linux Operating System?