MySQL TRUNCATE 与 DROP 命令之间的重要区别是什么?
MySQL TRUNCATE 命令与 DROP 命令最主要的区别在于,TRUNCATE 命令不会破坏表结构,而 DROP 命令则会破坏表结构。
示例
mysql> Create table testing(id int PRIMARY KEY NOT NULL AUTO_INCREMENT,Name Varchar(20)); Query OK, 0 rows affected (0.24 sec) mysql> Insert into testing(Name) Values('Ram'),('Mohan'),('John'); Query OK, 3 rows affected (0.12 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> Select * from testing; +----+-------+ | id | Name | +----+-------+ | 1 | Ram | | 2 | Mohan | | 3 | John | +----+-------+ 3 rows in set (0.00 sec)
现在,truncate 'testing' 表如下,尽管表结构仍保留在数据库中,但主索引也会被初始化。
mysql> Truncate table testing; Query OK, 0 rows affected (0.04 sec) mysql> DESCRIBE testing; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | Name | varchar(20) | YES | | NULL | | +-------+-------------+------+-----+---------+----------------+ 2 rows in set (0.21 sec)
但当我们对表应用 DROP 命令时,表结构也会从数据库中删除。
mysql> Drop table testing; Query OK, 0 rows affected (0.08 sec) mysql> DESCRIBE testing; ERROR 1146 (42S02): Table 'query.testing' doesn't exist
广告