Flash cards
Review the key moves
1/4
Core idea
What is the main idea behind Python MySQL Drop Table?
Lesson checks
Practice each idea before moving on
Short Mimo-style checks built from this lesson's code, terms, and sequence.
1Quick choice
Which statement best captures the main point of this lesson?
2Fill blank
Complete the missing token from the example code.
___ mysql.connector3Order
Put the learning moves in the order that makes the concept easiest to apply.
Delete the table "customers":
You can delete an existing table by using the "DROP TABLE" statement:
Drop Only if Exist
Delete a Table
You can delete an existing table by using the "DROP TABLE" statement:
Example
Delete the table "customers":
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="
yourusername
",
password="
yourpassword
",
database="mydatabase"
)
mycursor =
mydb.cursor()
sql = "DROP TABLE customers"
mycursor.execute(sql)Drop Only if Exist
If the table you want to delete is already deleted, or for any other reason does not exist, you can use the IF EXISTS keyword to avoid getting an error.
Example
Delete the table "customers" if it exists:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="
yourusername
",
password="
yourpassword
",
database="mydatabase"
)
mycursor =
mydb.cursor()
sql = "DROP TABLE IF EXISTS customers"
mycursor.execute(sql)