
- Python 資料訪問教程
- Python 資料訪問 - 首頁
- Python MySQL
- Python MySQL - 簡介
- Python MySQL - 資料庫連線
- Python MySQL - 建立資料庫
- Python MySQL - 建立表
- Python MySQL - 插入資料
- Python MySQL - 選擇資料
- Python MySQL - Where 子句
- Python MySQL - Order By
- Python MySQL - 更新表
- Python MySQL - 刪除資料
- Python MySQL - 刪除表
- Python MySQL - Limit
- Python MySQL - Join
- Python MySQL - 遊標物件
- Python PostgreSQL
- Python PostgreSQL - 簡介
- Python PostgreSQL - 資料庫連線
- Python PostgreSQL - 建立資料庫
- Python PostgreSQL - 建立表
- Python PostgreSQL - 插入資料
- Python PostgreSQL - 選擇資料
- Python PostgreSQL - Where 子句
- Python PostgreSQL - Order By
- Python PostgreSQL - 更新表
- Python PostgreSQL - 刪除資料
- Python PostgreSQL - 刪除表
- Python PostgreSQL - Limit
- Python PostgreSQL - Join
- Python PostgreSQL - 遊標物件
- Python SQLite
- Python SQLite - 簡介
- Python SQLite - 建立連線
- Python SQLite - 建立表
- Python SQLite - 插入資料
- Python SQLite - 選擇資料
- Python SQLite - Where 子句
- Python SQLite - Order By
- Python SQLite - 更新表
- Python SQLite - 刪除資料
- Python SQLite - 刪除表
- Python SQLite - Limit
- Python SQLite - Join
- Python SQLite - 遊標物件
- Python MongoDB
- Python MongoDB - 簡介
- Python MongoDB - 建立資料庫
- Python MongoDB - 建立集合
- Python MongoDB - 插入文件
- Python MongoDB - 查詢
- Python MongoDB - 查詢
- Python MongoDB - 排序
- Python MongoDB - 刪除文件
- Python MongoDB - 刪除集合
- Python MongoDB - 更新
- Python MongoDB - Limit
- Python 資料訪問資源
- Python 資料訪問 - 快速指南
- Python 資料訪問 - 有用資源
- Python 資料訪問 - 討論
Python MySQL - 刪除表
您可以使用DROP TABLE語句刪除整個表。您只需要指定要刪除的表的名稱。
語法
以下是 MySQL 中 DROP TABLE 語句的語法:
DROP TABLE table_name;
示例
在刪除表之前,請使用 SHOW TABLES 語句獲取表的列表,如下所示:
mysql> SHOW TABLES; +-----------------+ | Tables_in_mydb | +-----------------+ | contact | | cricketers_data | | employee | | sample | | tutorials | +-----------------+ 5 rows in set (0.00 sec)
以下語句將完全從資料庫中刪除名為 sample 的表:
mysql> DROP TABLE sample; Query OK, 0 rows affected (0.29 sec)
由於我們已從 MySQL 中刪除了名為 sample 的表,因此如果您再次獲取表的列表,將不會在其中找到表名 sample。
mysql> SHOW TABLES; +-----------------+ | Tables_in_mydb | +-----------------+ | contact | | cricketers_data | | employee | | tutorials | +-----------------+ 4 rows in set (0.00 sec)
使用 python 刪除表
您可以根據需要隨時使用 MYSQL 的 DROP 語句刪除表,但在刪除任何現有表時必須非常小心,因為刪除表後資料將無法恢復。
要使用 python 從 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 來驗證表是否存在,從而防止此錯誤。
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',)]
廣告