With SQL we can use various methods to check whether or not a table (or other object) exists in the database. The method we use will often depend on the RDBMS we’re using, as well as the task we’re trying to undertake.
There’s usually a reason we’re trying to check for the existence of a table, and often the syntax we use will be tied to that reason. For example the ...IF EXISTS
clause is a handy addition to the DROP TABLE
statement, and the ...IF NOT EXISTS
clause can often be used with the CREATE TABLE
statement.
Other times we may simply want to see if the table exists without performing any immediate actions against that table. In such cases, we would need to run code specifically to see if the table exists.
Below are examples of code we can use in each of the above scenarios.
Continue reading →