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!
- View the Contents of a Table in a SQL Server Database Using Enterprise Manager
- Create a MySQL Backup from Command Line – MySQL Import SQL File
- How to Install SQL Server 2012 Express on Windows Server 2012
Don’t see what you’re looking for? Use the search bar at the top to search our entire Knowledge Base.
The Hivelocity Difference
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.