MySQL Drop Table Statement literally drops the existing table from a database. The drop table removes or deletes all the records in a given one along with the structure or definition, and the syntax behind this is
DROP TABLE TableName; Or DROP TABLE Schema_name.TableName;
For this MySQL Drop Table demonstration, we are using the below.
MySQL Drop Table Example
In this example, we will delete the empdetails we have shown above.
NOTE: You should be careful while using this statement. You might lose your data and have to recreate the same MySQL table on your own.
DROP TABLE sqltest.empdetails;
Let me run the select statement against the table we deleted before. You can see the response, Error Code: 1146 stating that it does not exist.
If you try to remove the one that doesn’t exist, it throws an error. So, for instance, let me delete the already removed one, i.e., empdetails. From the below screenshot, you can see the error. 1051: Unknow table empdetails.
To avoid this, we can use the IF EXISTS parameter along with the DROP TABLE.
DROP TABLE IF EXISTS sqltest.empdetails;
As you see, the above code gives you a warning with a meaningful message.
Delete Table using Command Prompt
You can use the MySQL Drop Table statement from the command prompt or Terminal to eliminate it. Here, we delete the employee details and then try to select records from it. As you can see, it was throwing an error.
DROP TABLE sqltest.employeedetails; SELECT * FROM sqltest.employeedetails;
MySQL Drop Table Using Workbench
If you can access the Workbench, it is easy to delete any table. First, right-click on the one you want to delete. i.e., the dim customer. Next, select the Drop.. option.
It opens the pop-up window with Drop Now, Cancel, and ReviewSQL options. If you want, you can review the query generated by selecting the Review SQL or clicking on the Drop Now button.
Let us see the list of them under the sqltest schema.
MySQL Drop Multiple Tables
You must separate those names using a comma to delete more than one. This example deletes multiple tables, such as dim currency and department3.
DROP TABLE IF EXISTS sqltest.dimcurrency, sqltest.department3;
We successfully deleted the two.