DROP table student
If the table is not available then Error message we will get, so better to use IF EXISTS query while deleting table
DROP table IF EXISTS student
The above DROP table SQL command, we can use in Python script to delete MySQL table. from sqlalchemy import create_engine
my_conn = create_engine("mysql+mysqldb://userid:password@localhost/database_name")
With query
from sqlalchemy import create_engine
my_conn = create_engine("mysql+mysqldb://id:pw@localhost/my_db")
my_conn.execute("DROP table IF EXISTS student")
Using MySQL connector
import mysql.connector
my_conn = mysql.connector.connect(
host="localhost",
user="root",
passwd="password",
database="my_database"
)
####### end of connection ####
my_cursor = my_conn.cursor()
my_cursor.execute("DROP table IF EXISTS student")
We can capture the error message if any returned by MySQL database by using try except. Here is the code.
import mysql.connector
my_conn = mysql.connector.connect(
host="localhost",
user="userid",
passwd="password",
database="my_database"
)
####### end of connection ####
my_cursor = my_conn.cursor()
try:
my_cursor.execute("DROP table student")
except mysql.connector.Error as my_error:
print(my_error)
else:
print("Table deleted ")
Output is here
Table deleted
In above code we have not used IF EXISTS in our SQL statement to generate the error message. Note that if there is no error then the else part will display the message saying Table delete.
DROP TABLE `content`, `content_admin`, `content_cat`, `content_cmt_post`
ALTER TABLE 'content_cat' DROP INDEX 'cat_id'
The above command will remove the unique index associated with cat_id field of content_cat table
DROP DATABASE TEST
MySQL Delete records
More on Error handlingAuthor
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.