Categories: python

Drop Table Query – MySQL

The Drop Table query affects the structure of the table rather than the data. It is used to remove an existing table. When you are unsure whether a table to be dropped exists or not, use the DROP TABLE IF EXISTS command.

Syntax:

Following is the syntax of the DROP TABLE query in MySQL −

DROP TABLE table_name;

Example: Program to demonstrate drop if exists.

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()

The IF EXISTS keyword is used to prevent errors from occurring when attempting to drop a table that does not exist.

When we use the IF EXISTS clause, we are telling the SQL engine that if the given table name exists, drop it; otherwise, do nothing.

If the code executes without an error, then it means the employee table is deleted if it existed.

Note: also read about Delete Query – MySQL

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Recent Posts

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

2 hours ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

2 weeks ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

3 weeks ago

Staircase Climbing Ways

There is a staircase with N steps, and you can ascend either 1 step or…

3 weeks ago

Autocomplete System Implementation

Build an autocomplete system that, given a query string s and a set of possible…

3 weeks ago

Job Scheduler Implementation

Design a job scheduler that accepts a function f and an integer n. The scheduler…

4 weeks ago