Deleting a table completely
You're now going to practice dropping individual tables from a database with the .drop()
method, as well as all tables in a database with the .drop_all()
method!
As Spider-Man's Uncle Ben (as well as Jason, in the video!) said: With great power, comes great responsibility. Do be careful when deleting tables,
as it's not simple or fast to restore large databases! Remember, you can check to see if a table exists on an engine
with the .exists(engine)
method.
This is the final exercise in this chapter: After this, you'll be ready to apply everything you've learned to a case study in the final chapter of this course!
This exercise is part of the course
Introduction to Databases in Python
Exercise instructions
- Drop the
state_fact
table by applying the method.drop()
to it and passing it the argumentengine
(in fact,engine
will be the sole argument for every function/method in this exercise!) - Check to see if
state_fact
exists viaprint
. Use the.exists()
method withengine
as the argument. - Drop all the tables via the
metadata
using the.drop_all()
method. - Use a print statement to check if the
census
table exists.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Drop the state_fact table
# Check to see if state_fact exists
print(____)
# Drop all tables
# Check to see if census exists
print(____)