Welcome to the Treehouse Community
Looking to learn something new?
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.