PL/SQL CREATE VIEW
is a statement used to create a virtual table based on the result of a query. Views in PL/SQL allow users to access and manipulate data stored in one or more underlying tables as if it were a single table. In this article, We will learn about the PL/SQL CREATE VIEW by understanding the various examples with the output and so on.
CREATE VIEW in PL/SQL
PL/SQL 'CREATE VIEW' is a statement used to create a virtual table based on the result of a query. Views allow users to access and manipulate data stored in one or more underlying tables as if it were a single table. The 'CREATE VIEW' statement defines the view's name, the columns it will include and the query that retrieves the data. Views provide a way to simplify complex queries enhance data security and improve performance by storing frequently used query logic.
Syntax
CREATE [OR REPLACE] VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
[WHERE condition];
Explanation:
- CREATE VIEW view_name initiates the creation of a view with a specified name.
- [OR REPLACE] is optional, If the view already exists, it will allow us to modify the existing view with a new description. Otherwise, it creates a new view.
- AS SELECT.... is defined as the SELECT query whose result set will content of the view.
- [WHERE CONDITION] is optional, it applies the condition to filtering the rows included in the view.
Setting Up The Environment
To understand about the PL/SQL CREATE VIEW we need a table on which we will perform various operations and queries. Here we will consider a table called products which shown below:
Output:
product_id | product_name | category | price | stock_quantity |
---|
1 | Laptop | Electronics | 1200 | 50 |
2 | Smartphone | Electronics | 800 | 100 |
3 | Headphones | Electronics | 150 | 200 |
4 | Book | Books | 20 | 500 |
Example 1: Creating a View to Display Specific Columns From Table
Let's Create a view named `product_info` that includes `product_id`, `product_name`, and `price` from the `products` table. This view simplifies querying for specific product information.
CREATE OR REPLACE VIEW product_info AS
SELECT product_id, product_name, price
FROM products;
select * from product_info;
Output:
product_id | product_name | price |
---|
1 | Laptop | 1200 |
2 | Smartphone | 800 |
3 | Headphones | 150 |
4 | Book | 20 |
Explanation:
- The name of the view is product_info, it selects the columns such as product_id, product_name and price from the products table.
- When we querying the above view, only selected columns will be appear, other columns will be hidden.
Example 2: Creating a View with Data Filtered Based on Condition
Let's Create a view named expensive_products that includes `product_id`, `product_name`, and `price` from the `products` table where the `price` is greater than 100. This view provides a filtered list of products that are considered expensive.
CREATE OR REPLACE VIEW expensive_products AS
SELECT product_id, product_name, price
FROM products
WHERE price > 100;
select * from expensive_products;
Output:
product_id | product_name | price |
---|
1 | Laptop | 1200 |
2 | Smartphone | 800 |
3 | Headphones | 150 |
Explanation:
- The name of the view is expensive_products. It is select the rows which is the price is greater than 100.
- When we querying the view, only products meeting the above condition will visible, making it is easy to focus the specific subset of the data.
Conclusion
In conclusion, views in PL/SQL are offer the powerful tool for the simplifying data access and data manipulation. By the abstracting the underlying the complexities of the database structures, views are provide the convenient way to the present subsets of data to the users, enhancing the data security, improving the performance and simplifying the development of application.
Similar Reads
SQL - Rename View In relational databases, views are essential tools used to simplify complex queries, enhance data security, and improve overall database management. However, as our database evolves, the need to rename existing views may arise. This renaming process helps maintain consistency, improves clarity, and
6 min read
PL/SQL VIEW In Oracle PL/SQL, views are a powerful way to manage data access and simplify complex queries. A view is essentially a virtual table that presents data from one or more tables using a stored query. Unlike physical tables, views do not store the data themselves; they dynamically retrieve data based o
4 min read
PL/SQL DROP VIEW Views serve as an important tool for simplifying complex queries, enhancing data security, and streamlining database management. They provide a virtual layer that presents data from underlying tables in a structured format. However, views like other database objects need to be managed effectively to
3 min read
PL/SQL RENAME VIEW PL/SQL which is Oracle Corporation's extension of SQL with a procedural language enables developers and database administrators to carry out complex operations in the databases. One of the frequently performed operations in the process of database maintenance is the change of names of objects to acc
3 min read
SQL - DROP View SQL Views provide a powerful way to simplify complex queries and present data in a more understandable format. However, there may be times when we need to remove a view from our database schema. In SQL, deleting a view is straightforward using the DROP VIEW command. In this article, we will explain
5 min read