How to Check If a Table Exist in PL/SQL?
Last Updated :
06 Nov, 2024
In PL/SQL (Procedural Language/Structured Query Language), it's often necessary to determine whether a particular table exists in the database schema before attempting any operations on it. These views offer detailed metadata about database objects.
This article explores methods and techniques to check for the existence of a table in PL/SQL, providing examples and explanations for each approach.
Checking Table Existence in PL/SQL
The main concept involves querying the data dictionary views provided by the database management system to ascertain the existence of a table. PL/SQL offers various data dictionary views that contain metadata information about database objects, including tables. The syntax typically involves querying these views and checking if the desired table exists based on certain criteria.
Examples of Checking Table Existence in PL/SQL
Example 1: Using USER_TABLES Data Dictionary View
USER_TABLES contains metadata for all user-owned tables. Use this approach to determine if a table exists within the current schema.
DECLARE
v_table_name VARCHAR2(50) := 'employees';
v_count NUMBER;
BEGIN
SELECT COUNT(*)
INTO v_count
FROM USER_TABLES
WHERE TABLE_NAME = v_table_name;
IF v_count > 0 THEN
DBMS_OUTPUT.PUT_LINE('Table ' || v_table_name || ' exists in the current schema.');
ELSE
DBMS_OUTPUT.PUT_LINE('Table ' || v_table_name || ' does not exist in the current schema.');
END IF;
END;
- v_table_name: This variable holds the name of the table we want to check for existence.
- v_count: This variable stores the count of tables with the specified name in the current schema.
- SELECT COUNT(*) INTO v_count: This SQL statement selects the count of tables with the specified name from the USER_TABLES data dictionary view.
- IF v_count > 0 THEN: This conditional statement checks if the count is greater than 0, indicating that the table exists.
- DBMS_OUTPUT.PUT_LINE: This procedure outputs a message indicating whether the table exists or not in the current schema.
Explanation:
This PL/SQL block declares a variable v_table_name to hold the name of the table to be checked. It queries the USER_TABLES data dictionary view to count the number of tables with the specified name in the current schema. Based on the count, it prints a message indicating whether the table exists or not.
Example 2: Using DBA_TABLES Data Dictionary View (for privileged users)
DBA_TABLES includes metadata for all database tables but requires DBA privileges. Ideal for checking tables across schemas
DECLARE
v_table_name VARCHAR2(50) := 'employees';
v_count NUMBER;
BEGIN
SELECT COUNT(*)
INTO v_count
FROM DBA_TABLES
WHERE OWNER = USER AND TABLE_NAME = v_table_name;
IF v_count > 0 THEN
DBMS_OUTPUT.PUT_LINE('Table ' || v_table_name || ' exists in the database.');
ELSE
DBMS_OUTPUT.PUT_LINE('Table ' || v_table_name || ' does not exist in the database.');
END IF;
END;
- v_table_name: Same as in the first approach, this variable holds the name of the table we want to check for existence.
- v_count: Same as in the first approach, this variable stores the count of tables with the specified name.
- SELECT COUNT(*) INTO v_count: Similar to the first approach, this SQL statement selects the count of tables with the specified name, but from the DBA_TABLES data dictionary view.
- WHERE OWNER = USER AND TABLE_NAME = v_table_name: This condition ensures that the table belongs to the current user/schema.
- IF v_count > 0 THEN: Same as in the first approach, this conditional statement checks if the count is greater than 0, indicating that the table exists.
- DBMS_OUTPUT.PUT_LINE: Similar to the first approach, this procedure outputs a message indicating whether the table exists or not in the entire database.
Explanation:
This PL/SQL block is similar to Example 1 but queries the DBA_TABLES data dictionary view (which requires privileged access) to count the number of tables with the specified name in the entire database. It prints a message indicating whether the table exists or not based on the count.
Conclusion
Both approaches utilize data dictionary views to query metadata information about tables. The first approach checks within the current schema, while the second approach checks within the entire database. The choice between these approaches depends on the level of privilege and the scope of the search required.
Similar Reads
How to Check if a Column Exists in a SQL Server Table? In this article, we will look at how to check if a particular column exists in a database table or not. For checking the existence of a column we need to create the table first. So, let us create a table with some columns and data. Creating table: Syntax: CREATE TABLE table_name ( column1 datatype,
2 min read
How to Check If a Row Already Exists in PL/SQL? In database operations, particularly when dealing with data insertion, it's essential to ensure that duplicate rows are not inadvertently added to a table. In PL/SQL (Procedural Language/Structured Query Language), developers often need to check whether a row already exists in a table before perform
3 min read
How to Check if a Row Already Exists in SQLite? SQLite is a widely used relational database management system due to its simplicity, lightweight design and energy efficiency features. When working with SQLite databases, It is very important to ensure data integrity therefore it is necessary to avoid accidentally inserting duplicated records. In t
3 min read
How to Drop a Table If It Exists? When working with databases we may need to remove a table that we no longer need. Dropping a table removes the table structure and all the data contained within it. However, we might want to ensure that we don't encounter errors if the table doesn't exist when trying to drop it. In this article, we'
4 min read
How to Display all Tables in PL/SQL? In Oracle PL/SQL, we need to work with database objects like tables. It provides various approaches to display all the tables. Such as using the USER_TABLES, ALL_TABLES, and DBA_TABLES views. Each approach is explained with syntax and examples. In this article, We will learn about How to Display all
5 min read