如何在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)
关于如何在MySQL中使用truncate table语句问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。