If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
तो अभी तक आपने किसी table में data को अलग - अलग तरीके से Insert , Update और Select करना सीखा , इस topic में किसी table से records को delete करना सीखेंगे।
Tables से records को delete करने के लिए DELETE FROM statement का use किया जाता है। need के according आप condition के bases पर किसी particular या multiple records को भी एक साथ delete कर सकते हैं।
DELETE FROM table_name;यहां table_name को उस table से replace कर दीजिये जिसमे से आप records को delete करना चाहते हैं।
Suppose, हमारे पास users name की एक table है , जिसमे कुछ इस तरह से records available हैं।
+----+-----------+ | id | full_name | +----+-----------+ | 1 | Ravi | | 2 | Raju | | 3 | John | | 4 | Chris | | 5 | Tony | +----+-----------+
मुझे वो records delete करना है जिसकी id=1 है। इसलिए query में WHERE Clause का use होगा -
DELETE FROM students WHERE id=1;Output
Query OK, 1 row affected (0.042 sec)
Query OK है और 1 row affect हुई है इसका मतलब है कि 1 record delete हुआ है।
आप अपनी need के according AND , OR या BETWEEN Clause का use भी कर सकते हैं , ये आपकी need पर depend करता है।
ध्यान रहे apply की गयी condition जितने records के लिए match होगी वो सभी records table से delete हो जायेंगे ।
अगर आप एक साथ सभी records को delete करना चाहते हैं तो without WHERE Clause के query run कर देना है।
DELETE FROM students;Query OK, 4 rows affected (0.041 sec)
Output में आप देख सकते हैं कि बचे हुए 4 records एक साथ delete हो गए हैं और students table empty हो चुकी है।
जब आप DELETE statement का use करके किसी table को empty करते हो तो आपकी Auto Increment field की value refresh नहीं होता है , मतलब जो last record होगा वही से Auto Increment field की value start हो जाती है।
जैसे हमने अभी students table को empty किया है अब अगर एक record insert करेंगे तो id field की value 6 से start होगी क्योंकि last record की id 5 थी।
हालाँकि अगर आप चाहते हैं कि table empty होने के बाद Auto Increment field की value refresh हो जाए , जिससे कि जब new record insert होतो 1 से start हो तो आपको TRUNCATE Statement का use करना पड़ेगा।
TRUNCATE TABLE students;अब अगर कोई new record insert होतो Auto Increment field की value 1 से start होगी।