Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Development Tools Database Foundations Manipulating Schema with SQL Removing Databases

Andrew Walters
Andrew Walters
8,876 Points

Drop if exists

Hey there guys! I'm curious, why would someone want to drop some (database/table/col/etc) using the key if it exists? What are the benefits to doing this? Having no experience in building databases, it doesn't really make sense to me to delete say a table if it doesn't exist in the first place.

2 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Andrew;

You will get errors if you ask it to drop a table which does not exist. Therefore it is a best practice to do a DROP TABLE IF EXISTS to prevent errors. It is also a frequently asked question in job interviews, in my experience. Same with the opposite end of the idea, CREATE TABLE IF NOT EXISTS, which prevents table duplication.

Happy coding,
Ken

Kevin Faust
Kevin Faust
15,353 Points

should you always use IF EXISTS? to avoid any sort of error i feel like we should always use it

I assume this would be more common in larger databases? I guess I'm struggling to see an example of when we wouldn't know the name of the table we want to delete, modify, or whatnot.

Andrew Walters
Andrew Walters
8,876 Points

Thanks! Must have missed the explanation, but that really helps!