← Back to PostgreSQL Course | Chapter 4: Inserting & Modifying Data | Lesson 6 of 7

TRUNCATE करना

TRUNCATE एक table से सारी rows जल्दी remove करता है, DELETE से कहीं ज़्यादा तेज़।

In this page:

  1. TRUNCATE
Syntax
sql
TRUNCATE TABLE table_name;
TRUNCATE TABLE table_name RESTART IDENTITY;

TRUNCATE

TRUNCATE table एक या ज़्यादा tables को row by row delete करने की बजाय इनकी data files discard करके खाली करता है। RESTART IDENTITY identity या serial counters reset करता है, और CASCADE इसे reference करने वाली tables भी truncate करता है।

PostgreSQL में TRUNCATE transactional है लेकिन एक strong lock लेता है, और कोई row-level delete triggers fire नहीं करता।

Note:
  • TRUNCATE ...
  • RESTART IDENTITY auto-numbering reset करता है।

उदाहरण: TRUNCATE

bash
shop=# SELECT COUNT(*) FROM log_entries;
  count
---------
 2500000
shop=# TRUNCATE TABLE log_entries RESTART IDENTITY;
TRUNCATE TABLE
shop=# SELECT COUNT(*) FROM log_entries;
 count
-------
     0
shop=# BEGIN;
shop=*# TRUNCATE orders CASCADE;
NOTICE:  truncate cascades to table "order_items"
shop=*# ROLLBACK;

⚠️ Run this in your own terminal or Node.js environment.

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. जहाँ आपको एक WHERE clause चाहिए वहाँ TRUNCATE इस्तेमाल करना
  2. foreign keys exist करने पर CASCADE भूल जाना
  3. यह realize न करना कि यह एक exclusive lock लेता है
चैप्टर सारांश
  • TRUNCATE सारी rows जल्दी remove करता है
  • RESTART IDENTITY counters reset करता है
  • CASCADE foreign keys follow करता है
  • यह एक ACCESS EXCLUSIVE lock लेता है
🔒

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.