Python-data-access-python-mysql-drop-table

提供:Dev Guides
移動先:案内検索

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という名前のテーブルを削除したため、テーブルのリストを再度取得しても、テーブル名のサンプルは見つかりません。

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',)]