- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can you delete a table from a database in MySQL Python?
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.
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
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.
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.
- How can you delete a record from a table using MySQL in Python?
- How to delete a table from MongoDB database?
- How can we delete a single row from a MySQL table?
- How can we delete multiple rows from a MySQL table?
- How can we delete all rows from a MySQL table?
- How can we delete a MySQL stored function from the database?
- How to delete a column from a table in MySQL?
- How can we create a table from an existing MySQL table in the database?
- Delete multiple entries from a MySQL table
- How can I create a stored procedure to delete values from a MySQL table?
- Delete all records from a table in MySQL?
- How can you select data from a table based on some criteria using MySQL in Python?
- Delete all the records from a MySQL table?
- How can we delete a MySQL database by using PHP script?
- How can you update certain values in a table in MySQL using Python?