Python MySQL Delete Record
In this page:
sql = "DELETE FROM table_name WHERE column = %s"
cursor.execute(sql, (value,))
connection.commit()
एक Specific Row Delete करना
cursor.execute("DELETE FROM customers WHERE address = %s", ("Mountain 21",)) सिर्फ उन row(s) को हटाता है जो दी गई WHERE condition से मेल खाती हैं, table की बाकी हर row को बिना छुए छोड़ देता है।
उदाहरण: Deleting a Specific Row
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute("DELETE FROM customers WHERE address = %s", ("Mountain 21",)) # only matching rows are removed
conn.commit()
print("Matching row(s) deleted")
बिना WHERE Delete करने का खतरा
बिना किसी WHERE clause वाला DELETE FROM customers syntactically valid SQL है जो table की हर एक row delete कर देता है -- DROP TABLE के विपरीत यह empty table structure को बरकरार रखता है, पर सारा data खुद खत्म हो जाता है।
उदाहरण: The Danger of Deleting Without WHERE
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
# DANGER: no WHERE clause -- this deletes every row in the table
sql = "DELETE FROM customers"
print("Always double-check for a WHERE clause before running:", sql)
कई Matching Rows Delete करना
एक broader condition इस्तेमाल करने वाला WHERE clause -- जैसे कोई LIKE pattern, या < जैसा comparison operator -- एक ही DELETE statement में कई rows से मेल खाकर उन्हें delete कर सकता है, ठीक वैसे ही जैसे उसी WHERE वाला SELECT कई rows return करेगा।
उदाहरण: Deleting Multiple Matching Rows
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute("DELETE FROM customers WHERE city = %s", ("Austin",)) # broader condition can match several rows
conn.commit()
print("All matching rows removed in one statement")
Delete पर Foreign Key Constraints को Handle करना
किसी ऐसी row को delete करना जिसे अब भी किसी दूसरे table में एक FOREIGN KEY reference करता है, by default एक integrity error raise करता है -- MySQL orphaned references छोड़ने से बचने के लिए delete को मना कर देता है, जब तक कि foreign key specifically ON DELETE CASCADE के साथ defined न हो।
उदाहरण: Handling Foreign Key Constraints on Delete
from unittest.mock import MagicMock
class Error(Exception):
pass
conn = MagicMock()
cursor = conn.cursor()
cursor.execute.side_effect = Error("Cannot delete: foreign key constraint")
try:
cursor.execute("DELETE FROM customers WHERE id = %s", (1,)) # blocked by a referencing foreign key
except Error as e:
print("Blocked:", e)
Safer Deletes के लिए Transactions इस्तेमाल करना
किसी delete operation को explicit try/except और failure पर conn.rollback() के साथ wrap करना सुनिश्चित करता है कि अगर multi-step delete के बीच में कुछ गलत हो, तो database एक partially-completed state की बजाय अपनी original, consistent state में रहे।
उदाहरण: Using Transactions for Safer Deletes
from unittest.mock import MagicMock
class Error(Exception):
pass
conn = MagicMock()
cursor = conn.cursor()
try:
cursor.execute("DELETE FROM customers WHERE id = %s", (1,))
conn.commit()
except Error:
conn.rollback() # restores the database to its prior consistent state
print("Rolled back on failure")
else:
print("Delete committed safely")
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first:
- Python MySQL Get Started
- Python MySQL Create Database
- Python MySQL Create Table
- Python MySQL Insert Into Table
- Python MySQL Select From Table
- Python MySQL Select With a Filter
- Python MySQL Order By
- Python MySQL Delete Record
- Python MySQL Drop Table
- Python MySQL Update Table
- Python MySQL Limit
- Python MySQL Join