SQL DROP TABLE Statement
The SQL DROP TABLE
statement is used to remove the table definition and all its data.
Syntax
The syntax of the DROP TABLE
statement is as follows:
DROP TABLE table_name;
Parameters:
table_name
: The name of the table that you want to remove from the database.
Note: Dropping a table is irreversible. Be careful when using this command once a table is removed, then all the information available in that table will be permanently lost forever.
SQL DROP TABLE Example
You may found yourself that you have created a table that you no longer need. You can use the DROP TABLE
statement to remove the table from the database.
For our example, if you remember in the create table chapter, we have created a table "Student" in our database. The following SQL statement will remove this table permanently from the database.
DROP TABLE student;
After executing the above command, both the data associated with the "Student" table and its table definition will be permanently removed.
Now, If you try the DESC student
command, you will get the error that the table "Student" doesn't exist.
After dropping the table, you can recreate the "Student" table using the CREATE TABLE statement without getting an error that the table already exists.
You can also use the database name prefix with the table name when you drop a table.
DROP TABLE demo_db.employee;
In the above example, we have dropped a table called "Employee" from the "demo_db" database.