MySQL truncate table語句


• Truncate table語句用來刪除/截斷表里的所有數據
• 和delete刪除所有表數據在邏輯上含義相同,但性能更快
• 類似執行了drop table和create table兩個語句

mysql> select * from students_bak;
+-----+----------+--------+---------+
| sid | sname    | gender | dept_id |
+-----+----------+--------+---------+
| 101 | zhangsan | male   |      10 |
|   1 | aa       | 1      |       1 |
+-----+----------+--------+---------+
2 rows in set (0.00 sec)

mysql> truncate table students_bak;
Query OK, 0 rows affected (0.16 sec)

mysql> select * from students_bak;
Empty set (0.00 sec)

mysql> set autocommit=off;
Query OK, 0 rows affected (0.01 sec)

mysql> select * from students3;
+-----+-------+--------+---------+--------+
| sid | sname | gender | dept_id | sname2 |
+-----+-------+--------+---------+--------+
| 100 | NULL  | 1      |       1 | NULL   |
+-----+-------+--------+---------+--------+
1 row in set (0.01 sec)

mysql> truncate table students3;
Query OK, 0 rows affected (0.06 sec)

mysql> rollback;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from students3;
Empty set (0.00 sec)

mysql> delete from students;
Query OK, 5 rows affected (0.00 sec)

mysql> select * from students;
Empty set (0.00 sec)

mysql> rollback;
Query OK, 0 rows affected (0.07 sec)

mysql> select * from students;
+-----+-------+--------+---------+
| sid | sname | gender | dept_id |
+-----+-------+--------+---------+
|   1 | aa    | 3      |       1 |
|   4 | cc    | 3      |       1 |
|   5 | dd    | 1      |       2 |
|   6 | aac   | 1      |       1 |
|  10 | a     | 1      |       1 |
+-----+-------+--------+---------+
5 rows in set (0.00 sec)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM