Check if Table, View, Trigger, etc present in Oracle


Introduction

Oracle is a powerful, versatile relational database management system that is widely used in the enterprise. One of the most common tasks when working with Oracle is to check if a specific object, such as a table, view, trigger, or other types of object, exists in the database. This can be useful for a variety of purposes, such as validating input data, checking for dependencies, and more. In this article, we will explore several methods for checking if a table, view, trigger, or other object exists in Oracle.

Using the "DBA_OBJECTS" View

One of the easiest and most straightforward ways to check if an object exists in Oracle is to use the "DBA_OBJECTS" view. This view contains information about all objects in the database, including their names, types, and status. By querying this view and searching for a specific object, you can determine whether or not it exists in the database.

The following code snippet shows an example of how to query the "DBA_OBJECTS" view to check if a table named "employees" exists in the database −

SELECT COUNT(*) FROM DBA_OBJECTS WHERE OBJECT_TYPE = 'TABLE' AND OBJECT_NAME = 'EMPLOYEES';

In this example, the query returns the number of rows that match the specified criteria. If the query returns a value of 0, then the table does not exist in the database. If it returns a value greater than 0, then the table does exist in the database.

Using the "OBJECT_ID" Function

Another way to check if an object exists in Oracle is to use the "OBJECT_ID" function. This function returns the unique identifier for an object if it exists in the database. If the object does not exist, the function will return a null value.

The following code snippet shows an example of how to use the "OBJECT_ID" function to check if a view named "employee_view" exists in the database −

SELECT OBJECT_ID('EMPLOYEE_VIEW', 'VIEW') FROM DUAL;

In this example, the query returns the unique identifier of the "employee_view" view if it exists in the database. If the view does not exist, the query will return a null value.

Using the "EXISTS" Clause

A third way to check if an object exists in Oracle is to use the "EXISTS" clause in a subquery. This can be useful when you need to check if an object exists as part of a larger query or when you want to check for the existence of multiple objects at once.

The following code snippet shows an example of how to use the "EXISTS" clause to check if a table named "employees" and a trigger named "employee_trigger" exists in the database −

SELECT 'Employees Table Exists' FROM DUAL WHERE EXISTS (SELECT 1 FROM DBA_OBJECTS WHERE OBJECT_TYPE = 'TABLE' AND OBJECT_NAME = 'EMPLOYEES'); SELECT 'Employee Trigger Exists' FROM DUAL WHERE EXISTS (SELECT 1 FROM DBA_OBJECTS WHERE OBJECT_TYPE = 'TRIGGER' AND OBJECT_NAME = 'EMPLOYEE_TRIGGER');

In this example, the first query checks if the "employees" table exists in the database, and the second query checks if the "employee_trigger" trigger exists. If the object exists, the query will return the specified message. If the object does not exist, the query will return no results.

Using the "ALL_OBJECTS" View

Another useful view for checking if an object exists in Oracle is the "ALL_OBJECTS" view. This view contains information about all objects that the current user has access to, including their names, types, and status. By querying this view and searching for a specific object, you can determine whether or not it exists in the database and if the user has access to it.

The following code snippet shows an example of how to query the "ALL_OBJECTS" view to check if a table named "employees" exists and the current user has access to it −

SELECT COUNT(*) FROM ALL_OBJECTS WHERE OBJECT_TYPE = 'TABLE' AND OBJECT_NAME = 'EMPLOYEES';

Using the "USER_OBJECTS" View

Another way to check if an object exists and the current user has access to it is to use the "USER_OBJECTS" view. This view contains information about all objects that the current user owns, including their names, types, and status. By querying this view and searching for a specific object, you can determine whether or not it exists in the database and if the user has access to it.

The following code snippet shows an example of how to query the "USER_OBJECTS" view to check if a view named "employee_view" exists and the current user owns it −

SELECT COUNT(*) FROM USER_OBJECTS WHERE OBJECT_TYPE = 'VIEW' AND OBJECT_NAME = 'EMPLOYEE_VIEW';

Using the "EXECUTE IMMEDIATE" Statement

Another way to check if an object exists in Oracle is to use the "EXECUTE IMMEDIATE" statement, this allows you to execute a dynamic SQL statement, which can be useful for checking for the existence of an object without knowing its name or type in advance.

The following code snippet shows an example of how to use the "EXECUTE IMMEDIATE" statement to check if a table or view named "employees" exists in the database −

DECLARE object_exists INTEGER; BEGIN EXECUTE IMMEDIATE 'SELECT COUNT(*) FROM ALL_OBJECTS WHERE OBJECT_NAME = ''EMPLOYEES'' AND (OBJECT_TYPE = ''TABLE'' OR OBJECT_TYPE = ''VIEW'')' INTO object_exists; IF object_exists > 0 THEN DBMS_OUTPUT.PUT_LINE('Object exists'); ELSE DBMS_OUTPUT.PUT_LINE('Object does not exist'); END IF; END;

In this example, the "EXECUTE IMMEDIATE" statement is used to execute a dynamic SQL statement that checks for the existence of a table or view named "employees" in the "ALL_OBJECTS" view. The result of the query is stored in the "object_exists" variable, which is then used to determine if the object exists or not.

In addition to these methods, there are other ways to check if an object exists in Oracle, such as using the "DBMS_METADATA" package or the "DBA_OBJECTS" view. Each method has its own set of advantages and disadvantages, so it's important to choose the right one for your specific use case.

The "DBA_OBJECTS" view, for example, provides a comprehensive list of all objects in the database, while the "ALL_OBJECTS" and "USER_OBJECTS" view only provide information on objects that the current user has access to. The "OBJECT_ID" function and the "EXISTS" clause are useful when you need to check for the existence of an object as part of a larger query, while the "EXECUTE IMMEDIATE" statement allows for checking for the existence of an object without knowing its name or type in advance.

It's also worth noting that some of these methods might require specific permissions, for example, querying the "DBA_OBJECTS" view requires DBA or SELECT_CATALOG_ROLE privileges, using "EXECUTE IMMEDIATE" also requires EXECUTE privilege. Therefore, it's important to make sure that the user or role that is executing these queries has the necessary permissions to do so.

Real-Life Examples

  • In a web application, you may want to check if a specific table exists in the database before allowing a user to perform certain actions. For example, you may want to check if a table named "orders" exists before allowing a user to view or modify the orders in the system. By using one of the methods outlined above, you can easily check for the existence of the table and take the appropriate action.

  • In a data migration process, you may want to check if all of the necessary tables, views, and triggers exist in the target database before proceeding with the migration. By using the "DBA_OBJECTS" view or the "OBJECT_ID" function, you can quickly and easily check for the existence of all the necessary objects and ensure that the migration process goes smoothly.

  • In a database management system, you may want to check if a specific view or trigger exists before allowing a user to modify or delete it. By using the "EXISTS" clause, you can check for the existence of multiple objects at once, ensuring that the user is only allowed to make changes to objects that actually exist in the database.

Conclusion

In this article, we have explored several methods for checking if a table, view, trigger, or other object exists in Oracle. Whether you're working on a web application, a data migration process, or a database management system, these methods can help you easily and quickly check for the existence of objects in the database, ensuring that your code runs smoothly and without errors.

Updated on: 16-Jan-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements