
- Python Data Access Tutorial
- Python Data Access - Home
- Python MySQL
- Python MySQL - Introduction
- Python MySQL - Database Connection
- Python MySQL - Create Database
- Python MySQL - Create Table
- Python MySQL - Insert Data
- Python MySQL - Select Data
- Python MySQL - Where Clause
- Python MySQL - Order By
- Python MySQL - Update Table
- Python MySQL - Delete Data
- Python MySQL - Drop Table
- Python MySQL - Limit
- Python MySQL - Join
- Python MySQL - Cursor Object
- Python PostgreSQL
- Python PostgreSQL - Introduction
- Python PostgreSQL - Database Connection
- Python PostgreSQL - Create Database
- Python PostgreSQL - Create Table
- Python PostgreSQL - Insert Data
- Python PostgreSQL - Select Data
- Python PostgreSQL - Where Clause
- Python PostgreSQL - Order By
- Python PostgreSQL - Update Table
- Python PostgreSQL - Delete Data
- Python PostgreSQL - Drop Table
- Python PostgreSQL - Limit
- Python PostgreSQL - Join
- Python PostgreSQL - Cursor Object
- Python SQLite
- Python SQLite - Introduction
- Python SQLite - Establishing Connection
- Python SQLite - Create Table
- Python SQLite - Insert Data
- Python SQLite - Select Data
- Python SQLite - Where Clause
- Python SQLite - Order By
- Python SQLite - Update Table
- Python SQLite - Delete Data
- Python SQLite - Drop Table
- Python SQLite - Limit
- Python SQLite - Join
- Python SQLite - Cursor Object
- Python MongoDB
- Python MongoDB - Introduction
- Python MongoDB - Create Database
- Python MongoDB - Create Collection
- Python MongoDB - Insert Document
- Python MongoDB - Find
- Python MongoDB - Query
- Python MongoDB - Sort
- Python MongoDB - Delete Document
- Python MongoDB - Drop Collection
- Python MongoDB - Update
- Python MongoDB - Limit
- Python Data Access Resources
- Python Data Access - Quick Guide
- Python Data Access - Useful Resources
- Python Data Access - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python MySQL - Drop Table
You can remove an entire table using the DROP TABLE statement. You just need to specify the name of the table you need to delete.
Syntax
Following is the syntax of the DROP TABLE statement in MySQL −
DROP TABLE table_name;
Example
Before deleting a table get the list of tables using the SHOW TABLES statement as follows −
mysql> SHOW TABLES; +-----------------+ | Tables_in_mydb | +-----------------+ | contact | | cricketers_data | | employee | | sample | | tutorials | +-----------------+ 5 rows in set (0.00 sec)
Following statement removes the table named sample from the database completely −
mysql> DROP TABLE sample; Query OK, 0 rows affected (0.29 sec)
Since we have deleted the table named sample from MySQL, if you get the list of tables again you will not find the table name sample in it.
mysql> SHOW TABLES; +-----------------+ | Tables_in_mydb | +-----------------+ | contact | | cricketers_data | | employee | | tutorials | +-----------------+ 4 rows in set (0.00 sec)
Removing a table using python
You can drop a table whenever you need to, using the DROP statement of MYSQL, but you need to be very careful while deleting any existing table because the data lost will not be recovered after deleting a table.
To drop a table from a MYSQL database using python invoke the execute() method on the cursor object and pass the drop statement as a parameter to it.
Example
Following table drops a table named EMPLOYEE from the database.
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()
Output
List of tables in the database: [('employee',), ('employeedata',), ('sample',), ('tutorials',)] Table dropped... List of tables after dropping the EMPLOYEE table: [('employeedata',), ('sample',), ('tutorials',)]
Drop table only if exists
If you try to drop a table which does not exist in the database, an error occurs as −
mysql.connector.errors.ProgrammingError: 1051 (42S02): Unknown table 'mydb.employee'
You can prevent this error by verifying whether the table exists before deleting, by adding the IF EXISTS to the DELETE statement.
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()
Output
List of tables in the database: [('employeedata',), ('sample',), ('tutorials',)] Table dropped... List of tables after dropping the EMPLOYEE table: [('employeedata',), ('sample',), ('tutorials',)]