1. Trang chủ
  2. » Công Nghệ Thông Tin

The Language of SQL- P31 docx

5 244 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

FROM Customers LEFT JOIN Orders ON Customers.CustomerID ¼ Orders.CustomerID LEFT JOIN Refunds ON Orders.OrderID ¼ Refunds.OrderID The only item missing in the above CREATE VIEW is the ORDER BY clause of the original SELECT statement. Since views aren’t stored as physical data, there is no point in including an ORDER BY clause 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 this SELECT 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 Adam Petrie NULL NULL NULL NULL What if you only wanted to see a few columns from the view for one specific customer? You could issue a SELECT statement such as: SELECT [First Name], [Last Name], [Order Date] FROM CustomersOrdersRefunds WHERE [Last Name] ¼ 'Lopez' Chapter 13 ■ Self Joins and Views136 The 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. DATABASE DIFFERENCES: MySQL and Oracle 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 simplify SELECT statements that are particularly complex. For example, if you have a SELECT statement that joins six tables together, it may be useful to create views with two or three tables each. You can reference those views in a SELECT statement 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. Benefits of Views 137 ■ Views can properly format data. If you have a column that is not formatted correctly in your database, you can use the CAST or 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 Views138 When altering a view, you need to completely specify the entire SELECT state- ment contained in the view. The original SELECT in the view gets replaced by the new SELECT that 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. DATABASE DIFFERENCES: Oracle 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. The DROP VIEW statement 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. Looking Ahead 139 Essentially, 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 VIEW statements. 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 tha t lends itself to a certain amo unt of creativity in your query designs. Chapter 13 ■ Self Joins and Views140 . 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. 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. 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

Ngày đăng: 05/07/2014, 05:20

Xem thêm: The Language of SQL- P31 docx

Mục lục

    Chapter 1 Relational Databases and SQL

    Microsoft SQL Server, Oracle, and MySQL

    Primary and Foreign Keys

    The Significance of SQL

    Chapter 2 Basic Data Retrieval

    Column Names with Embedded Spaces

    Chapter 3 Calculations and Aliases

    The Function of Functions

    Sorting in Ascending Order

    Sorting in Descending Order

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN