Home >>Python Tutorial >Python Mysql Drop Table

Python Mysql Drop Table

To Delete a Table

This is the python mysql drop table part of our entire Python MySQL tutorial. And while using this programming language, if you wish to delete a pre-existing table then you can do that too. And to accomplish that task, it is suggested that you should use the “DROP TABLE” statement. If you wish to delete a table that is named as ‘customers’ then the python mysql drop all tables example for this is mentioned below.  
import mysql.connector

mydb = mysql.connector.connect (

  host=“localhost” ,

  user=“yourusername” ,

  passwd=“yourpassword” ,

  database=“mydatabase” ,

)

mycursor = mydb.cursor ( )

sql = “DELETE TABLE customers”
mycursor.execute ( sql )

  We’ll now be discussing about the function of Python mysql drop table if exists. The details and the example of this function are mentioned below.

To Drop Only if Exist

If you wish to delete a particular table but you are not entirely sure whether you have already deleted that table or if that table even exists then it is suggested that you should use the IF EXISTS keyword to perform the function of Python mysql drop table if exists. If you wish to delete a table named ‘customers’ with the help of this keyword then the python mysql drop all tables example for this is mentioned below.  
import mysql.connector

mydb = mysql.connector.connect (

  host=“localhost” ,

  user=“yourusername” ,

  passwd=“yourpassword” ,

  database=“mydatabase” ,

)

mycursor = mydb.cursor ( )

sql = “DELETE TABLE IF EXISTS customers”

mycursor.execute ( sql )

  With this, we finish the Python mysql drop table part of our entire Python MySQL Tutorial.

No Sidebar ads