Junior
What is the TRUNCATE command?
sobes.tech AI
Answer from AI
This is a SQL command used for quickly removing all rows from a table. It frees the space occupied by the table but preserves the table structure and indexes.
Differences from DELETE:
| Feature | TRUNCATE | DELETE |
|---|---|---|
| Speed of execution | Faster, as it does not scan rows | Slower, scans each row |
| Log usage | Minimal transaction logging | Full transaction logging |
| Rollback capability | Usually not supported by DBMS for rollback | Supports rollback via ROLLBACK |
| SUPPORTS WHERE | Does not support | Supports |
| ID counter reset | Often resets auto-increment counters | Does not reset |
Example usage:
-- Delete all data from the "users" table without rollback capability
TRUNCATE TABLE users;