Mysql 简明教程
MySQL - Truncate Table
MySQL TRUNCATE TABLE Statement
MySQL TRUNCATE TABLE 语句仅用于删除现有表的 data,但不删除表。
The MySQL TRUNCATE TABLE statement is used to delete only the data of an existing table, but not the table.
此命令有助于一次完全 TRUNCATE 表,而不是逐个删除表记录,这是一个非常耗时且繁重的过程。
This command helps to TRUNCATE a table completely in one go instead of deleting table records one by one which will be very time consuming and hefty process.
Syntax
以下是 TRUNCATE TABLE 语句的基本语法 −
Following is the basic syntax of the TRUNCATE TABLE statement −
TRUNCATE TABLE table_name
其中,table_name 是您需要从中删除所有记录的表的名称。
Where, table_name is the name of the table you need to delete all the records from.
Example
首先,让我们使用以下查询创建一个名为 CUSTOMERS 的表 −
First of all, let us create a table with name CUSTOMERS using the following query −
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)
);
现在,我们使用以下 INSERT 语句向上面创建的表中插入 7 条记录 −
Now, we are inserting 7 records into the above-created table using the following INSERT statement −
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES
(1, 'Ramesh', 32, 'Ahmedabad', 2000.00 ),
(2, 'Khilan', 25, 'Delhi', 1500.00 ),
(3, 'Kaushik', 23, 'Kota', 2000.00 ),
(4, 'Chaitali', 25, 'Mumbai', 6500.00 ),
(5, 'Hardik', 27, 'Bhopal', 8500.00 ),
(6, 'Komal', 22, 'Hyderabad', 4500.00 ),
(7, 'Muffy', 24, 'Indore', 10000.00 );
使用以下查询,我们正在显示 CUSTOMERS 表的记录 −
Using the following query, we are displaying the records of CUSTOMERS table −
SELECT * FROM CUSTOMERS;
以下是 CUSTOMERS 表的记录:
Following are the records of CUSTOMERS table −
在以下查询中,我们使用 TRUNCATE TABLE 命令删除 CUSTOMERS 表中的所有记录 −
In the following query, we are using the TRUNCATE TABLE command to remove all the records in the CUSTOMERS table −
TRUNCATE TABLE CUSTOMERS;
Output
记录已从 CUSTOMERS 表中截断,没有任何错误。
The records have been truncated from the CUSTOMERS table without any error.
Query OK, 0 rows affected (0.02 sec)
Verification
为了验证记录是否已截断,让我们使用以下查询检索记录 −
To verify whether the records have been truncated, let us retrieve the records using the following query −
SELECT * FROM CUSTOMERS;
正如我们在下面的输出中看到的,CUSTOMERS 表中没有记录。因此,记录已截断。
As we can see the output below, there are no records present in the CUSTOMERS table. Thus, the records have been truncated.
Empty set (0.00 sec)
TRUNCATE vs DELETE
以下是 TRUNCATE 和 DELETE 命令之间的主要区别,即使它们在逻辑上工作方式类似:
Following are some major differences between the TRUNCATE and DELETE commands, even though they work similar logically:
TRUNCATE vs DROP
TRUNCATE 和 DROP 是两个不同的命令。TRUNCATE 仅删除表的记录,而 DROP 命令则从数据库中完全删除表。
The TRUNCATE and DROP are two different commands. TRUNCATE just deletes the table’s records, whereas DROP command deletes the table entirely from the database.
但是,这些命令之间仍然存在一些差异,汇总在以下表中 −
However, there are still some differences between these commands, which are summarized in the following table −
Truncating Table Using a Client Program
除了使用 MySQL 查询截断 MySQL 数据库中的表之外,我们还可以使用客户端程序执行 TRUNCATE TABLE 操作。
Besides truncating a table in a MySQL database with a MySQL query, we can also use a client program to perform the TRUNCATE TABLE operation.