SQL Drop Table Example

While using databases and tables, you may encounter a situation when you will have to drop the table – including the data, and the structure of the table. For this purpose, you will use the SQL Drop Table command.

1. Pre-requisities

To implement the examples given in the article, you can use SQL in the following ways.

  1. Install MySQL in your local machine.
  2. You can also search for online SQL compilers.

2. What is SQL Drop Table?

SQL Drop Table command is used to drop a table which is currently existing in the database.

Note: It is important to be careful before you drop the table. Dropping a table would mean losing the entire information in the table.

Dropping a table is different from deleting a table. When you delete the table, only the records are deleted. When you drop a table, records as well as the definition of the table are deleted.

3. SQL Drop Table Syntax

The syntax for the SQL Drop Table command is as follows:

DROP TABLE tableName;

4. SQL Drop Table Example

SQL Drop Table command will work on an existing table in the database. So to understand the functionality of Drop Table, let us first create a table.

  1. Create a database named – DB, and a table named – info in the database. Let us consider two columns in the student database – id (of int data type), and name (of varchar data type)
SQL Drop Table - Creation of the database and table
Creation of the database and table

2. Now, you can use the Drop Table command to drop the entire table. Note that you can only drop a table if it exists in the database. And, you will get an error if you use the Drop Table command a second time.

SQL Drop Table - Usage
Usage of Drop Table command

5. Summary

In this article, we learned about the functionality and usage of the Drop Table command in SQL through an example. We also highlighted the importance of being careful while dropping the table, as it can delete the entire structure of the table along with data.

6. Download the source code

This article illustrated the usage of Drop Table command using an example.

You can download the full source code of this example here: SQL Drop Table Example

Simran Koul

Simran has graduated as a Bachelor of Engineering in Computer Science from Chitkara University. She has undergone a 6-months long comprehensive industrial training at the reputed Centre for Development of Advanced Computing (C-DAC), where she worked on a project including the likes of Java, JSP, Servlets while the UI-UX through the pioneering HTML, CSS and JS. Her inquisitive nature and the seed of curiosity keeps her on the toes to find material to write about. Along with her interests in Software Development, she is an ardent reader and always ready-to-write writer.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button