SQLite TRUNCATE TABLE Keyword
The SQLite TRUNCATE TABLE keyword is used to delete complete data from an existing table.
SQLite does not have an explicit TRUNCATE TABLE command like other databases. Instead, it has added a TRUNCATE optimizer to the DELETE statement. To truncate a table in SQLite, DELETE statement without a WHERE clause can be used. This will empty the table but the table structure will be retained.
The SQLite DROP TABLE keyword can also be used to delete complete data of a table but it will delete whole table structure from the database.
Syntax
The syntax of using DELETE FROM keyword is given below:
DELETE FROM table_name;
Example:
Consider a database containing a table called Employee with the following records:
EmpID | Name | City | Age | Salary |
---|---|---|---|---|
1 | John | London | 25 | 3000 |
2 | Marry | New York | 24 | 2750 |
3 | Jo | Paris | 27 | 2800 |
4 | Kim | Amsterdam | 30 | 3100 |
5 | Ramesh | New Delhi | 28 | 3000 |
6 | Huang | Beijing | 28 | 2800 |
The description of the table can be checked using pragma table_info statement as shown below:
pragma table_info('Employee');
This result of the above code will be:
cid | name | type | notnull | dflt_value | pk |
---|---|---|---|---|---|
0 | EmpID | INT | 1 | 1 | |
1 | Name | VARCHAR(255) | 1 | 0 | |
2 | City | VARCHAR(100) | 0 | 0 | |
3 | Age | INT | 0 | 0 | |
4 | Salary | DECIMAL(18,2) | 0 | 0 |
To truncate this table, the statement is given below:
DELETE FROM Employee;
After truncating the table, the pragma table_info statement will still show the same structure as shown above but the table will contain no records.
❮ SQLite Keywords