SQLite clears the table and zeros the self-increment column

Source: Internet
Author: User
Tags sqlite sqlite database

The SQL Standard has a TRUNCATE TABLE statement that clears all the contents of the table. However, SQLite does not support this statement. You can use "DELETE from TableName" directly in SQLite. For most DBMS, using delete is not as fast as truncate, because TRUNCATE does not have to access the entire table without logging data changes.
Although SQLite does not support truncate, it is optimized for DELETE: "When the WHERE are omitted from a DELETE statement and the table being deleted have no TR Iggers, SQLite uses an optimization to erase the entire table content without have to visit each row of the table Indivi Dually. This "truncate" optimization makes the delete run much faster. "
Usually, when you clear the table, you also need to reset the self-increment column to zero. Here's how to define a self-increment column in SQLite:
CREATE TABLE TableName (id INTEGER PRIMARY KEY autoincrement, ...);
A table named Sqlite_sequence is automatically created when the SQLite database contains a self-increment column. This table consists of two columns: name and seq. The name records the table where the increment column is located, and the SEQ record is the current ordinal (the number of the next record is the current ordinal plus 1). If you want to set the ordinal of a self-increment column to zero, you only need to modify the Sqlite_sequence table.
UPDATE sqlite_sequence SET seq = 0 WHERE name = ' TableName ';

You can also delete the record directly:
DELETE from sqlite_sequence WHERE name = ' TableName ';

To reset all the table's self-increment columns to zero, empty the Sqlite_sequence table directly:

DELETE from Sqlite_sequence;


Delete a table using the drop statement and recreate the table, and the table's primary key is regenerated.

DROP TABLE TableName



Welcome to scan QR Code, follow the public number


SQLite clears the table and zeros the self-increment column

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.