Python MySQL Drop Table
In this page:
cursor.execute("DROP TABLE table_name")
एक Table Drop करना
cursor.execute("DROP TABLE customers") customers table को -- उसकी column structure और अंदर की हर row data -- database से permanently पूरी तरह हटा देता है; उसके बाद उस table पर चलाई गई कोई भी query fail होती है क्योंकि वह अब मौजूद ही नहीं है।
उदाहरण: Dropping a Table
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute("DROP TABLE customers") # removes the table structure and all its data
print("Table and all its data permanently removed")
IF EXISTS से Errors से बचना
DROP TABLE IF EXISTS tableName, CREATE ... IF NOT EXISTS pattern का safe equivalent है -- अगर table मौजूद है तो यह उसे drop करता है, या अगर मौजूद नहीं है तो (error raise करने की बजाय) कुछ नहीं करता, जो उन teardown scripts में उपयोगी है जिन्हें सुरक्षित रूप से दोबारा चलाया जा सके।
उदाहरण: Avoiding Errors with IF EXISTS
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute("DROP TABLE IF EXISTS customers") # does nothing instead of erroring if it's already gone
print("Safe to run even if the table is already gone")
DROP TABLE बनाम DELETE FROM
DROP TABLE table की पूरी structure और data permanently हटा देता है; DELETE FROM सिर्फ rows हटाता है (optionally WHERE से filtered) जबकि empty table और उसकी column structure पूरी तरह बरकरार रखता है, बाद में नया data insert करने के लिए तैयार।
उदाहरण: DROP TABLE vs DELETE FROM
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute("DELETE FROM customers") # keeps the empty table
cursor.execute("DROP TABLE customers") # removes the table entirely
print("DELETE keeps structure, DROP removes it")
Foreign Key Dependencies को Handle करना
ऐसे table को drop करने की कोशिश करना जिसे बाकी tables अब भी FOREIGN KEY से reference करती हैं by default एक error raise करता है, क्योंकि ऐसा करने से उन दूसरे tables की foreign keys कहीं भी point नहीं करेंगी -- referencing tables (या उनके foreign key constraints) को आमतौर पर पहले drop या alter करना पड़ता है।
उदाहरण: Handling Foreign Key Dependencies
from unittest.mock import MagicMock
class Error(Exception):
pass
conn = MagicMock()
cursor = conn.cursor()
cursor.execute.side_effect = Error("Cannot drop: still referenced by orders")
try:
cursor.execute("DROP TABLE customers") # blocked because another table's foreign key points here
except Error as e:
print("Blocked:", e)
एक Alternative के रूप में Table को Truncate करना
TRUNCATE TABLE tableName, DELETE और DROP के बीच का एक middle ground है -- यह table structure बरकरार रखते हुए बहुत तेज़ी से (बड़ी tables के लिए DELETE से तेज़) सभी rows हटा देता है, पर DELETE के विपरीत इसे WHERE clause से selectively filter नहीं किया जा सकता और यह किसी भी AUTO_INCREMENT counter को वापस उसकी starting value पर reset कर देता है।
उदाहरण: Truncating a Table as an Alternative
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute("TRUNCATE TABLE customers") # removes all rows fast, keeps the structure, resets AUTO_INCREMENT
print("All rows removed, structure kept, AUTO_INCREMENT reset")
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