آموزش عبارت MySQL – DROP TABLE در پایتون

2 سال پیش

آموزش عبارت MySQL – DROP TABLE در پایتون

در این درس از مجموعه آموزش برنامه نویسی سایت سورس باران، به آموزش عبارت MySQL – DROP TABLE در پایتون خواهیم پرداخت.

پیشنهاد ویژه : پکیج آموزش صفر تا صد پایتون

با استفاده از عبارت DROP TABLE می توانید کل جدول را حذف کنید. فقط باید نام جدولی را که باید حذف کنید مشخص کنید.

در زیر دستور دستور DROP TABLE در MySQL – آمده است

DROP TABLE table_name;

 

مثال

قبل از حذف جدول، فهرست جداول را با استفاده از عبارت SHOW TABLES به صورت زیر دریافت کنید

mysql> SHOW TABLES;
+-----------------+
| Tables_in_mydb  |
+-----------------+
| contact         |
| cricketers_data |
| employee        |
| sample          |
| tutorials       |
+-----------------+
۵ rows in set (0.00 sec)

 

عبارت زیر جدول با نام نمونه را به طور کامل از پایگاه داده حذف می کند

mysql> DROP TABLE sample;
Query OK, 0 rows affected (0.29 sec)

 

از آنجایی که جدولی به نام نمونه را از MySQL حذف کرده ایم، اگر لیست جداول را دوباره دریافت کنید، نمونه نام جدول را در آن پیدا نمی کنید.

mysql> SHOW TABLES;
+-----------------+
| Tables_in_mydb  |
+-----------------+
| contact         |
| cricketers_data |
| employee        |
| tutorials       |
+-----------------+
۴ rows in set (0.00 sec)

 

حذف جدول با استفاده از پایتون

با استفاده از دستور DROP MYSQL می توانید هر زمان که نیاز داشتید جدولی را رها کنید، اما باید هنگام حذف جدول موجود بسیار مراقب باشید زیرا داده های از دست رفته پس از حذف جدول بازیابی نمی شوند.

برای رها کردن جدول از پایگاه داده MYSQL با استفاده از پایتون، متد ()execute را روی شی مکان نما فراخوانی کنید و عبارت drop را به عنوان پارامتر به آن ارسال کنید.

مثال

جدول زیر جدولی به نام EMPLOYEE را از پایگاه داده حذف می کند.

import mysql.connector

#establishing the connection conn = mysql.connector.connect(
   user='root', password='password', host='127.0.0.1', database='mydb'
)

#Creating a cursor object using the cursor() method 
cursor = conn.cursor()

#Retrieving the list of tables print("List of tables in the database: ") 
   cursor.execute("SHOW Tables") print(cursor.fetchall())

#Doping EMPLOYEE table if already exists cursor.execute
   ("DROP TABLE EMPLOYEE") print("Table dropped... ")

#Retrieving the list of tables print(
   "List of tables after dropping the EMPLOYEE table: ") 
   cursor.execute("SHOW Tables") print(cursor.fetchall())

#Closing the connection conn.close()

 

خروجی

List of tables in the database:
[('employee',), ('employeedata',), ('sample',), ('tutorials',)]
Table dropped...
List of tables after dropping the EMPLOYEE table:
[('employeedata',), ('sample',), ('tutorials',)]

اگر سعی کنید جدولی را که در پایگاه داده وجود ندارد رها کنید، خطایی به صورت – رخ می دهد

mysql.connector.errors.ProgrammingError: 1051 (42S02): 
   Unknown table 'mydb.employee'

با افزودن IF EXISTS به عبارت DELETE، می توانید با بررسی وجود جدول قبل از حذف، از این خطا جلوگیری کنید.

import mysql.connector

#establishing the connection
conn = mysql.connector.connect(
   user='root', password='password', host='127.0.0.1', database='mydb')

#Creating a cursor object using the cursor() method
cursor = conn.cursor()

#Retrieving the list of tables
print("List of tables in the database: ")
cursor.execute("SHOW Tables")
print(cursor.fetchall())

#Doping EMPLOYEE table if already exists
cursor.execute("DROP TABLE IF EXISTS EMPLOYEE")
print("Table dropped... ")

#Retrieving the list of tables
print("List of tables after dropping the EMPLOYEE table: ")
cursor.execute("SHOW Tables")
print(cursor.fetchall())

#Closing the connection
conn.close()

 

خروجی

List of tables in the database:
[('employeedata',), ('sample',), ('tutorials',)]
Table dropped...
List of tables after dropping the EMPLOYEE table:
[('employeedata',), ('sample',),
('tutorials',)]

 

منبع.

 

لیست جلسات قبل آموزش دسترسی به داده پایتون 

  1. آموزش دسترسی به داده پایتون 
  2. آموزش پایگاه MySQL در پایتون
  3. آموزش اتصال به پایگاه داده MySQL در پایتون
  4. آموزش ایجاد پایگاه داده MySQL در پایتون
  5. آموزش ایجاد جدول MySQL در پایتون
  6. آموزش درج داده MySQL در پایتون
  7. آموزش انتخاب داده MySQL در پایتون
  8. آموزش عبارت MySQL – Where در پایتون
  9. آموزش عبارت MySQL – OrderBy در پایتون
  10. آموزش به روز رسانی جدول MySQL در پایتون
  11. آموزش حذف داده ها MySQL در پایتون
0
برچسب ها :
نویسنده مطلب erfan molaei

دیدگاه شما

بدون دیدگاه