Home >Database >Mysql Tutorial >How to delete data table in mysql?

How to delete data table in mysql?

青灯夜游
青灯夜游Original
2020-10-16 14:34:193918browse

In mysql, you can use "DROP TABLE" to delete one or more data tables. The syntax format is "DROP TABLE [IF EXISTS] table name 1 [ , table name 2, table name 3 ...]; "; When deleting the table, the structure of the table and all data in the table will be deleted.

How to delete data table in mysql?

(Recommended tutorial: mysql video tutorial)

In the MySQL database, for data tables that are no longer needed , we can delete it from the database.

When deleting a table, the table structure and all data in the table will be deleted, so it is best to back up the data table before deleting it to avoid irreparable losses.

How to delete data tables in MySQL database

Use the DROP TABLE statement to delete one or more data tables,Syntax format As follows:

DROP TABLE [IF EXISTS] 表名1 [ ,表名2, 表名3 ...]

The syntax format is explained as follows:

  • Table name 1, table name 2, table name 3...indicates what is to be deleted The name of the data table. DROP TABLE can delete multiple tables at the same time. Just write the table names at the end and separate them with commas.

  • IF EXISTS is used to determine whether the table exists before deleting it. If IF EXISTS is not added, MySQL will prompt an error and interrupt the execution of the SQL statement when the data table does not exist; after adding IF EXISTS, when the data table does not exist, the SQL statement can be executed smoothly, but a warning will be issued.

Two points to note:

  • The user must have the authority to execute the DROP TABLE command, otherwise the data table will not be delete.

  • When a table is deleted, the user's permissions on the table will not be automatically deleted.

Example:

Query the data table of test_db database

mysql> USE test_db;
Database changed
mysql> SHOW TABLES;
+--------------------+
| Tables_in_test_db  |
+--------------------+
| tb_emp2            |
| tb_emp3            |
+--------------------+
2 rows in set (0.00 sec)

As can be seen from the running results, test_tb There are two data tables tb_emp2 and tb_emp3 in the database.

Let’s delete the data table tb_emp3. The input SQL statement and the execution result are as follows:

mysql> DROP TABLE tb_emp3;
Query OK, 0 rows affected (0.22 sec)
mysql> SHOW TABLES;
+--------------------+
| Tables_in_test_db  |
+--------------------+
| tb_emp2            |
+--------------------+
1 rows in set (0.00 sec)

As you can see from the execution result, the name tb_emp3 no longer exists in the data table list of the test_db database. table, the delete operation was successful.

The above is the detailed content of How to delete data table in mysql?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn