Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python MySQL – Drop Table

  • Last Updated : 09 Mar, 2020

A connector is employed when we have to use MySQL with other programming languages. The work of MySQL-connector is to provide access to MySQL Driver to the required language. Thus, it generates a connection between the programming language and the MySQL Server.

Drop Table Command

Drop command affects the structure of the table and not data. It is used to delete an already existing table. For cases where you are not sure if the table to be dropped exists or not DROP TABLE IF EXISTS command is used. Both cases will be dealt with in the following examples.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

Syntax:



DROP TABLE tablename;

DROP TABLE IF EXISTS tablename;

The following programs will help you understand this better.

Tables before drop:

python-mysql-drop

Example 1: Program to demonstrate drop if exists. We will try to drop a table which does not exist in the above database.




# Python program to demonstrate
# drop clause
  
  
import mysql.connector
  
# Connecting to the Database
mydb = mysql.connector.connect(
  host ='localhost',
  database ='College',
  user ='root',
)
  
cs = mydb.cursor()
  
# drop clause
statement = "Drop Table if exists Employee"
  
# Uncommenting statement ="DROP TABLE employee"
# Will raise an error as the table employee
# does not exists
  
cs.execute(statement)
      
# Disconnecting from the database
mydb.close()


Output:

python-mysql-drop-1

Example 2: Program to drop table Geeks




# Python program to demonstrate
# drop clause
  
  
import mysql.connector
  
# Connecting to the Database
mydb = mysql.connector.connect(
  host ='localhost',
  database ='College',
  user ='root',
)
  
cs = mydb.cursor()
  
# drop clause
statement ="DROP TABLE Geeks"
  
cs.execute(statement)
      
# Disconnecting from the database
mydb.close()


Output:

python-mysql-drop-2




My Personal Notes arrow_drop_up
Recommended Articles
Page :