Yesterday I posted 3 useful MySQL queries that I use almost every day. As I also work with Oracle I have to use the equivalents for that database i.e. I regularly have to create database update scripts – scripts that create tables or add new columns to existing tables or populate new tables with base data. One requirement is that these scripts be re-runnable. For example, if a new table doesn’t exist it should be created, if it does exist then the update script should continue without throwing an error. For these situations I find the following simple queries very useful. Wrap them in functions written in your language of choice and you have an easy way to check the current status of your Oracle database before taking action.
1. Check if a table exists in the current database schema
select table_name from user_tables where table_name = '__TABLE_NAME__'
select column_name as found from user_tab_cols where table_name = '__TABLE_NAME__' and column_name = '__COLUMN_NAME__'
select 1 from __TABLE_NAME__ where rownum = 1
