SQLite

How to use TRUNCATE TABLE statement in SQLite

Truncate means to eliminate something; in relational databases like MySQL, the clause TRUNCATE TABLE is used as a command to delete all the rows of the table, without changing the structure of the table. Like MySQL, SQLite is also used to manage the data which is stored in the databases. The data of a website or an application is stored in the form of tables, which consist of rows and columns, to create and modify these tables, different commands are used in SQLite such as CREATE TABLE, INSERT INTO.

How does the Truncate table command work in SQLite

The TRUNCATE TABLE command is not supported by SQLite to delete the rows or to delete the data of the entire table without changing the structure of the table, but we can have this task done in another way which is using the DELETE clause. The DELETE clause will delete all the data from the table but it is slightly different from the TRUNCATE clause, some of the important differences are:

TRUNCATE DELETE
This is used to delete rows from the entire table This is used either to delete a specific row (using WHERE) or all the rows (without using WHERE)
Faster execution Slow execution as compared to TRUNCATE
We can truncate using ALTER permission We can delete using DELETE permission
Cannot be used with indexed views Can be used with indexed views
It is a DDL (Data Definition Language) command It is a DML (Data Manipulation Language) command

Syntax of the DELETE command

The general syntax of the DELETE command is given below which will perform the functionality of TRUNCATE in SQLite:

DELETE FROM TABLE_NAME;

The explanation of this syntax is simple as:

  • Use the DELETE clause so that it can perform the delete action
  • Use the FROM clause to tell from where the delete action is to be performed
  • Replace the table_name, with the table name you want to modify

How to use DELETE command instead of TRUNCATE in SQLite

First, we will display all the tables, available in the database:

.tables

Now we will display the contents of the table, named, Players_data, using the command:

SELECT * FROM Players_data;

Now to delete all the rows, we will use the DELETE clause without using the WHERE clause as:

DELETE FROM Players_data;

We will display the table to confirm whether the table is present or deleted from the database:

.tables

Now, again we will confirm the successful execution of the above command by displaying the entire data of the table using:

SELECT * FROM Players_data;

To confirm whether the columns are present or not, we will display the details of the table:

PRAGMA table_info([Players_data]);

We can see from the above output, the rows of the table have been deleted without deleting the structure of a table, but the size of the table is the same as before with the data of rows because it only erases the data. So to make the space occupied by that data, we will vacuum it by running the following command:

VACUUM;

What is the difference between DELETE and DROP clauses in SQLite

The DELETE clause is used to remove the data of one or multiple rows, but the DROP clause is used to delete the entire table from the database. For example, in the above example, we deleted the Players_data using the DELETE clause, which only removes the rows of the table. Now we will drop the same table, Players_data, using the DROP clause as:

DROP TABLE Players_data;

Now, we will again display the table using:

SELECT * FROM Players_data;

The entire table, Players_data, has been deleted from the database using the DROP TABLE clause.

Conclusion

There are slight differences in SQLite with the other relational databases such as the TRUNCATE TABLE clause is not supported by SQLite, but we can use the DELETE clause for the same purpose. In this article, we learned how to use the DELETE clause to remove the data from a table without removing the table from the database. We also learned the difference between DELETE and DROP clauses.

About the author

Hammad Zahid

I'm an Engineering graduate and my passion for IT has brought me to Linux. Now here I'm learning and sharing my knowledge with the world.