It can be at times required to delete the whole table from the database. It is bad use of the storage to keep the unwanted data in the database. Suppose, we have a table named “Employees” in our database and due to some reasons , we do not require this table in our database anymore. Therefore, it is best to delete the particular table which is of no use to us.
This is done using the “DROP TABLE” command. This table deletes the entire table from the database.
Syntax
DROP TABLE table_name
Here, table_name specifies the name of the table you want to delete.
Steps invloved to delete a table in database using MySQL in python
import MySQL connector
establish connection with the connector using connect()
create the cursor object using cursor() method
create a query using the appropriate mysql statements
execute the SQL query using execute() method
close the connection
Example
Suppose we have a table named “Employees” in our database and we want to delete this table from our database.
import mysql.connector db=mysql.connector.connect(host="your host", user="your username", password="your password",database="database_name") cursor=db.cursor() query="DROP TABLE Employees " cursor.execute(query) print("TABLE DROPED..") db.close()
The above code when executed without any error deletes the table named “Employees” from the database. This can be verified by executing the “SHOW TABLES” statement.
Output
TABLE DROPED..
NOTE
The DELETE and DROP statements must not be considered same. Even though we can delete all the records from the table using DELETE command, but there is difference between the two statements. The DELETE statement is just used to delete all the rows from the table, it does not erase the table definition. The DROP command on the other hand is used to delete the whole table as well as the schema of the table.