Tech

How to Delete or Drop a Table in MySQL

Introduction

MySQL is renowned as the most prevalent relational database management system. It is typically employed for storing and managing data. In MySQL, tables are utilized to organize as well as store data in a structured manner. Occasionally, it may be necessary for you to delete or drop a table because of various reasons, like the table is no longer required. Or, you wish to build a new table with the same name.

Under this guide, you will get insights into how to delete or drop a table in MySQL with the help of different methods. All you need to make sure is that you meet the following prerequisites – 

  • Accessibility to a terminal window/command line
  • A system running MySQL
  • A working or test database
  • A MySQL user with the essential privileges (DROP privileges needed)

Continue reading and grab all the essential details. 

DROP TABLE MySQL Command Syntax

DROP TABLE MySQL Command Syntax

The DROP TABLE command is employed to delete a table and all its data permanently from a database. Following is the syntax for the DROP TABLE command in MySQL:

DROP TABLE table_name;
DROP TABLE command in MySQL

Here, table_name is the name of the table that you wish to drop.

You must note that dropping a table in SQL will permanently delete all data linked to that table. So, you have to use this command with caution and ensure to take a backup of the data before dropping the table.

Use the DROP Statement to Remove a Table

Certainly! Here’s an illustration of how to DROP a table named “company” from a database:

DROP TABLE company;

When you operate this command, MySQL will instantly delete the “company” table and all the data linked with it from the database. Since you can not undo this action, you must ensure that you delete the table from SQL and all its data.

Use DROP to Remove Only Existing Tables

Use DROP to Remove Only Existing Tables

When utilizing the DROP statement to remove a table in MySQL, it is essential to make sure that the table exists in the database. Here is an illustration of how to check if a table exists before executing the DROP statement to avoid any errors:

DROP statement to remove a table in MySQL

— Check if table exists

IF EXISTS(SELECT * FROM information_schema.tables WHERE table_name = ‘company’) THEN

   — Table exists, so drop it

   DROP TABLE company;

END IF;

DROP statement to remove a table in MySQL

This code first checks if the table “company” exists in the database by querying the information_schema.tables table. In case the table exists, it is dropped with the usage of the DROP TABLE statement. Whereas if the table does not exist, the DROP TABLE statement is not conducted, avoiding any errors.

By utilizing this approach, you can ensure that the DROP statement is only conducted if the table exists in the database. Parallely, you must avoid any potential errors that could happen if you attempt to drop a table that does not exist.

How to DROP Multiple Tables?

To drop multiple tables at once in MySQL, you can simply include the names of all the tables you wish to drop in the DROP TABLE statement, separated by commas. The following is an illustration of how to drop three tables named “tableS”, “tableP”, and “tableR” using a single DROP TABLE statement:

DROP TABLE tableS, tableP, tableR;
DROP TABLE statement

When you conduct this command, MySQL will immediately drop all three tables and their associated data from the database.

You must know that dropping multiple tables simultaneously can be a potentially dangerous operation. Thus, it is a wise idea to make sure you have a backup of the data before running the command. You should be certain that you desire to delete all the tables & their associated data.

How to DROP a Temporary Table

How to DROP a Temporary Table

For dropping a temporary table in MySQL, you can utilize the same syntax as for dropping a regular table. However, you need to add the TEMPORARY keyword. Here is an illustration of how to drop a temporary table named “temp_table”:

DROP TEMPORARY TABLE temp_table;

drop a temporary table

When you enter this command, MySQL will immediately drop the temporary table as well as its associated data from the database.

You must keep in mind that the temporary tables are automatically dropped when the database connection is closed or when the session ends. However, you may still wish to explicitly drop a temporary table before the end of the session, in case you are required to free up memory or disk space.

Also Read: Check Disk Space in Linux: df and du Commands

How to DROP Tables Based on Character Strings

How to DROP Tables Based on Character Strings

You can delete or drop a table in MySQL based on character strings in its name. For this, you can employ the LIKE operator in the DROP TABLE statement. The LIKE operator is employed to match a string pattern, so you can define a pattern that corresponds to the names of the tables you wish to drop.

Here is an illustration of how to drop all tables in SQL that start with “temp_” using the LIKE operator:

DROP TABLE IF EXISTS `temp_%`;

This statement employs the IF EXISTS clause to avoid any errors if a table does not exist. The % symbol is a wildcard character that corresponds to any number of characters in a table name. Thus, this statement will drop all tables whose names start with “temp_”.

You can also utilize other wildcard characters with the LIKE operator to match more detailed patterns in table names. For instance, you can employ the underscore (_) wildcard character to check any single character. The following is an example of how to drop all tables whose names contain the word “archive”:

DROP TABLE IF EXISTS `%archive%`;

This statement helps in dropping all tables whose names include the string “archive”, regardless of where it appears in the name.

Conclusion

To delete or drop a table in MySQL, you can easily make use of the DROP TABLE statement. This statement helps you entirely remove the table as well as all of its data from the database. Therefore, it is essential to be cautious when utilizing the statement. Further, you must always back up your database before executing any operation that can result in data loss. At the same time, you must also be aware that dropping a table will also terminate any associated triggers, indexes, or constraints. In case you wish to keep these details, you should first drop them manually before dropping the table. Overall, dropping a table in MySQL is a robust operation that should be used with high caution. You must always ensure you have a backup of your data & double-check your commands before implementing them.

Arpit Saini

He is the Chief Technology Officer at Hostbillo Hosting Solution and also follows a passion to break complex tech topics into practical and easy-to-understand articles. He loves to write about Web Hosting, Software, Virtualization, Cloud Computing, and much more.

Leave a Reply

Your email address will not be published. Required fields are marked *