Thursday 12 July 2018

Using MySQL DROP TABLE To Remove Existing Tables

Using MySQL DROP TABLE To Remove Existing Tables?

Summary: in this tutorial, we will show you how to remove existing tables using the MySQL DROP TABLE statement.

MySQL DROP TABLE statement syntax

To remove existing tables, you use the MySQL DROP TABLE statement. The syntax of the DROP TABLE is as follows:
The DROP TABLE statement removes a table and its data permanently from the database. In MySQL, you can also remove multiple tables using a single DROP TABLE statement, each table is separated by a comma (,).
The TEMPORARY flag allows you to remove temporary tables only. It is very convenient to ensure that you do not accidentally remove non-temporary tables.
The IF EXISTS addition helps you prevent from removing non-existent tables. When you use IF EXISTS addition, MySQL generates a NOTE, which can be retrieved by using the SHOW WARNINGstatement. It is important to note that the DROP TABLE statement removes all existing tables and issues an error message or a NOTE when you have a non-existent table in the list.
As mentioned above, the DROP TABLE statement only removes table and its data. However, it does not remove specific user privileges associated with the table. Therefore if a table with the same name is re-created after that, the existing privileges will apply to the new table, which may pose a security risk.
The RESTRICT and CASCADE  flags are reserved for the future versions of MySQL.
Last but not least, you must have DROP privileges for the table that you want to remove.

MySQL DROP TABLE example

We are going to remove the tasks table that we created in the previous tutorial in the creating tables using CREATE TABLE statement tutorial. In addition, we remove a non-existent table to practice with the SHOW WARNING statement. The statement to remove the tasks table and a non-existent table with the name nonexistent_table is as follows:
If you check the database, you will see that the tasks table was removed. You can check the NOTE, which is generated by MySQL because of non-existent table, by using the SHOW WARNING statement as follows:

MySQL DROP TABLE with LIKE

Image you have a lot of tables whose names start with test in your database and you want to save time by removing all of them using a single DROP TABLE statement. Unfortunately, MySQL does not provide the DROP TABLE LIKE statement that can remove tables based on pattern matching like the following:
However, there are some workarounds. We will discuss one of them here for your reference.
Let’s start creating test* tables for the sake of demonstration.
We’ve created four tables named test1 , test2 , test3  and test4  with the same table structure.
Suppose you want to remove all test* tables at a time, you can follow the steps below:
First, you declare two variables that accept database schema and a pattern that you want to the tables to match:
Next, you need to build a dynamic DROP TABLE statement:
Basically, the query instructs MySQL to go to the information_schema  table , which contains data on all tables in all databases, and to concatenate all tables in the database @schema ( classicmodels that matches the pattern @pattern test% ) with the prefix DROP TABLE The GROUP_CONCAT functioncreates a comma-separated list of tables.
Then, we can display the dynamic SQL to verify if it works correctly:

We can see that it works as expected.
After that, we can execute the statement using prepared statement in MySQL as follows:
For more information on MySQL prepared statement, check it out the MySQL prepared statementtutorial.
Putting it all together.
So if you want to drop multiple tables that have a specific pattern in a database, you just use the script above to save time. All you need to do is replacing the pattern and the database schema in @pattern and @schema variables. If you often have to deal with this task, you can always develop a stored procedurebased on the script and reuse this stored procedure.
In this tutorial, we’ve shown you how to use the DROP TABLE statement to remove existing tables in a particular database. We also discussed about a workaround that allows you to use the DROP TABLEstatement to remove tables based on pattern matching.

0 comments:

Post a Comment