Get Started: Python MySQL Drop Table

python-mysql-drop-table
Python MySQL Drop Table - This article is about Python MySQL Drop Table that describes how to delete a table and check if it exists or not in Python MYSQL.

In This Article, You Will Know About Python MySQL Drop Table.

Python MySQL Drop Table – Before moving ahead, let’s know a bit about Python MySQL Order By.

Table of Contents

DROP – Delete a Table

Use “DROP” statement to delete the existing table.

Syntax: DROP Object_type Object_name

Example: Use “DROP” statement to delete the existing table (Stu_information).

				
					import mysql.connector
my_user_details = mysql.connector.connect(host="localhost",
                                          username="your_MySQL_username",
                                          password="Your_MySQL_password",
                                          database="YOUR_MYSQL_DATABASE_NAME")
database_for_mysql = my_user_details.cursor()

database_for_mysql.execute("DROP TABLE Stu_information")

				
			

SHOW – Check if table exists

Use the “SHOW” statement to see the list of all tables in the current database or check whether the deleted table exists or not.

Syntax: SHOW Object_type

Example: Use “SHOW” statement to check if the table name is presented in the database or not.

				
					import mysql.connector
my_user_details = mysql.connector.connect(host="localhost",
                                          username="your_MySQL_username",
                                          password="Your_MySQL_password",
                                          database="YOUR_MYSQL_DATABASE_NAME")
database_for_mysql = my_user_details.cursor()

database_for_mysql.execute("SHOW TABLES")

				
			

As a result, it shows you a list of tables, if there is more than one table exists in your database, but will not include the table you deleted in the previous code, otherwise it will not show any table means you had only one table in your database, that you deleted in the previous code.

DROP – Only If Exist

If the table you’d like to erase has been deleted, or for reason, it’s not available then you can utilize the IF EXISTS keyword to avoid an error.

Syntax: DROP Object_type IF EXISTS Object_name

Example: Use “IF EXISTS” statement to delete the table (Stu_information) if it exists.

				
					import mysql.connector
my_user_details = mysql.connector.connect(host="localhost",
                                          username="your_MySQL_username",
                                          password="Your_MySQL_password",
                                          database="YOUR_MYSQL_DATABASE_NAME")
database_for_mysql = my_user_details.cursor()

database_for_mysql.execute("DROP TABLE IF EXISTS Stu_information")


				
			

As a result, if the above code is executed without any error, then the table has already been deleted successfully.

If you find anything incorrect in the above-discussed topic and have further questions, please comment below.

Connect on:

Recent Articles