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
Why use if not exists ?
I don't understand what is the reason of using the SQL syntax IF NOT EXISTS when creating a database, is it like a conditional or is just there to disable the errors and show warning messages instead ? I am a bit confused and any example would be helpful thanks !
The if NOT exists is used to check if the database exists prior to creating a new one. It is saying:
If the database does not exist, then create one.
You do not want to create a duplicate database, so this ensures you do not. Theoretically, you could use an if exists, then do all this stuff, else create the database. But if you do that, then you have to recode all the stuff you want to do after the database is created.
Andre West2,861 Points
The way I understood it is that by using "if not exists" you get a warning instead of an error, in case youre trying to create a database that already exists. The reason why you would want a warning over an error is circumstantial but an error will stop your code from running while it will continue with a warning.