Home > Database > Mysql Tutorial > How to delete table from database

How to delete table from database

青灯夜游
Release: 2021-03-18 14:51:28
Original
33449 people have browsed it

In the database, you can use the "DROP TABLE" statement to delete one or more data tables. The specific syntax is "DROP TABLE [IF EXISTS] table name 1 [ , table name 2, table name 3 ... ]", where "IF EXISTS" is used to determine whether the table exists before deleting the data table.

How to delete table from database

The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.

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

You can use the DROP TABLE statement to delete one or more data tables. The syntax format is as follows:

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

Instructions:

  • IF EXISTS: Used to determine whether the table exists before deleting the data table. 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.

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

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

(Recommended tutorial: mysql video tutorial)

Example:

First check the test_db database Data table

mysql> USE test_db;
Database changed

mysql> SHOW TABLES;
+--------------------+
| Tables_in_test_db  |
+--------------------+
| tb_emp1           |
| tb_emp2            |
| tb_emp3            |
| tb_emp3            |
+--------------------+
4 rows in set (0.00 sec)
Copy after login

Use the DROP TABLE statement to delete the data table

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

The execution result can be seen that the name tb_emp3 no longer exists in the data table list of the test_db database. table, the delete operation was successful.

For more programming related knowledge, please visit: Programming Video! !

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template