Open In App

Drop Table in MariaDB

Last Updated : 23 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

MariaDB is one of the most widely used open-source relational database management systems, it offers many useful commands for manipulating database structures. It supports the same features that MySQL does but with additional features. Some of the most significant features are new storage engines, JSON API support, and parallel data replication.

One such command that performs an important role in database administration is the DROP TABLE command. In this article, We will discuss the DROP TABLE command in MariaDB along with syntax, table creation, and examples of dropping the table using multiple methods.

DROP TABLE in MariaDB

MariaDB’s DROP TABLE command is designed to delete a current table as well as all the data and objects related to it. The main scenarios for the use of DROP TABLE are the removal of outdated tables, reforming database structures, or starting from scratch when required. With the help of DROP TABLE, the data can be deleted permanently.

Syntax:

DROP TABLE [IF EXISTS] table_name;

Explanation:

  • DROP TABLE: This is the main clause to drop the table.
  • IF EXISTS: This is the optional clause. It ensures that the command does not produce an error if the specified table does not exist.
  • table_name: Name of the table.

Syntax to drop Multiple Tables:

DROP TABLE [IS EXISTS] table1, table2, table3,...;

Explanation: In this syntax, you specify a comma list of tables you want to remove after the drop table keywords.

Examples of DROP TABLE

For good understanding of the DROP TABLE Command, Let’s create an table through which we will understand the DROP TABLE Command better and get the deep understanding of DROP TABLE Command.

Employees Table

CREATE TABLE Employees 
(
emp_id int,
emp_name varchar(255) not null
);

Products Table

CREATE TABLE Products 
(
pro_name varchar(255),
price decimal(10,2)
);

States Table

CREATE TABLE States
(
id int,
state_name varchar(255),
area decimal (15,2)
);

Verify whether the table is created successfully or not executing the below command.

Query:

SHOW TABLES;

You can see below our tables got created successfully.

Output:

TABLESlist

Tables

Explanation: As we can see in the output that we have all tables which we ill created above.

Drop One Table

Let’s look at a simple DROP TABLE example that shows how to use the DROP TABLE statement to drop one table in MariaDB.

Query:

DROP TABLE employees;

Check using SHOW TBALES command.

Output:

DropEmployees

Drop table

Explanation: : As we can see in the output that now no table named as employees is there in database.

Drop Multiple Table

Let’s look at an example where we want to drop more than one table using the DROP TABLE statement.

Query:

DROP TABLE Products, States;

Output:

NoTables

Drop All Tables

Explnation: As we can see in the output that now no table named as product and states in database.

Drop table Using Management Studio

We can drop table using management studio also.

First create a new table called Books:

CREATE TABLE Books 
(
id int,
bookName varchar(255)
);

Step 1: Select the table that we want to delete and then right click on that. We will see a page like this:

drop

Drop Table

Step 2: Click on drop and then it will generate a pop up and click ok.

drop1

Drop Table

Step 3: We can verify that the table is deleted or not.

SHOW TABLES;
Tables3

No Tables in database

Precautions and Considerations

  • Backup Your Data: It is essential to backup the database prior to running the DROP TABLE command, or at a minimum, the table being dropped. This guarantees that we have a backup in case of mistake deletion.
  • Use IF EXISTS: The use of the “IF EXISTS” clause in the DROP TABLE statement serves to protect against errors in case of indicated table does not exist. This is a good way to prevent unwanted interruptions to the database.
  • Review Dependencies: If the table to be dropped has foreign key relationships to other tables, it is important to review and understand these associations. Accordingly, use the CASCADE or RESTRICT clauses to control the impact on dependent objects.
  • Double-Check Table Names: In order to running the DROP TABLE command, check the table names again to ensure their correctness. Data loss may happen if the wrong table by mistake is deleted, and it may be complex to recover.
  • Consider Transactional Safety: When DROP TABLE is performed as part of a larger sequence of operations, take an additional step and enclose the operation in a transaction. This allows for rollback in case of an error, ensuring database integrity.

Conclusion

Overall, the DROP TABLE command in MariaDB is a powerful tool for database structure management, but it has great responsibilities associated with it. This command must be used by database administrators with caution, best practices, and consequences. Effective risk mitigation steps related to the DROP TABLE include performing regular backups, using IF EXISTS, and understanding dependencies. With proper planning and following these pointers, administrators can exploit the potential of DROP TABLE in their MariaDB environments.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads