EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

SQL TRUNCATE()

Home » Data Science » Data Science Tutorials » SQL Tutorial » SQL TRUNCATE()

SQL TRUNCATE()

Introduction to SQL TRUNCATE()

TRUNCATE in standard query language (SQL) is a data definition language (DDL) statement that is used to delete complete data from a database table without deleting the table itself. It frees up space or empties space in the table. However, we should note that TRUNCATE TABLE statements might not be roll backable in many SQL databases. Also being a DDL statement, TRUNCATE table statement does not require commit at each step, it automatically fires commit at the end of execution of the statement. Hence, we should be careful while using it.

We often get confused between TRUNCATE TABLE, DELETE TABLE and DROP TABLE statements in SQL. So before going ahead, let us first check and understand the difference between the three of them.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Differences between SQL TRUNCATE TABLE, SQL DELETE TABLE statement and SQL DROP TABLE statement 

DROP TABLE TRUNCATE TABLE DELETE TABLE
The SQL statement that removes all the data in the table and destroys the table itself. The SQL statement that removes data rows from the table. The SQL statement that removes data rows from the table.
It is a Data Definition Language(DDL) statement. It is a Data Definition Language(DDL) statement. It is a Data Manipulation Language(DML) statement.
We cannot use a WHERE condition. We cannot use a WHERE condition. We can use a WHERE condition.
We cannot perform a rollback operation to undo a DROP table statement. We cannot perform a rollback operation to undo a TRUNCATE table statement. We can perform a rollback operation to undo a DELETE table statement.
Table and all its relationships, index, privileges, etc are deleted permanently. Table rows are deleted permanently. Table rows are not deleted permanently.

Syntax and Parameters

The basic syntax for using a SQL TRUNCATE TABLE statement is as follows :

TRUNCATE TABLE table_name;

The parameters used in the above syntax are :

Table_name: It is the name of the table whose records or rows you want to delete permanently.

How does the TRUNCATE TABLE statement work in SQL?

TRUNCATE TABLE statement in SQL, works by zeroing out a file in the database, i.e after running a TRUNCATE statement on an existing table, the table becomes empty and hence does not hold any row records. It resets the table to zero entries.

However, after truncating the table, its structure, columns, indexes, constraints, relationships, views, etc are preserved. The entire operation is like erasing data from the table but keeping the table intact.

TRUNCATE in Data Definition Language (DDL) is equivalent to DELETE in Data Manipulation Language (DML). The only difference between the two is that the latter can be rolled back, but the first one cannot be. However, TRUNCATE is faster than DELETE because it usually bypasses the transaction system. It is not logged (it can vary across SQL databases) and does not follow predicates and hence seems to be faster than DELETE operation. DELETE is a safer and slower operation.

Examples of SQL TRUNCATE()

Here are a few examples to explain the TRUNCATE TABLE statement in great detail.

Example #1

Simple SQL query to illustrate the function of the TRUNCATE TABLE statement.

To understand the SQL TRUNCATE TABLE, let us consider a “customers” table. The data in the table looks like this.

SELECT * FROM public.customers

SQL TRUNCATE() output 1

Next, let us run the TRUNCATE TABLE statement on the customer’s table to remove all its records. We can do so using the following SQL query.

TRUNCATE TABLE customers;

SQL TRUNCATE() output 2

We can see in the figure below that the TRUNCATE TABLE statement has removed all the records in the customer’s table. However, all the columns, relationships, indexes, table structures have been kept safely.

SELECT * FROM customers;

SQL TRUNCATE() output 3

Example #2

SQL query to illustrate the difference between SQL DROP TABLE and TRUNCATE TABLE statements.

For this let us consider two tables “customer_details” and “students”. The table structure and the data in them look something like this. Records in the “Customer_details” table are as follows:

SELECT * FROM public.customers_details

SQL TRUNCATE() output 4

Records in the “Students” table are as follows:

SELECT * FROM public.students

output 5

Next, we will be running the TRUNCATE TABLE on the customer_details table and DROP TABLE on the student’s table and then we will check the difference.

TRUNCATE TABLE customer_details;

output 6

DROP TABLE students;

output 7

We can observe from the images above that the DROP TABLE statement is faster than the TRUNCATE TABLE statement in SQL.

Now let us check what happened to the two tables after truncating and dropping respectively.

SELECT * FROM customer_details;

output 8

SELECT * FROM students;

output 9

From the above two images, we can observe that in the TRUNCATE statement the table structure is preserved, only the data/records in the table have been removed. Whereas in the case of the DROP TABLE statement, the entire table has been removed from the database. All its columns, indexes, relationships, privileges, etc. are removed.

Conclusion

TRUNCATE TABLE in SQL is a Data Definition Language (DDL) statement that empties an existing table by removing all the records while preserving table columns, privileges, indexes, views, constraints, relationships, etc. It is equivalent to but faster than the DELETE statement in SQL. However, unlike DELETE it cannot be rolled back.

Recommended Articles

This is a guide to SQL TRUNCATE(). Here we discuss the examples to explain the TRUNCATE TABLE statement in great detail along with the syntax and parameters. You can also go through our other suggested articles to learn more –

  1. SQL SELECT RANDOM
  2. ROLLUP in SQL
  3. JSON in SQL
  4. CAST in PostgreSQL

SQL Training Program (7 Courses, 8+ Projects)

7 Online Courses

8 Hands-on Projects

73+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

1 Shares
Share
Tweet
Share
Primary Sidebar
SQL Tutorial
  • Functions
    • SQL Date Function
    • SQL String Functions
    • SQL Compare String
    • SQL Window Functions
    • SQL Timestamp
    • SQL TO_DATE()
    • SQL DATEADD()
    • ANY in SQL
    • LIKE Query in SQL
    • BETWEEN in SQL
    • LTRIM() in SQL
    • TOP in SQL
    • SQL Select Top
    • Merge SQL
    • SQL TRUNCATE()
    • SQL UNION
    • SQL ALL
    • SQL INTERSECT
    • SQL Alias
    • SQL Server Substring
    • CUBE in SQL
    • SQL RANK()
    • SQL CTE
    • SQL LAG()
    • SQL MID
    • SQL avg()
    • SQL WEEK
    • SQL DELETE
    • SQL DATEPART()
    • SQL DECODE()
    • SQL DENSE_RANK()
    • SQL NTILE()
    • SQL NULLIF()
    • SQL Stuff
    • SQL Ceiling
    • SQL EXISTS
    • SQL LEAD()
    • SQL ROW_NUMBER
    • SQL Server Replace
    • T-SQL INSERT
    • SQL Ranking Function
  • Basic
    • What is SQL
    • Careers in SQL
    • Careers in SQL Server
    • IS SQL Microsoft?
    • SQL Management Tools
    • What is SQL Developer
    • Uses of SQL
    • How to Install SQL Server
    • What is SQL Server
    • Database in SQL
    • SQL Data Types
    • SQL Keywords
    • Composite Key in SQL
    • SQL Constraints
    • Transactions in SQL
    • First Normal Form
    • SQL Server Data Types
    • SQL Administration
    • SQL Variables
    • Cheat sheet SQL
  • Operators
    • SQL Operators
    • SQL Arithmetic Operators
    • SQL Logical Operators
    • SQL String Operators
    • Ternary Operator in SQL
  • Commands
    • SQL Commands
    • SQL Alter Command
    • SQL Commands Update
    • SQL DML Commands
    • FETCH in SQL
  • Clause
    • SQL Clauses
    • SQL IN Operator
    • SQL LIKE Clause
    • SQL NOT Operator
    • SQL Minus
    • SQL WHERE Clause
    • SQL with Clause
    • SQL HAVING Clause
    • GROUP BY clause in SQL
    • ORDER BY Clause in SQL
    • SQL ORDER BY CASE
    • SQL ORDER BY DATE
    • SQL ORDER BY Alphabetical
    • SQL ORDER BY Ascending
    • SQL GROUP BY Month
    • SQL GROUP BY Multiple Columns
    • SQL GROUP BY DAY
    • SQL GROUPING SETS
  • Queries
    • SQL Insert Query
    • SQL SELECT Query
    • SQL SELECT RANDOM
    • SQL Except Select
    • SQL Subquery
    • SQL SELECT DISTINCT
    • SQL WITH AS Statement
  • Keys
    • SQL Keys
    • Primary Key in SQL
    • Foreign Key in SQL
    • Unique Key in SQL
    • Alternate Key in SQL
  • Joins
    • Join Query in SQL
    • Types of Joins in SQL
    • Types of Joins in SQL Server
    • SQL Inner Join
    • SQL Join Two Tables
    • SQL Delete Join
    • SQL Left Join
    • SQL Right Join
    • SQL Cross Join
    • SQL Outer Join
    • SQL Full Join
    • SQL Self Join
    • Natural Join SQL
    • SQL Multiple Join
  • Advanced
    • Aggregate Functions in SQL
    • IF ELSE Statement in SQL
    • SQL CASE Statement
    • SQL While Loop
    • SQL INSTR()
    • What is Procedure in SQL
    • Stored Procedure in SQL?
    • SQL Server Constraints
    • SQL DELETE ROW
    • Column in SQL
    • Table in SQL
    • SQL Virtual Table
    • SQL Merge Two Tables
    • SQL Table Partitioning
    • SQL Temporary Table
    • SQL Clone Table
    • SQL Rename Table
    • SQL LOCK TABLE
    • SQL Mapping
    • Cursors in SQL
    • AND in SQL
    • Wildcard in SQL
    • SQL FETCH NEXT
    • SQL Views
    • Triggers in SQL
    • SQL UPDATE Trigger
    • SQL AFTER UPDATE Trigger
    • SQL Update Statement
    • SQL DROP TRIGGER
    • Views in MySQL
    • SQL COMMIT
    • Distinct Keyword in SQL
    • PARTITION BY in SQL
    • SQL Set Operators
    • SQL UNION ALL
    • Metadata in SQL
    • SQL Bulk Insert
    • Array in SQL
    • SQL REGEXP
    • JSON in SQL
    • SQL For loop
    • EXPLAIN in SQL
    • SQL Cluster
    • SQL Backup
    • SQL Pattern Matching
    • SQL Users
    • ISNULL SQL Server
    • SQL Import CSV
  • Interview Questions
    • SQL Interview Questions
    • Advance SQL Interview Questions
    • SQL Joins Interview Questions
    • SQL Server Interview Questions

Related Courses

JDBC Training Course

PHP course

Windows 10 Training

SQL Course Training

PL/SQL Certification Courses

Oracle Certification Courses

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - SQL Training Program (7 Courses, 8+ Projects) Learn More