"Be the change that you want to see in the world."
- Mahatma Gandhi
View Fundamentals
A view is a virtual table.
A view may define a subset of columns.
A view can even define a subset of rows if it has a WHERE clause.
A view never duplicates data or stores the data separately.
Views provide security.
View Advantages
An additional level of security is provided.
Helps the business user not miss join conditions.
Help control read and update privileges.
Unaffected when new columns are added to a table.
Unaffected when a column is dropped unless its referenced in the view.
The above is designed to introduce View fundamentals, View advantages and View recommendations.
The purposes of views are to restrict access to certain columns, derive columns or Join Tables, and to restrict access to certain rows (if a WHERE clause is used). This view does not allow the user to see the column salary.
To see the basic information about a view you can use the DESCRIBE <viewname> command.
To see more detailed information about a view you can use the DESCRIBE EXTENDED <viewname> command.
You SELECT from a view exactly like you SELECT from a table. A user might not know whether or not they are querying a view or a table. Notice we added a WHERE Clause and an ORDER BY statement when querying the view.
The purposes of views are to restrict access to certain columns, derive columns or Join Tables, and to restrict access to certain rows (if a WHERE clause is used). This view does not allow the user to see information about rows unless the rows have a Dept_No of either 300 or 400.
This view restricts the column Salary from being seen and all rows except for Dept_No 300 and 400 rows.
No ORDER BY inside the View CREATE (exceptions exist)
All Aggregation needs to have an ALIAS
Any Derived columns (such as Math) needs an ALIAS
Above are the basic rules of Views with excellent examples.
The CREATE OR REPLACE Keywords will allow a user to change a view.
There are EXCEPTIONS to the ORDER BY rule when creating a view. ANSI OLAP statements that use the ORDER BY statement also work inside a View.
CREATE VIEW Order_V6 AS
SELECT
Order_Number
,Customer_Number
,regexp_extract(Order_Time, '(.*-.*-.*)\s(.*)',1) as Ord_Date
,Order_Total As Total
FROM Order_Table ;
SELECT *
FROM Order_V6 ;
Views are designed to do many things. In the example above, this view formats some of the data.
This view is designed to join two tables together. By creating a view, we have now made it easier for the user community to join these tables by merely selecting the columns and rows they want from the view.
SELECT *
FROM E_View
ORDER BY Mnth_Sal ;
Columns in a view that are derived, such as aggregates or calculated columns, must have an alias. There are multiple ways to alias the columns and this example shows you the first option.
SELECT *
FROM E_View2
ORDER BY Sal_Monthly ;
The ALIAS for Salary / 12 that’ll be used in this example is Sal_Monthly. This form of aliasing is the most popular, but we will show you multiple ways to alias a column in a view.
SELECT *
FROM E_View3
ORDER BY 3 ;
The ALIAS for Salary / 12 that is used in this example is Mnth_Sal. It came first at the top, even though it is aliased in the SELECT list also. This example actually shows both ways to alias a column, but understand that only the top alias will be recognized.
What happens when this query runs?
What will happen in the above query?
What happens when this query runs?
Error – Sal_Mnth is unrecognized
The query above errors because Sal_Mnth is an unrecognized alias. That is because we did our aliasing at the top so this makes the second alias non-valid for use when querying the view.
CREATE VIEW Aggreg_Order_v AS
SELECT
Customer_Number
,COUNT(Order_Total)AS Order_Cnt
,SUM(Order_Total)AS Order_Sum
,AVG(Order_Total)AS Order_Avg
FROM Order_Table
GROUP BY Customer_Number ;
The examples above show how we put a SUM on the aggregate Order_Sum!
This view will run after the table has added an additional column!