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)
現在,對“testing”表執行 TRUNCATE 操作後,表的結構仍保留在資料庫中,並會初始化 PRIMARY KEY。
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
廣告