MySQL TRUNCATE和DROP命令之间最显着的区别是TRUNCATE命令不会破坏表的结构,但是相反,DROP命令也会破坏表的结构。
mysql> Create table testing(id int PRIMARY KEY NOT NULL AUTO_INCREMENT,Name Varchar(20)); mysql> Insert into testing(Name) Values('Ram'),('Mohan'),('John'); 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)
现在,在按如下所示对“测试”表进行TRUNCATING之后,其结构仍保留在数据库中,并且还初始化了PRIMARY KEY。
mysql> Truncate table testing; 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; mysql> DESCRIBE testing; ERROR 1146 (42S02): Table 'query.testing' doesn't exist