Why to DROP TABLE?

While creating tables, by mistake we may provide some information of the table wrongly. The information may be a table name or column name or the data type of the column etc, In some cases, the table might create long back and that does not fit into the current requirement but still we need the table to name as per requirements or delete the table to free the used memory. There are some more requirements that may pull us to delete the existing table and create them again.

To delete the table from the database, DROP TABLE statement is used.

How to DROP TABLE?

DROP TABLE is used to delete the existing table in database. By using DROP TABLE statement, we can delete the data in table and whole structure of the table. It also deletes all the relationships and permissions of the table.

Syntax -

 DROP TABLE [IF EXISTS] [database_name.][schema_name.]table_name;
  • Database_name - Specifies the database name where the table exists.
  • Schema_name - Specifies the schema under which the table exists.
  • table_name - Specifies the name of the table.
Note :- It is always suggestable to take a backup of the data before dropping a table. Because once the table dropped, the data in the table lost permanently.

Examples -

Let us consider below table(s) as an example table(s) to frame the SQL query for getting the desired results.

employee_details -

emp_id ename designation manager_id date_of_hire salary dept_id
001 Employee1 Director 2019-07-11 60000.00 1000
002 Employee2 Director 2019-07-11 40000.00 2000
003 Employee3 Manager Employee1 2019-07-11 27000.00 1000
004 Employee4 Manager Employee2 2019-10-08 25000.00 2000
005 Employee5 Analyst Employee3 2019-07-11 20000.00 1000
006 Employee6 Analyst Employee3 2019-10-08 18000.00 1000
007 Employee7 Clerk Employee3 2019-07-11 15000.00 1000
008 Employee8 Salesman Employee4 2019-09-09 14000.00 2000
009 Employee9 Salesman Employee4 2019-10-08 13000.00 2000

Dropping a simple table

In the below example, we are deleting employee_details table. This deletes the entire the table along with the data in it. The DROP statement for the same is -

 DROP TABLE employee_details; 

If the above query executed successfully, then the table employee_details gets deleted successfully.

The above DROP statement works when the table is stand alone and do not have any reference key columns from other tables. Otherwise, it will throw the below error -

Could not drop object 'employee_details' because it is referenced by a FOREIGN KEY constraint.

Delete referenced and referencing table

If a table contain foreign key and referenced by another table, SQL does not allow to drop referenced table. If we want to delete referenced table, first we have deleted foreign key, or the referencing table then delete the referenced table.

Consider the below table as an example to explain more about above scenario.

department_details -

CREATE TABLE department_details (
    dept_id int PRIMARY KEY,
    dept_name VARCHAR(255),
    dept_location VARCHAR(255)
  ); 

employee_details -

CREATE TABLE employee_details (
   emp_id int,  emp_name varchar(255),
   designation varchar(255),
   manager_id varchar(255),
   manager_id varchar(255),
   date_of_hire date,
   salary decimal(7,2),
   dept_id int NOT NULL,
   FOREIGN KEY (dept_id) REFERENCES department_details (dept_id)
 ); 

department_details is the referenced table and employee_details is the referencing table.

As we discussed before, dropping the referenced table throws an error without deleting referencing table. So, we have to delete referencing table first and next delete the referenced table.

In this example, department_details will not get deleted until or unless employee_details gets deleted. So first delete employee_details table with below query -

DROP TABLE employee_details; 

So that all the references for the department_details get dropped. Now, we can delete department_details by using below query –

DROP TABLE department_details; 

If both statements are executed successfully, then both tables got deleted successfully.

We can combine both DROP statements into a single statement to delete the tables. That is -

DROP TABLE employee_details, department_details; 

Make sure that referencing table should be first and referenced table should be next.

Verifying table

Once the DROP statement executed successfully, then we can conclude that the table gets deleted successfully. However, it is always best practice to verify once before confirming to the external world.

To verify the table gets deleted or not, just trigger a select query on the table like below -

SELECT * FROM department_details;

It should trigger an error like "Invalid Object". So that we can conclude that the table gets deleted successfully.

What happen if use try to delete the non-existing table in database?

If we are trying to delete a non-existing table, then system throws an error saying, 'Cannot find object "table_name"'.