← Back to MySQL Course | Chapter 2: Databases & Tables | Lesson 6 of 7

TRUNCATE TABLE

TRUNCATE TABLE किसी chart से सब कुछ मिटाने जैसा है लेकिन खाली headings रखते हुए, ताकि आप इसे फिर scratch से भरना शुरू कर सकें।
Syntax
sql
TRUNCATE TABLE table_name;

Table Data Clear करना

TRUNCATE TABLE हर row हटाता है साथ ही table की column structure को intact रखते हुए, और यह आमतौर पर DELETE से कहीं तेज़ है क्योंकि यह row-by-row logging और trigger execution को पूरी तरह skip कर देता है।

उदाहरण: Clearing Table Data

sql
TRUNCATE TABLE users;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Auto Increment Values Reset करना

DELETE के उलट, जो AUTO_INCREMENT counter को जहाँ रुका था वहीं छोड़ देता है, TRUNCATE इसे वापस अपनी starting value पर reset कर देता है — यानी अगला inserted row फिर ID 1 पाता है, पुराने sequence की continuation नहीं।

उदाहरण: Resetting Auto Increment Values

sql
CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50));
TRUNCATE TABLE users; -- next inserted row gets id 1 again

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Safe Data Cleanups

क्योंकि TRUNCATE को एक WHERE clause से selectively filter नहीं किया जा सकता, यह हमेशा पूरी table खाली करता है — इसे चलाने से पहले double-check करें कि आप सही database से connected हैं, क्योंकि operation undo नहीं हो सकता।

उदाहरण: Safe Data Cleanups

sql
-- TRUNCATE always empties the entire table; there is no WHERE clause
TRUNCATE TABLE users;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Truncate Operations Compare करना

TRUNCATE technically एक DDL (data definition language) statement है, DML नहीं — internally यह rows को एक-एक करके delete करने के बजाय table structure को drop और recreate करता है, जो इसकी speed explain करता है।

उदाहरण: Comparing Truncate Operations

sql
-- TRUNCATE is DDL: it drops and recreates the table structure, unlike DELETE
TRUNCATE TABLE users;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Post-Truncate Verification

truncate करने के बाद, एक quick SELECT COUNT(*) confirm करता है कि table खाली है और fresh data के लिए ready है, आपको confidence देते हुए कि reset actually expected के अनुसार complete हुआ।

उदाहरण: Post-Truncate Verification

sql
TRUNCATE TABLE users;
SELECT COUNT(*) FROM users;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. TRUNCATE TABLE के साथ एक WHERE clause की उम्मीद करना, जो इसकी अनुमति नहीं देता और हमेशा हर row हटाता है।
  2. यह मान लेना कि TRUNCATE को ROLLBACK से undo किया जा सकता है, जबकि यह DDL है और तुरंत commit हो जाता है।
  3. इसे किसी दूसरी table से एक foreign key द्वारा referenced table पर चलाना, जो fail होता है।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.