Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Truncate Table in SQL

Truncate Table in SQL

The TRUNCATE TABLE statement in SQL is used to remove all rows from a table quickly and efficiently. Unlike the DELETE statement, TRUNCATE TABLE deletes data without logging each row deletion, which makes it faster, especially for large tables.


Key Characteristics of TRUNCATE TABLE

  • Deletes all rows from the specified table.

  • Cannot use a WHERE clause — it always removes every row.

  • Resets any identity (auto-increment) counters to their seed value in many database systems.

  • Usually cannot be rolled back once executed (depending on the database and transaction settings).

  • Does not fire DELETE triggers in most database systems.

  • Removes data but does not remove the table structure or its indexes.


Syntax

TRUNCATE TABLE table_name;

Example

TRUNCATE TABLE Customers;

This command will quickly remove all rows from the Customers table.


Differences from DELETE

AspectTRUNCATE TABLEDELETE
Deletes rowsAll rows (no filtering)Can delete specific rows with WHERE
LoggingMinimal loggingLogs each row deletion
TriggersUsually does not fireFires DELETE triggers
RollbackOften cannot be rolled backCan be rolled back
Identity resetResets identity counterDoes not reset identity
SpeedFasterSlower (especially for large tables)

Notes

  • In some databases, TRUNCATE TABLE is a DDL (Data Definition Language) command and may cause an implicit commit.

  • Always use with caution because it removes all data irreversibly in many systems.


If you want, I can provide examples of TRUNCATE TABLE usage for your specific SQL database!

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql