![]() |
MySQL is a free and open-source relational database management system written in C and C++ that is extremely popular among developers. Like other relational database management systems, MySQL provides various rich features to create databases and tables, insert data in them, and further manipulate them as the system evolves. In this article, we are going to have a look at one such functionality, the DROP TABLE statement. DROP TABLE in MySQLThe DROP TABLE statement drops one or more existing tables from the database. It removes the table definition and all the data inside the table, so be careful while using this statement. Syntax:
Parameters:
Use the DROP Statement to Remove a TableLet’s start by creating some tables and inserting records in it. We will later use these tables in the subsequent examples to understand the DROP TABLE statement better. The first table we are going to create is the EMPLOYEE table. The following query creates the table and then later inserts records in it. -- create employee table We will run the following query to fetch the initial data in the table: SELECT * FROM EMPLOYEE;
The following is the initial data in the table: ![]() Initial Data The second table, we are going to create is the MANAGER table. The following query creates the table and then later inserts records in it: -- create manager table We will run the following query to fetch the initial data in the table: SELECT * FROM MANAGER;
The following is the initial data in the table ![]() Initial Data The tables that we have created are all permanent tables, i.e. they will be persisted even after the session is closed. Now let’s create a temporary table. The temporary tables do not persist and are only present for the duration of the current session. We will create a temporary table ORGANISATION using the records we already have in the EMPLOYEE and MANAGER tables. The following query creates the table and populates it with records: -- create temporary table organisation We will run the following query to fetch the initial data in the table: SELECT * FROM ORGANISATION;
The following is the initial data in the table ![]() Initial Data Dropping a Temporary TableExample: Dropping Temporary Table and Handling Non-Existent Table ErrorIn this example, we will see how we can drop a temporary table. The following query drops the table ORGANISATION that we created above. DROP TEMPORARY TABLE ORGANISATION;
Note that we used the TEMPORARY keyword to specify to the engine that we are deleting a temporary table. Now if we run the following query: SELECT * FROM ORGANISATION;
We will get the following error: ERROR 1146 (42S02) at line line_num: Table 'db_name.organisation' doesn't exist
Explanation: Dropping the temporary table ORGANISATION using DROP TEMPORARY TABLE removes it from the session. Attempting to query it afterward results in an “ERROR 1146” as the table no longer exists. This underscores the ephemeral nature of temporary tables and the need for careful session-specific management. Dropping Multiple TablesExample: Dropping Multiple Tables and Handling Subsequent Query ErrorsIn this example let’s try to delete multiple tables. The following query drops the tables EMPLOYEE and MANAGER tables that we created. DROP TABLE EMPLOYEE, MANAGER;
Now if we try to use the above tables in any subsequent query we will get an error message. Explanation: Executing the query DROP TABLE EMPLOYEE, MANAGER; removes both the EMPLOYEE and MANAGER tables. Subsequent attempts to use these tables in queries result in error messages, specifically indicating that the tables no longer exist. Handling Non-Existent Table with DROP TABLE IF EXISTSExample: Handling Non-Existent Table with DROP TABLE IF EXISTSIn this example, we will try to drop a table that doesn’t exist. The following query tries to drop table STUDENT which does not exist. DROP TABLE STUDENT;
Once you run this command, you will get the following query. ERROR 1146 (42S02) at line line_num: Table 'db_name.student' doesn't exist
We can avoid the error message by running the following query: DROP TABLE IF EXISTS STUDENT
Here we made use of the IF EXISTS keyword. Executing the above query doesn’t throw any error. Explanation: The initial attempt to drop the non-existent table STUDENT results in an “ERROR 1146” as it’s not found. Using the DROP TABLE IF EXISTS STUDENT prevents errors by checking for existence before attempting to drop, providing a smooth execution even if the table doesn’t exist. ConclusionIn this article, we went through the DROP TABLE statement. We had a chance to look at the different use cases as well as different keywords that we can use along the statement. As you can see the DROP TABLE statement provides the developer immense power in playing with data and tables. However, one should be careful about using the DROP TABLE statement as once committed there is no way to retrieve the data back. FAQs on MySQL DROP TABLE StatementWhat is the DROP TABLE statement in MySQL?
What happens to the data in the table when you use the DROP TABLE statement?
Can you use the DROP TABLE statement on views or temporary tables?
|
Reffered: https://www.geeksforgeeks.org
Databases |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |