How to Display all Tables in PL/SQL?
Last Updated :
28 Oct, 2024
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 Tables in PL/SQL by understanding various methods and so on in detail.
Different Approaches to Display Tables in PL/SQL
When working with PL/SQL, there are several methods to display all tables in a database schema. Each method has its advantages depending on specific requirements. Below is the method that helps us to display all tables in PL/SQL are as follows:
Syntax
DECLARE
v_table_name all_tables.table_name%TYPE;
BEGIN
FOR table_rec IN (SELECT table_name FROM all_tables) LOOP
v_table_name := table_rec.table_name;
DBMS_OUTPUT.PUT_LINE(v_table_name);
END LOOP;
END;
/
Explanation:
- DECLARE is the beginning of the PL/SQL block. This block is used to declare the variables.
- v_table_name is declare the variable v_table_name of type all_tables . table_name%TYPE which is stores the table names retrieved from query.
- BEGIN is the mark for the beginning of the execution section of PL/SQL.
- FOR Loop is initiates the loop iterates through each row returned by SQL query.
- v_table_name is used to assigns the current table name from loop iteration to the v_table_name variable.
- DBMS_OUTPUT.PUT_LINE is used to print the statements in the output console.
- END LOOP is mark the loop end.
- END is mark the end of the executable section of PL/SQL block.
Setting Up the Environment
To understand How to display all tables in PL/SQL we need a some table on which we will perform various operations and queries. Here we will consider some table are shown below:
Departments Table:
Emp_id | Emp_name | Emp_salary |
---|
101 | Charan | 80000 |
102 | Jagan | 60000 |
103 | Pawan | 90000 |
104 | Kalyan | 85000 |
Departments Table:
Department_id | Department_name |
---|
201 | Electrical |
202 | Mechanical |
203 | Civil |
204 | Computer |
Orders Table:
Order_id | Customer_id |
---|
301 | 101 |
302 | 102 |
303 | 103 |
304 | 104 |
Approach 1: Using the USER_TABLES View
The USER_TABLES view displays tables that are specifically owned by the current user. This is useful for users who only need to work within their schema.
Suppose we have to display the names of all tables owned by the current user in a PL/SQL block using a cursor and the DBMS_OUTPUT.PUT_LINE
procedure.
DECLARE
v_table_name user_tables.table_name%TYPE;
BEGIN
FOR table_rec IN (SELECT table_name FROM user_tables) LOOP
v_table_name := table_rec.table_name;
DBMS_OUTPUT.PUT_LINE('Table Name: ' || v_table_name);
END LOOP;
END;
/
Output:
Table Name: Employees
Table Name: Departments
Table Name: Orders
PL/SQL procedure successfully completed.
Explanation:
- The USER_TABLES view contains the information about the tables owned by the current user.
- The PL/SQL block is iterate through the each table name retrieved from USER_TABLES view using the cursor.
- For each table name is fetched, it is print table name with the help of DBMS_OUTPUT.PUT_LINE.
Approach 2: Using the ALL_TABLES View
The ALL_TABLES view allows users to see all tables accessible to them, including tables owned by other users but accessible through permissions. This method is beneficial for users who need a broader view of the database schema, especially in collaborative environments.
Suppose we have to display the names of all tables accessible to the current user in a PL/SQL block using a cursor and the DBMS_OUTPUT.PUT_LINE
procedure.
DECLARE
v_table_name all_tables.table_name%TYPE;
BEGIN
FOR table_rec IN (SELECT table_name FROM all_tables) LOOP
v_table_name := table_rec.table_name;
DBMS_OUTPUT.PUT_LINE('Table Name: ' || v_table_name);
END LOOP;
END;
/
Output:
Table Name: Employees
Table Name: Departments
Table Name: Orders
PL/SQL procedure successfully completed.
Explanation:
- The ALL_TABLES view contains the information about the tables access to the current user.
- The PL/SQL block is iterate through the each table name retrieved from ALL_TABLES view using the cursor.
- For each table name is fetched, it is print table name with the help of DBMS_OUTPUT.PUT_LINE.
Approach 3: Using the DBA_TABLES View
The DBA_TABLES view lists every table within the Oracle database, but it is only accessible to users with DBA privileges. This view is ideal for database administrators who need to perform tasks such as database-wide audits, schema analyses, or table management across the entire database.
Suppose have to display the names of all tables in the database accessible to the user with DBA privileges in a PL/SQL block using a cursor and the DBMS_OUTPUT.PUT_LINE
procedure.
DECLARE
v_table_name dba_tables.table_name%TYPE;
BEGIN
FOR table_rec IN (SELECT table_name FROM dba_tables) LOOP
v_table_name := table_rec.table_name;
DBMS_OUTPUT.PUT_LINE('Table Name: ' || v_table_name);
END LOOP;
END;
/
Output:
Table Name: Employees
Table Name: Departments
Table Name: Orders
PL/SQL procedure successfully completed.
Explanation:
- The DBA_TABLES view is contain the information about the all tables database.
- The PL/SQL block is iterate through the each table name retrieved from DBA_TABLES view using the cursor.
- For each table name is fetched, it is print table name with the help of DBMS_OUTPUT.PUT_LINE.
Conclusion
Overall, displaying all tables in a PL/SQL environment is a task with multiple approaches. If using the USER_TABLES, ALL_TABLES, or DBA_TABLES view, developers and database administrators can easily retrieve and display table names based on their specific requirements. Understanding these methods enhances the ability to manage and analyze database schemas effectively in a PL/SQL environment.
Similar Reads
How to List All Tables in Oracle?
In this article, we will discuss all the methods to list all tables in the oracle SQL Database. We have three types of a subset of tables available to use as identifiers which in turn help us to sort the required table names. Here, are the following types of table identifiers in the Oracle SQL Datab
2 min read
How to Declare a Variable in PL/SQL?
Declaring variables in PL/SQL is a fundamental step towards building powerful and efficient database applications. Variables act as placeholders for data which enable us to manipulate and store information within our PL/SQL programs. Here, we will explore various methods of declaring variables in PL
5 min read
How to Drop All Tables from PostgreSQL
In PostgreSQL, managing our database often includes tasks like creating, modifying, and sometimes removing tables. If we want to clear our database and remove all tables, we can do this efficiently with a few PostgreSQL commands. To clear our database without deleting the entire schema, we can drop
5 min read
How to Open a PL/SQL File?
In database management and application development, PL/SQL (Procedural Language/Structured Query Language) files play an important role. These files contain stored procedures, functions, triggers, and other programmatic constructs essential for Oracle database systems. Opening a PL/SQL file is the f
4 min read
How to list the Tables in a SQLite Database File ?
SQLite is a database engine which is written in C programming language. SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. SQLite is the most widely deployed SQL database engine in the world. The source code for SQLite is
4 min read
How to Show/List Tables in MySQL Database
In MySQL, the SHOW TABLES command is a powerful tool used to list the tables within a specific database. This command provides a convenient way to view the tables that exist in a database without needing to query the database schema directly. In this article, we are going to explore various ways whe
5 min read
How to Get Counts of all Tables in a Schema in PL/SQL?
In Database Management System, it is essential to retrieve the statistical information about tables with the schema. Whether it is for monitoring the database health, optimizing the performance, or simply understanding the data structures having access to row counts of the tables can be more valuabl
5 min read
How to Declare a Variable in SQL?
Variables in SQL are fundamental for building efficient and scalable database applications. They enhance the flexibility and efficiency of database queries by acting as placeholders for data. Understanding how to declare and use variables in SQL is crucial for writing dynamic and effective queries I
3 min read
How to Query Multiple Tables in SQL
SQL (Structured Query Language) is a powerful tool for managing and querying relational databases. One of its most valuable features is the ability to query multiple tables simultaneously, allowing us to retrieve and integrate related data efficiently. In this article, we will explain how to query m
4 min read
How to Restore a Dump File in PL/SQL?
Dump files are essential in database management, storing data and structure in a binary format. They're important for backups, migrations, and setting up new environments. Typically created using tools like Oracle Data Pump or Export, they contain a database's data and structure, including tables, v
4 min read