SQL Drop

Removing a Table from Database

You can use the DROP TABLE statement to easily delete database tables that are no longer needed. DROP TABLE permanently erases all data from the table, along with its metadata stored in the data dictionary.

Syntax

The DROP TABLE statement removes one or more tables. The syntax is as follows:

DROP TABLE table1_name, table2_name, ...;

Here, table1_name, table2_name, ... represent the names of the tables you wish to delete.

Warning: Deleting a database or table cannot be undone. Exercise caution when using the DROP statement, as database systems typically do not offer a confirmation step like "Are you sure?". This action permanently deletes the database or table and all its data.

Let's attempt to delete a database table using the DROP TABLE statement.

If you recall from the create table chapter, we created a table named persons in our demo database. The statement below will permanently delete this table from the database.

DROP TABLE persons;

After executing the above command, if you attempt to perform any operation on the persons table, such as selecting records from it, you will receive an error message.


Removing Database

Similarly, you can delete a database using the DROP DATABASE statement. The following command will permanently remove the demo database from the database server:

DROP DATABASE demo;

If you attempt to select the demo database using the USE demo; statement after its deletion, you will receive an error message indicating "Unknown database" or "Database does not exist".