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.
To implement the examples given in the article, you can use SQL in the following ways.
- Install MySQL in your local machine.
- 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.
- 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)
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.
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