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

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago