SQL Truncate Table Example

In this article, we are going to explain the SQL Truncate Table through examples.

While using tables in databases, you may need to delete the data of the entire table. In this article, we will study the SQL Truncate Table command, and use it with an example. We will also understand the difference between the SQL Drop Table command and the SQL Truncate Table command.

1. Pre-requisities

If you want to implement the code used in this example, you can do it in two ways.

  1. Download and install MySQL in your local machine.
  2. You can use any online SQL compilers.

2. About SQL Truncate Table command

The SQL Truncate Table the command deletes the data of the table. But, it does not delete the structure of the table. Note that the Truncate Table command is different than the SQL Drop Table command. The SQL Drop Table command deletes both the data and the structure of the table.

SQL Truncate Table is same as SQL Delete command except there is no Where clause with the former.

3. SQL Truncate Table Syntax

The basic syntax of the SQL Truncate Table command is given below.


4. Example

Let us now use the SQL Truncate Table Command.

  1. Let us first create a database – demo, and then create a table – example. The example table has two columns – id (of the type int), and name (of the type varchar).
SQL Truncate Table - Creating Database
Creating Database – demo and Table – example

2. Now, use the SQL Truncate Table command, and run the SQL Select Command to find the ‘Empty set’, which means that we have successfully deleted the data of the table.

SQL Truncate Table command
Using Truncate Table command

5. Summary

In this article, we studied the SQL Truncate Table command. We also explored the command using the example. This article also explained the primary difference between the Drop table command and the Truncate Table command.

6. Download the source code

It is suggested to download the source code and implement it at your own end.

You can download the full source code of this example here: SQL Truncate Table Example

Simran Koul

Simran has graduated as a Bachelor of Engineering in Computer Science from Chitkara University. She has undergone a 6-months long comprehensive industrial training at the reputed Centre for Development of Advanced Computing (C-DAC), where she worked on a project including the likes of Java, JSP, Servlets while the UI-UX through the pioneering HTML, CSS and JS. Her inquisitive nature and the seed of curiosity keeps her on the toes to find material to write about. Along with her interests in Software Development, she is an ardent reader and always ready-to-write writer.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button