Referencing Views When we execute the above CREATE VIEW statement, it creates a view called CustomersOrdersRefunds.. The output is:First Name Last Name Order Date Natalie Lopez 2009-09-0
Trang 1FROM Customers
LEFT JOIN Orders
ON Customers.CustomerID ¼ Orders.CustomerID
LEFT JOIN Refunds
ON Orders.OrderID ¼ Refunds.OrderID
The only item missing in the aboveCREATE VIEWis theORDER BYclause of the originalSELECTstatement Since views aren’t stored as physical data, there is no point in including anORDER BYclause in a view
Referencing Views
When we execute the above CREATE VIEW statement, it creates a view called CustomersOrdersRefunds Creating the view does not return any data It merely defines the view for later use
To use the view to bring back the same data as before, we execute thisSELECT
statement:
SELECT *
FROM CustomersOrdersRefunds
This retrieves:
First Name Last Name Order Date Order Amt Refund Date Refund Amt
William Smith 2009-09-01 10.00 2009-09-02 5.00
Natalie Lopez 2009-09-02 12.50 NULL NULL
Natalie Lopez 2009-10-03 18.00 2009-10-12 18.00
Brenda Harper 2009-09-15 20.00 NULL NULL
What if you only wanted to see a few columns from the view for one specific customer? You could issue aSELECTstatement such as:
SELECT
[First Name],
[Last Name],
[Order Date]
FROM CustomersOrdersRefunds
WHERE [Last Name] ¼ 'Lopez'
Chapter 13 ■ Self Joins and Views
136
Trang 2The output is:
First Name Last Name Order Date
Natalie Lopez 2009-09-02
Natalie Lopez 2009-10-03
It is important to note that when you reference columns in this view, you need to
specify the column alias names that were specified when the view was created
You can no longer reference original column names For example, the view
assigns a column alias named ‘First Name’ for the Customers.FirstName
column In Microsoft SQL Server, these column aliases are enclosed in square
brackets due to the embedded spaces in the names
D A T A B A S E D I F F E R E N C E S : M y S Q L a n d O r a c l e
As discussed in Chapter 2, MySQL and Oracle use different characters around columns containing
spaces MySQL uses the accent grave ( `); Oracle uses double quotes (‘‘).
Benefits of Views
The previous example illustrates one of the important benefits of using views
Once a view is created, that view can be referenced just like it was a table Even if
the view were created from multiple tables joined together, it now appears,
logically, to be just one table
Let’s summarize the benefits of using views:
■ Views can reduce complexity First, views can simplifySELECTstatements
that are particularly complex For example, if you have aSELECTstatement
that joins six tables together, it may be useful to create views with two or
three tables each You can reference those views in aSELECTstatement that
is less complex than the original
■ Views can increase reusability If you have a situation where three tables
are always joined together, you can create a view with those three tables
Then, instead of always having to join those three tables every time you need
data from those tables, you can simply reference a predefined view
Trang 3■ Views can properly format data If you have a column that is not formatted
correctly in your database, you can use theCASTor other functions to for-mat that column exactly as you want For example, you may have a date column that is stored as an integer datatype in your database in a
YYYYMMDD format Users may prefer to view this data as a date/time column so it can be presented and used as a true date To accomplish this, a view can be created on the table, which transforms that column to the proper format Then all subsequent references to that table can reference the new view rather than the table
■ Views can create calculated columns Let’s say that you have two columns
in a table: Quantity and PricePerItem Your users are usually interested in TotalPrice data, which is found by multiplying the two columns together You can create a view of the original table easily with a new calculated col-umn that contains this calculation Users can then reference the new view and always have the calculation available
■ Views can be used to rename column names If your database contains
cryptic column names, you can create views with column aliases to translate those names into something more meaningful
■ Views can create a subset of data Let’s say you have a table with all your
customers Most of your users only need to see customers who have placed
an order during the past year You can easily create a view that has this useful subset of data
■ Views can be used to enforce security restrictions You may have a
situa-tion where you want certain users to be able to access only certain columns
in a given table To accomplish this, you can create a view of the table for those users You can then use the security features of your database to grant access to the new view for those users, while restricting them from accessing the underlying table
Modifying and Deleting Views
After a view is created, it can be modified easily by using the ALTER VIEW
statement Here’s the syntax:
ALTER VIEW ViewName AS
SelectStatement
Chapter 13 ■ Self Joins and Views
138
Trang 4When altering a view, you need to completely specify the entireSELECT
state-ment contained in the view The originalSELECT in the view gets replaced by
the newSELECTthat you specify
Let’s say that you originally created a view with this statement:
CREATE VIEW CustomersView AS
SELECT
FirstName AS 'First Name',
LastName AS 'Last Name'
FROM Customers
If you now want to modify the view to add a new column for middle name, you
can issue a statement such as:
ALTER VIEW CustomersView AS
SELECT
FirstName AS 'First Name',
MiddleName AS 'Middle Name',
LastName AS 'Last Name'
FROM Customers
Once again, creating or altering a view does not return any data It merely creates
or modifies the definition of the view
D A T A B A S E D I F F E R E N C E S : O r a c l e
Unlike Microsoft SQL Server and MySQL, the ALTER VIEW command in Oracle is more restrictive.
To accomplish the previous ALTER VIEW in Oracle, you would need to issue a DROP VIEW and
then a CREATE VIEW with the new view definition.
TheDROP VIEWstatement is used to delete a view you previously created The
syntax is:
DROP VIEW ViewName
If you want to delete the CustomersView view you created earlier, you can issue
this statement:
DROP VIEW CustomersView
Looking Ahead
Self joins and views are two different ways to view data in a virtual manner The
self join allows you to join a table to itself Views are much more flexible
Trang 5Essentially, any SELECT statement can be saved as a view, which can then be referenced like any normal table Unlike tables, views do not contain any data They merely define a new virtual view of data in existing tables Views serve a wide variety of functions, from reducing complexity to reformatting data Once created, views can be modified or deleted with the ALTER VIEW and DELETE VIEWstatements
In our next chapter, ‘‘Subqueries,’’ we are going to return to a topic more directly related to our prior discussion of how to join tables together Subqueries provide a method of relating tables to each other without making explicit use of
an inner or outer join Due to the wide variety of types of subqueries and ways in which they can be used, this is probably the most difficult but potentially rewarding subject in this book There’s actually quite a bit of flexibility as to how and when subqueries can be used As such, this is something that lends itself to a certain amount of creativity in your query designs
Chapter 13 ■ Self Joins and Views
140