Home » Core Java » sql » SQL Truncate Table Example

About 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.

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.

TRUNCATE TABLE tableName;

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.

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

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments