[, ,
…];" statement deletes one or more data tables in the database."/> [,
…];" statement deletes one or more data tables in the database.">

Home  >  Article  >  Database  >  How to delete a table in the database in mysql

How to delete a table in the database in mysql

2019-05-17 11:31:24105693browse

Mysql method to delete tables in the database: 1. Use the "USE database name;" statement to select and enter the database where the table needs to be deleted; 2. Use "DROP TABLE [IF EXISTS]

[ ,
…];” statement deletes one or more data tables in the database.

How to delete a table in the database in mysql

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. Let's take a look at how to delete data tables in the MySQL database.

Basic syntax

When you need to delete a table, you can use the DROP TABLE statement to complete it. The syntax format is as follows:

DROP TABLE [IF EXISTS] <表名1> [ , <表名2> , <表名3> …];

The syntax is as follows:

: The name of the deleted table. The DROP TABLE statement can delete multiple tables at the same time, and the user must have permission for this command.

  • When the table is deleted, all table data and table definitions will be canceled, so be careful when using this statement.

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

  • The parameter IF EXISTS is used to determine whether the deleted table exists before deletion. After adding this parameter, when deleting the table, if the table does not exist, the SQL statement can be executed smoothly. But a warning will be issued.

  • Note: 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. .


    mysql> USE test_db;
    Database changed
    mysql> CREATE TABLE tb_emp3
        -> (
        -> id INT(11),
        -> name VARCHAR(25),
        -> deptId INT(11),
        -> salary FLOAT
        -> );
    Query OK, 0 rows affected (0.27 sec)
    mysql> SHOW TABLES;
    | Tables_in_test_db  |
    | tb_emp2            |
    | tb_emp3            |
    2 rows in set (0.00 sec)

    Delete the data table tb_emp3. The input SQL statement and running results 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)

    The execution results show that the table named tb_emp3 no longer exists in the data table list of the test_db database, and the deletion operation was successful.

    The above is the detailed content of How to delete a table in the database in mysql. For more information, please follow other related articles on the PHP Chinese 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
    Previous article:How to delete data in mysqlNext article:How to delete data in mysql

    Related articles

    See more