Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Concept Entry] TRUCATE in SQL #5782

Merged
merged 4 commits into from
Dec 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions content/sql/concepts/commands/terms/truncate/truncate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
Title: 'TRUNCATE'
Description: 'Deletes all rows from a table while retaining the table structure.'
Subjects:
- 'Data Science'
- 'Data Visualization'
Tags:
- 'Data'
- 'Database'
- 'Functions'
- 'SQL'
CatalogContent:
- 'learn-sql'
- 'paths/analyze-data-with-sql'
---

**`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.

## Key Characteristics

- _Performance_: `TRUNCATE` is more efficient than `DELETE` because it uses minimal logging.
- _Data Integrity_: It resets identity columns (e.g., `AUTO_INCREMENT`) to their seed values.
- _Irreversibility_: `TRUNCATE` operations cannot be rolled back in most database systems, as it does not log row-level changes.
- _Constraints_: Cannot truncate a table that is referenced by a foreign key.

## Comparison: TRUNCATE vs. DELETE

| Feature | `TRUNCATE` | `DELETE` |
| --------------------- | ----------------------------- | ------------------------- |
| Row Logging | No | Yes |
| Triggers | Not activated | Activated |
| Rollback | Not supported in many systems | Supported |
| Identity Column Reset | Yes | No |
| Performance | Faster | Slower for large datasets |

## Syntax

```pseudo
TRUNCATE TABLE table_name;
```

- `table_name`: Specifies the name of the table to truncate.

> **Note:** Use `TRUNCATE` carefully in production environments, as it cannot be undone.

## Example

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`:

```sql
TRUNCATE TABLE employee_data;
```

This removes all rows from the `employee_data` table without affecting the table's schema or structure.
Loading