Site icon TestingDocs.com

MySQL DROP TABLE Statement

Overview

In this tutorial, we will learn MySQL DROP TABLE statement with examples. The DROP TABLE removes one or more tables. All the table data and the table definition will be removed.

A DROP TABLE statement cannot be undone. This is a destructive statement, so be careful and use caution when executing this statement.

Syntax

The general syntax for the statement is as follows:

mysql> DROP [TEMPORARY] TABLE [IF EXISTS] <database_table_name>

We can also use the DROP statement to drop multiple database tables. We must have the DROP privilege for dropping each table.

mysql> DROP [TEMPORARY] TABLE [IF EXISTS] <table_name1> [{, <table_name2>}…]

 

The TEMPORARY keyword drops only the TEMPORARY tables. A TEMPORARY table is visible only to the client that created it. TEMPORARY keyword is a good way  to ensure that we do not accidentally drop a non-TEMPORARY table.

Use IF EXISTS to prevent an error from occurring for tables that do not exist in the database. A NOTE message is generated for each non-existent table when using the IF EXISTS clause in the statement.

Examples

Let’s drop a table R_Actor from the database.

mysql> DROP TABLE IF EXISTS R_Actor;
Query OK, 0 rows affected (0.10 sec)

A successful DROP statement returns Query OK message. We can verify using the SHOW TABLES command. The table should be deleted from the list.

Warnings non-existent table

mysql> DROP TABLE IF EXISTS non_existent_table;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> SHOW WARNINGS;
+——-+——+——————————————-+
| Level | Code | Message |
+——-+——+——————————————-+
| Note | 1051 | Unknown table ‘sakila.non_existent_table’ |
+——-+——+——————————————-+
1 row in set (0.00 sec)

mysql>

MySQL Tutorials

MySQL Tutorials on this website:

https://www.testingdocs.com/mysql-tutorials-for-beginners/

For more information on MySQL Database:

https://www.mysql.com/

Exit mobile version