SQLite 教程
1. SQLite 教程 2. SQLite 简介 3. SQLite 安装 4. SQLite 命令 5. SQLite 语法 6. SQLite 数据类型 7. SQLite 创建数据库 8. SQLite 附加数据库 9. SQLite 分离数据库 10. SQLite 创建表 11. SQLite 删除表 12. SQLite Insert 语句 13. SQLite Select 语句 14. SQLite 运算符 15. SQLite 表达式 16. SQLite Where 子句 17. SQLite AND/OR 运算符 18. SQLite Update 语句 19. SQLite Delete 语句 20. SQLite Like 子句 21. SQLite Glob 子句 22. SQLite Limit 子句 23. SQLite Order By 24. SQLite Group By 25. SQLite Having 子句 26. SQLite Distinct 关键字 27. SQLite PRAGMA 28. SQLite 约束 29. SQLite Join 30. SQLite Unions 子句 31. SQLite NULL 值 32. SQLite 别名 33. SQLite 触发器 34. SQLite 索引 35. SQLite Indexed By 36. SQLite Alter 命令 37. SQLite Truncate Table 38. SQLite 视图 39. SQLite 事务 40. SQLite 子查询 41. SQLite Autoincrement 42. SQLite 注入 43. SQLite Explain 44. SQLite Vacuum 45. SQLite 日期 & 时间 46. SQLite 常用函数 47. SQLite – C/C++ 48. SQLite – Java 49. SQLite – PHP 50. SQLite – Perl 51. SQLite – Python

SQLite Truncate Table

SQLite Truncate Table

在 SQLite 中,并没有 TRUNCATE TABLE 命令,但可以使用 SQLite 的 DELETE 命令从已有的表中删除全部的数据。

语法

DELETE 命令的基本语法如下:


sqlite> DELETE FROM table_name;

但这种方法无法将递增数归零。

如果要将递增数归零,可以使用以下方法:


sqlite> DELETE FROM sqlite_sequence WHERE name = 'table_name';

当 SQLite 数据库中包含自增列时,会自动建立一个名为 sqlite_sequence 的表。这个表包含两个列:name 和 seq。name 记录自增列所在的表,seq 记录当前序号(下一条记录的编号就是当前序号加 1)。如果想把某个自增列的序号归零,只需要修改 sqlite_sequence 表就可以了。

UPDATE sqlite_sequence SET seq = 0 WHERE name = 'table_name';

实例

假设 COMPANY 表有如下记录:


ID          NAME        AGE         ADDRESS     SALARY

----------  ----------  ----------  ----------  ----------

1           Paul        32          California  20000.0

2           Allen       25          Texas       15000.0

3           Teddy       23          Norway      20000.0

4           Mark        25          Rich-Mond   65000.0

5           David       27          Texas       85000.0

6           Kim         22          South-Hall  45000.0

7           James       24          Houston     10000.0

下面为删除上表记录的实例:


SQLite> DELETE FROM sqlite_sequence WHERE name = 'COMPANY';

SQLite> VACUUM;

现在,COMPANY 表中的记录完全被删除,使用 SELECT 语句将没有任何输出。