SQL DROP or DELETE Table

By | October 2, 2021
SQL DROP or DELETE Table

If we do not require a table in a database then we can simply delete it by using the SQL DROP TABLE command. DROP TABLE comes very usefully when we especially want to delete the complete table, and while deleting a table make sure that you delete the correct one.

Syntax

Follow this syntax to delete a table from the Database:

Vamware
DROP TABLE table_name;

Example

Query: First Select a Database:

USE student_db;

Output

Database changed

Query: Create a Temporary Table which you want to delete:

CREATE TABLE temp(
    temp_column_1 VARCHAR(3),
    temp_column_2 INT(2));

Output

Query OK, 0 rows affected (0.44 sec)

Query: Verify is table is created:

SHOW TABLES;

Output

+----------------------+
| Tables_in_student_db |
+----------------------+
| student_backup       |
| students             |
| temp                 |
+----------------------+
3 rows in set (0.04 sec)

Query: Delete Table Temp:

DROP TABLE temp;

Output

Query OK, 0 rows affected (0.39 sec)

Query: Verify If the table is deleted:

SHOW TABLES;

Output

+----------------------+
| Tables_in_student_db |
+----------------------+
| student_backup       |
| students             |
+----------------------+
2 rows in set (0.00 sec)

Summary

  • To Delete a table from a Database we use the DELETE TABLE command.
  • Before we delete a table, first we need to select the specific database.
  • Make sure you write the table name correct, once the table is deleted you will lost all the data of that table.

People are also reading:

Leave a Reply

Your email address will not be published. Required fields are marked *