Menu Close

Python MySQL Drop Table Statement

Python MySQL Drop Table

In this Python MySQL Drop Table tutorial, we are going to learn how to drop the existing table using Python MySQL Connector. In the previous tutorial, we have seen a complete process to delete the specific records or all the records from the table.

Make sure you have already installed MySQL connector in your machine using pip as well as create database and table.

Python MySQL Drop Table:

MySQL provide DROP TABLE statement to delete the existing table from the MySQL Database.

Example:

In this example, we will delete a table named “employess“.


import mysql.connector
mydb = mysql.connector.connect(

	host="localhost",
	user="root",
	password="root21",
	database = "demodb",
    port = 3308
	
)
cursor = mydb.cursor()
cursor.execute('DROP TABLE employees')
print("Table successfully deleted..")

Output will be:- Table successfully deleted..

Drop Only if exist:

Here we will check table is exist or not, which we want to delete by using “IF EXISTS” keyword.

Example:


import mysql.connector
mydb = mysql.connector.connect(

	host="localhost",
	user="root",
	password="root21",
	database = "demodb",
    port = 3308
	
)

cursor = mydb.cursor()
cursor.execute('DROP TABLE IF EXISTS employees')
mydb.commit()
print("Table succesfully deleted..")

Conclusion

In this Python article, you have learned all about how to drop tables from the MySQL database by using the MySQL DROP TABLE statement.
Python MySQL connector is the best for connecting Python applications to the MySQL database and executes SQL queries. After created of database successfully, you can perform a query on that particular database.

If this article for you, please share and keep visiting for further Python MySQL database tutorials.

More Tutorials


Python MySQL LIMIT
Python MySQL Order By Clause

Related Posts