Mysql 简明教程
MySQL - Drop Tables
The MySQL DROP TABLE statement
DROP TABLE 语句是数据定义语言 (DDL) 命令,用于删除表的定义,及其数据、索引、触发器、约束和权限规范(如果任何)。简单来说,此语句会从数据库中删除整个表。
The MySQL DROP TABLE statement is a Data Definition Language (DDL) command that is used to remove a table’s definition, and its data, indexes, triggers, constraints and permission specifications (if any). In simple terms, this statement will delete the entire table from the database.
但是,在使用 DROP TABLE 命令时,我们需要注意到以下内容 -
However, while using DROP TABLE command, we need make note of the following −
-
You should be very careful while using this command because once a table is deleted then all the information available in that table will also be lost forever.
-
To drop a table in a database, one must require ALTER permission on the specified table and CONTROL permissions on the table schema.
-
Even though it is a data definition language command, it is different from TRUNCATE TABLE statement as the DROP statement completely removes the table from the database.
Dropping Tables from a database
为了从数据库中删除表,我们需要使用 MySQL DROP TABLE 命令。但在删除之前,我们必须确保表存在于数据库中。如果我们尝试删除数据库中不存在的表,将引发一个错误。
To drop tables from a database, we need to use the MySQL DROP TABLE command. But we must make sure that the table exists in the database, before dropping it. If we try to drop a table that does not exist in the database, an error is raised.
Example
让我们先通过执行以下语句创建 TUTORIALS 数据库 -
Let us start by creating a database TUTORIALS by executing below statement −
CREATE DATABASE TUTORIALS;
使用以下查询,将 TUTORIALS 修改为数据库 -
Using the following query, change the database to TUTORIALS −
USE TUTORIALS;
使用以下 CREATE TABLE 语句创建一个表 CUSTOMERS -
Create a a table CUSTOMERS using the following CREATE TABLE statement −
CREATE TABLE CUSTOMERS (
ID INT NOT NULL,
NAME VARCHAR(20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25),
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);
为了验证上面创建的内容是否在 TUTORIALS 数据库中,执行以下 SHOW TABLES 命令 -
To verify whether the above created is created in the TUTORIALS database or not, execute the following SHOW TABLES command −
SHOW TABLES IN TUTORIALS;
正如我们在下面给出的输出中看到,CUSTOMERS 表已在 TUTORIALS 数据库中创建。
As we can see in the output below, the CUSTOMERS table is created in the TUTORIALS database.
现在,让我们使用 MySQL DROP TABLE 语句删除上面创建的 CUSTOMERS 表 -
Now, let us use the MySQL DROP TABLE statement to delete the above created CUSTOMERS table −
DROP TABLE CUSTOMERS;
The IF EXISTS clause
而不是在删除表之前一直检查它是否存在于数据库中,我们可以将 IF EXISTS 子句和 DROP TABLE 语句一起使用。
Instead of constantly checking whether a table exists or not in a database before deleting it, we can use the IF EXISTS clause along with the DROP TABLE statement.
当我们在 DROP TABLE 查询中指定此子句时,它将自动验证表是否存在于当前数据库中。如果存在,它将删除表。如果表不存在,该查询将被忽略。
When we specify this clause in the DROP TABLE query, it will automatically verify if the table exists in the current database. If it exists, it will then delete the table. If the table doesn’t exist, the query will be ignored.
Syntax
以下是 DROP TABLE IF EXISTS 语句的基本语法 -
Following is the basic syntax of DROP TABLE IF EXISTS statement −
DROP TABLE [IF EXISTS] table_name;
Example
在这里,我们只使用 DROP TABLE 语句删除已经在上个示例中删除的 CUSTOMERS 表。
Here, we are using just DROP TABLE statement to drop the CUSTOMERS table which has been deleted already in the previous example.
DROP TABLE CUSTOMERS;
Output
由于我们没有使用 IF EXISTS 和 DROP TABLE 命令,它将显示如下错误 -
Since, we are not using IF EXISTS with the DROP TABLE command, it will display an error as follows −
ERROR 1051 (42S02): Unknown table 'tutorials.customers'
Example
如果我们尝试删除数据库中不存在的 CUSTOMERS,使用 IF EXISTS 子句时,该查询将被忽略而不发出任何错误 -
If we try to drop CUSTOMERS that does not exist in the database, using the IF EXISTS clause, the query will be ignored without issuing any error −
DROP TABLE IF EXISTS CUSTOMERS;
执行上述查询将产生以下输出:
Executing the query above will produce the following output −
Query OK, 0 rows affected, 1 warning (0.01 sec)
Dropping Table Using a Client Program
除了使用 MySQL 查询从 MySQL 数据库中删除表以外,还可以使用客户端程序对表执行 DROP TABLE 操作。
In addition to dropping a table from MySQL Database using the MySQL query, we can also perform the DROP TABLE operation on a table using a client program.