TRUNCATE - potatoscript/sqlite GitHub Wiki
TRUNCATE
?
π¬ What is Think of
TRUNCATE
as a super vacuum cleaner π§Ή
It clears out a whole table in one big whoooosh πͺοΈ
Itβs like saying:
βRemove ALL the toys from the toy box, FAST!β π§Έπͺπͺπ§Ό
TRUNCATE
β Important: SQLite Does NOT Support Wait what?! π±
Thatβs right β SQLite does not use TRUNCATE
like MySQL or PostgreSQL do.
But donβt worry β Iβll still explain it (because you might use MySQL or PostgreSQL someday).
And Iβll show you what to use instead in SQLite π‘
TRUNCATE
do (in other DBs)?
π§ What does TRUNCATE TABLE table_name;
- Deletes ALL rows β
- But does not log each row (faster than DELETE) π
- Auto-resets IDs (like
AUTO_INCREMENT
) to 1 π
β‘ Faster than:
DELETE FROM table_name;
π Example (in MySQL/PostgreSQL)
You have a table called fruits
:
TRUNCATE TABLE fruits;
π₯ Boom! All your fruits are gone. πππ
Table is empty, but still exists (unlike DROP).
π§ͺ What about in SQLite?
π« SQLite doesn't support TRUNCATE
.
Instead, you do:
DELETE FROM table_name;
β
Same result: all rows deleted
β IDs not reset by default
π How to Reset the IDs in SQLite?
Letβs say you have a table with AUTOINCREMENT
.
After deleting everything:
DELETE FROM my_table;
You then reset the sequence like this:
DELETE FROM sqlite_sequence WHERE name = 'my_table';
β¨ That resets the auto-increment counter back to 1!
π‘ Summary Table
Feature | DELETE |
TRUNCATE (Not in SQLite) |
---|---|---|
Deletes all rows | β | β |
Logs each row | β | β |
Faster | β | β |
Resets auto-increment ID | β (need extra step) | β |
Supported in SQLite? | β | β |