欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MySQL truncate table语句的使用

程序员文章站 2022-06-27 10:34:45
truncate table语句用来删除/截断表里的所有数据 和delete删除所有表数据在逻辑上含义相同,但性能更快 类似执行了drop table和create table两个语句mysql&...

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)

到此这篇关于mysql truncate table语句的使用的文章就介绍到这了,更多相关mysql truncate table内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!