← Back to Chapters

MySQL DROP & TRUNCATE Table

?️ MySQL DROP & TRUNCATE Table

? Quick Overview

The DROP and TRUNCATE commands in MySQL are both used to delete data, but they work differently and have different implications. The DROP command completely removes a table from the database, while the TRUNCATE command deletes all rows from a table but keeps the table structure intact for future use.

? Key Concepts

  • DROP removes the entire table definition and data.
  • TRUNCATE removes only the table data.
  • Both operations are irreversible without backups.

? Syntax / Theory

Understanding the syntax helps you choose the correct command based on whether you want to remove only data or the entire table structure.

? Code Examples

1️⃣ DROP Table

? View Code Example
-- This command permanently removes the table and its structure
DROP TABLE employees;

2️⃣ TRUNCATE Table

? View Code Example
-- This command deletes all rows but keeps the table structure
TRUNCATE TABLE employees;

? Live Output / Explanation

DROP results in the table being completely removed from the database.

TRUNCATE results in an empty table ready for new data.

? Interactive Explanation

Imagine a whiteboard:

  • TRUNCATE = Erase all writing.
  • DROP = Throw away the whiteboard.

? Use Cases

  • Use TRUNCATE to reset test data quickly.
  • Use DROP when a table is no longer required.

✅ Tips & Best Practices

  • Always double-check before running DROP.
  • Prefer TRUNCATE for performance when clearing data.

? Try It Yourself

  • Create a sample table and insert rows.
  • Apply TRUNCATE and observe the result.
  • Recreate the table and try DROP.