Answered by the Webhosting Experts

How to Delete a Table in MySQL

If you need to delete a MySQL table from your system, you can do so easily using the MySQL Command Shell and the DROP TABLE command. Read on to learn all about the DROP TABLE command, and the various modifiers that can be used in conjunction with it.

Deleting a Table in MySQL

If you need to delete a table from MySQL, the first step is to access the MySQL Command Shell. If you’re not sure how to do that, just type MySQL into your system’s command terminal and hit Enter.

Once you’re in the MySQL shell, you can use the command DROP TABLE, followed by the name of table you are trying to delete to permanently remove the designated table from your system. *Note: even after deleting a MySQL table, its associated privileges will remain. This means if you create a new table with the same name as the deleted table, the new table will automatically inherit the old table’s privileges.

So, let’s say you have a table named table_A that you’re trying to delete. To do so, you could use the syntax:

DROP TABLE table_A;

*Note: when using commands in the MySQL shell, you must remember to end you commands with a semicolon “;” as shown above.

Now, if you’re trying to delete a table which does not exist (or if you simply enter the table’s name wrong), you’ll receive an error output from MySQL stating that the table does not exist. While this isn’t an issue on it’s own, if your DROP TABLE statement is included as part of a script, producing an error can cause your script to get stuck.

To solve this, add the modifier IF EXISTS to the command, like so:

DROP TABLE IF EXISTS table_Z;

If the table_Z referenced above does not exist, instead of printing an error, MySQL will now print a warning, allowing your script to continue running (mostly as intended).

If you need to delete multiple tables at once, you can do so by listing all the tables you’d like to delete in the same command, separated by commas:

DROP TABLE IF EXISTS table_A, table_B, table_C;

If you are trying to delete a temporary table or are worried you might accidentally delete a non-temporary table as well, you can add the TEMPORARY modifier to your command to ensure that only temporary tables are targeted. It would look something like this:

DROP TEMPORARY TABLE IF EXISTS table_temp1;

In this instance, the command would only delete the table table_temp1 if it is already designated as temporary. Otherwise, it would report a warning that the table does not exist.

And there you have it!

 

Popular Links

Looking for more information on MySQL? Search our Knowledge Base!

Interested in more articles about Databases? Navigate to our Categories page using the bar on the left or check out these popular articles:

Popular tags within this category include: MySQL, MSSQL, phpMyAdmin, PostgreSQL, and more.

Don’t see what you’re looking for? Use the search bar at the top to search our entire Knowledge Base.

 

The Hivelocity Difference

Seeking a better Dedicated Server solution? In the market for Private Cloud or Colocation services? Check out Hivelocity’s extensive list of products for great deals and offers.

With best-in-class customer service, affordable pricing, a wide-range of fully-customizable options, and a network like no other, Hivelocity is the hosting solution you’ve been waiting for.

Unsure which of our services is best for your particular needs? Call or live chat with one of our sales agents today and see the difference Hivelocity can make for you.

Need More Personalized Help?

If you have any further issues, questions, or would like some assistance checking on this or anything else, please reach out to us from your my.hivelocity.net account and provide your server credentials within the encrypted field for the best possible security and support.

If you are unable to reach your my.hivelocity.net account or if you are on the go, please reach out from your valid my.hivelocity.net account email to us here at: support@hivelocity.net. We are also available to you through our phone and live chat system 24/7/365.