SQL Statements
DROP statement
Overview
In this section, we will learn how to delete the data from a table using the DROP
statement.
Running a
DROP
statement will also delete all existing records from the table.Syntax
The basic syntax for the DROP
statement is as follows:
DROP TABLE table_name;
In this syntax, we have several elements that are elaborated on below.
-
DROP
represents the statement name. -
TABLE
entity type you want to drop. -
table_name
defines which table you want to remove.
The
DROP
example below is executed in the public
schema. You can also drop a table from another specific schema. Click here for more info.Example
We have a table named warehouse that stores the product’s id, name, and quantity:
CREATE TABLE warehouse (
id int,
product string,
qty int
);
INSERT INTO warehouse
(id, product, qty)
VALUES
(889771,'Shirt',22),
(777821,'Hat',99),
(103829,'Bed Cover',12);
SELECT * FROM warehouse;
It will create a table as shown below:
+---------+------------+---------+
| id | product | qty |
+---------+------------+---------+
| 889771 | Shirt | 22 |
| 777821 | Hat | 99 |
| 103829 | Bed Cover | 12 |
+---------+------------+---------+
- Use the following query to delete the warehouse’s table:
DROP TABLE warehouse;
- If you have successfully run the query, you will get the following output: the table and the values within it are deleted.
DROP TABLE
Query returned successfully in 284 msec.
If you attempt to use the table for any operation, you will find that the table no longer exists.