InSQL, the DROP TABLE statement is used to delete table definitions and all data, indexes, constraints, and table parameters.

drop table command

drop table command

Before using the DROP TABLE command, you must carefully consider, because when this command is executed the entire data in the table will be permanently deleted.

The syntax of DROP TABLE:

DROP TABLE ten_bang;

Example of the DROP TABLE command

First, we must confirm that the table NHANVIEN exists and then delete the table fromthe database, as the example below:

SQL> DESC NHANVIEN;
+-----------+---------------+------+-----+---------+-------+
| Field     | Type          | Null | Key | Default | Extra |
+-----------+---------------+------+-----+---------+-------+
| ID        | int(11)       | NO   | PRI |         |       |
| TEN       | varchar(20)   | NO   |     |         |       |
| TUOI      | int(11)       | NO   |     |         |       |
| DIACHI    | char(25)      | YES  |     | NULL    |       |
| LUONG     | decimal(18,2) | YES  |     | NULL    |       |
+---------+---------------+------+-----+---------+---------+
5 rows in set (0.00 sec)

That means that the NHANVIEN table exists in the database, so now we will remove it using the DROP TABLE command:

SQL> DROP TABLE NHANVIEN;
Query OK, 0 rows affected (0.01 sec)

Now you can check whether the table deletion succeeded by reusing the DESC command:

SQL> DESC NHANVIEN;
ERROR 1146 (42S02): Table 'QTM.NHANVIEN' doesn't exist

If you receive the error message above, the table has deleted from your database.

In QTM.NHANVIEN, NHANVIEN is the name of the table, while QTM is the illustrated database used for the examples in this series.

Leave a Comment

Close