← Back to MySQL Course | Chapter 7: Updating & Deleting | Lesson 3 of 4

DELETE Statement

DELETE अपने chart से rows को एक eraser से मिटाने जैसा है जो कोई marks नहीं छोड़ता। एक बार वे गायब हो गईं, वे गायब हो गईं, इसलिए carefully aim करें।
Syntax
sql
DELETE FROM table_name
WHERE condition;

DELETE का Introduction

DELETE statement किसी table से एक या ज़्यादा rows हटाता है, उस data को permanently free करते हुए। यह एक permanent action है, इसलिए caution से इस्तेमाल करें — कोई built-in undo नहीं है। production data पर एक delete operation चलाने से पहले हमेशा अपना database backup लें।

उदाहरण: Introduction to DELETE

sql
DELETE FROM users WHERE id = 1;

Missing WHERE Clauses का Danger

अगर आप बिना WHERE clause के एक DELETE statement चलाएँ, table की सारी rows delete हो जाएँगी। table structure खुद बचा रहेगा, लेकिन यह पूरी तरह empty होगा, DROP TABLE के उलट जो structure भी हटा देता है। execute दबाने से पहले हमेशा अपनी queries triple-check करें।

उदाहरण: The Danger of Missing WHERE Clauses

sql
DELETE FROM users; -- deletes ALL rows, table structure remains

TRUNCATE बनाम DELETE

किसी table से सभी rows हटाने के लिए TRUNCATE DELETE से कहीं ज़्यादा तेज़ है, क्योंकि यह हर row deletion को individually log नहीं करता। TRUNCATE table को reset कर देता है (AUTO_INCREMENT सहित) और row-by-row logged नहीं है। अगर आपको WHERE से rows filter करनी हों तो इसके बजाय DELETE इस्तेमाल करें।

उदाहरण: TRUNCATE versus DELETE

sql
TRUNCATE TABLE users; -- faster, resets AUTO_INCREMENT
DELETE FROM users; -- slower, logs each row

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

LIMIT और ORDER BY के साथ Delete करना

आप LIMIT clause इस्तेमाल करके delete की गई rows की संख्या limit कर सकते हैं, धीरे-धीरे पुराने records हटाने के लिए उपयोगी। आप पहले ORDER BY से rows sort भी कर सकते हैं। यह आपको पुराने data के specific subsets safely delete करने देता है, जैसे सबसे पुरानी 100 log entries।

उदाहरण: Deleting with LIMIT and ORDER BY

sql
DELETE FROM logs ORDER BY created_at ASC LIMIT 100;

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

Safe Deletion Practices

अपनी DELETE query को पहले एक SELECT query की तरह test करना एक अच्छी practice है, DELETE FROM को same WHERE के साथ SELECT * FROM से swap करके। यह आपको commit करने से पहले exactly यह preview करने देता है कि कौन सी rows हटेंगी। एक बार sure हो जाने पर, इसे safely एक DELETE statement में convert करें।

उदाहरण: Safe Deletion Practices

sql
SELECT * FROM users WHERE status = 'banned'; -- preview first
DELETE FROM users WHERE status = 'banned';
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 के DELETE FROM users; चलाना, जो हर row हटा देता है।
  2. यह मान लेना कि DELETE table free कर देता है और AUTO_INCREMENT reset कर देता है, जबकि यह ऐसा नहीं करता।
  3. एक parent row delete करने की कोशिश करना जिसे एक foreign key reference करती है, जो fail हो जाता है।
🔒

Chapter Quiz — Complete all 4 topics to unlock

0/4 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.