Skip to content

Commit 43e2430

Browse files
[Term Entry] SQL Commands: TRUNCATE
* Add SQL truncate concept entry * Fix file URL * Minor changes ---------
1 parent 0539167 commit 43e2430

File tree

1 file changed

+54
-0
lines changed
  • content/sql/concepts/commands/terms/truncate

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
Title: 'TRUNCATE'
3+
Description: 'Deletes all rows from a table while retaining the table structure.'
4+
Subjects:
5+
- 'Data Science'
6+
- 'Data Visualization'
7+
Tags:
8+
- 'Data'
9+
- 'Database'
10+
- 'Functions'
11+
- 'SQL'
12+
CatalogContent:
13+
- 'learn-sql'
14+
- 'paths/analyze-data-with-sql'
15+
---
16+
17+
**`TRUNCATE`** is a SQL statement used to quickly delete all rows from a table while retaining the table structure. Unlike the **`DELETE`** statement, `TRUNCATE` does not log individual row deletions and does not generate triggers, making it faster for large datasets.
18+
19+
## Key Characteristics
20+
21+
- _Performance_: `TRUNCATE` is more efficient than `DELETE` because it uses minimal logging.
22+
- _Data Integrity_: It resets identity columns (e.g., `AUTO_INCREMENT`) to their seed values.
23+
- _Irreversibility_: `TRUNCATE` operations cannot be rolled back in most database systems, as it does not log row-level changes.
24+
- _Constraints_: Cannot truncate a table that is referenced by a foreign key.
25+
26+
## Comparison: TRUNCATE vs. DELETE
27+
28+
| Feature | `TRUNCATE` | `DELETE` |
29+
| --------------------- | ----------------------------- | ------------------------- |
30+
| Row Logging | No | Yes |
31+
| Triggers | Not activated | Activated |
32+
| Rollback | Not supported in many systems | Supported |
33+
| Identity Column Reset | Yes | No |
34+
| Performance | Faster | Slower for large datasets |
35+
36+
## Syntax
37+
38+
```pseudo
39+
TRUNCATE TABLE table_name;
40+
```
41+
42+
- `table_name`: Specifies the name of the table to truncate.
43+
44+
> **Note:** Use `TRUNCATE` carefully in production environments, as it cannot be undone.
45+
46+
## Example
47+
48+
The `TRUNCATE` statement can be used to clear all records from a table while retaining its structure. For instance, to remove all data from a table named `employee_data`:
49+
50+
```sql
51+
TRUNCATE TABLE employee_data;
52+
```
53+
54+
This removes all rows from the `employee_data` table without affecting the table's schema or structure.

0 commit comments

Comments
 (0)