Tài liệu Oracle SQL Jumpstart with Examples- P10 docx

50 491 0
Tài liệu Oracle SQL Jumpstart with Examples- P10 docx

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

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

420 18.7 The Recycle Bin can also add a comment to the end of a line of code by placing the hyphens after all executable code.  Single-Line Comments. Precede a line with the REM or REMARK keyword to mark that line as a comment. The following query demonstrates all three types of comments. See the result in Figure 18.28. REM This query looks for Artists with REM a letter "a" in their names. SELECT /* Ignore this line and this line and this line too */ NAME this is the FROM clause FROM ARTIST WHERE NAME LIKE '%a%' and this is the WHERE clause ; In a color graphic, the comments would be highlighted in red within SQL*Plus Worksheet. This makes them much easier to see. I have high- lighted commented sections by boxing them, as shown in Figure 18.28. The penultimate section in this chapter on tables will examine the recy- cle bin, which is newly introduced in Oracle Database 10g. 18.7 The Recycle Bin Oracle Database 10g introduces a recycle bin. This feature is certainly use- ful from the perspective of database administration, but it could cause problems with space and perhaps performance if the recycle bin is not regu- larly monitored and cleared. There are several changes to various DDL commands associated with the recycle bin as listed below. The syntax dia- gram in Figure 18.29 shows generally applicable recycle bin syntax.  The DROP TABLE command now requires a PURGE option if an object is not to be retained in the recycle bin. Chap18.fm Page 420 Thursday, July 29, 2004 10:13 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 18.8 Metadata Views 421 Chapter 18  The new PURGE command is required to allow clearing the recycle bin.  The Oracle Database 10g SQL Reference Manual states that the FLASHBACK TABLE command is used to recover a table from the recycle bin. This chapter concludes with a short section on database metadata as directly applicable to this chapter. 18.8 Metadata Views This section lists metadata views allowing access into the structural details of tables. Chapter 19 describes the basis and detail of Oracle Database metadata views.  USER_TABLES. Table structural definitions.  USER_TAB_COLS and USER_TAB_COLUMNS. Table column definitions where USER_TAB_COLS includes hidden columns. Figure 18.28 Commenting SQL Code. Chap18.fm Page 421 Thursday, July 29, 2004 10:13 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 422 18.8 Metadata Views  USER_TAB_COMMENTS and USER_COL_COMMENTS. Comments on table columns.  USER_UNUSED_COL_TABS. This view shows columns in tables marked as SET UNUSED and not physically dropped from tables.  DBA_RECYCLEBIN and USER_RECYCLEBIN. These two metadata views represent all recycle bins for all users and the specific connected user recycle bin. The RECYCLEBIN view is often referred to and is simply a synonym for the USER_RECYCLEBIN view. See Chapter 22 for details on synonyms.  USER_OBJECT_TABLES. Object type table structures.  USER_TAB_PARTITIONS and USER_TAB_SUBPARTITIONS. Table partition and subpartition structures.  USER_PART_TABLES. Table partitioning details of tables at the partition rather than the table level, as is the case for the USER_TAB_PARTITIONS and USER_TAB_SUBPARTITIONS views. Figure 18.29 Syntax for the Recycle Bin. Chap18.fm Page 422 Thursday, July 29, 2004 10:13 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 18.9 Endnotes 423 Chapter 18 The script executed in Figure 18.30 matches all tables and table col- umns for the currently logged-in user. This gives an example of the power of metadata views. The script is included in Appendix B. That covers all we want to cover about tables at present. Chapter 20 cov- ers constraints and partially returns to the subject of tables. The next chap- ter looks at views. 18.9 Endnotes 1. Oracle Performance Tuning for 9i and 10g (ISBN: 1-55558-305-9) Figure 18.30 Querying USER_TABLES and USER_TAB_COL UMNS. Chap18.fm Page 423 Thursday, July 29, 2004 10:13 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. This page intentionally left blank Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 425 19 Views In this chapter:  What is a view?  What types of views are available?  How do we use views?  What are metadata views? There are various examples of views in other chapters. This chapter describes views in detail. A view is an overlay onto one or more other data sources. A data source can be a table, view, or multiples thereof. 19.1 What Is a View? Imagine that you are working in an insurance company, where part of your job duties are to help users who have trouble writing queries for reports. The users have had basic training in writing SQL queries, but they often get stuck when they must join many tables or use subqueries. If you could set up the join or subquery ahead of time, then the users would have no trou- ble adding to it to refine their report requirements. This is one of the best reasons to create a view. Note: This approach can, however, be bad for performance because the entire query in the view will always be executed, whatever filtering is placed on a query against a view. 1 A view is a query that is stored in the database and executed to create a virtual table. A view is given a name, is owned by a schema, and is executed Chap19.fm Page 425 Thursday, July 29, 2004 10:13 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 426 19.2 Types and Uses of Views whenever a query or other SQL command uses the view. The tables refer- enced in the view’s query are called base tables . Views do not contain any data of their own, and therefore do not require storage. Views belong to a schema, and you can grant privileges such as SELECT, INSERT, UPDATE, and DELETE on views, even if the user does not have any privileges on the base table(s) used in the view. Views are most often used for security purposes and as an aid to query- ing the database; however, some views can be used to insert, update, and delete data in the underlying table. 19.2 Types and Uses of Views Here are some of the more common reasons for creating a view:  Security . Create a view with a limited subset of the rows and/or col- umns in a table or tables and give the user permission to use the view, but not the base tables.  Simplicity . Create a view that combines tables that have complex relationships so users writing queries do not need to understand the relationships.  Complex Joins . Sometimes queries cannot be done without great difficulty unless you create a view in something like a temporary table first. For example, you can create a view with a GROUP BY clause that summarizes data. You can join that summary data with other tables only by using a view.  Materialized Views . This is not a view as such because the data in the view is physically stored in the materialized view, thus the term materi- alized . Materialized views are a little too specialized for this book. Regardless of why a view is created, it falls into one of three basic catego- ries or types of views:  Simple View . A simple view contains a query on a single table. For example, a view that lists the names, addresses, and zip codes of all artists in the USA is a simple view because only the ARTIST table is queried in the view. Simple views can be used to narrow the focus or visible data window of a specific user from the entire table to a subset Chap19.fm Page 426 Thursday, July 29, 2004 10:13 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 19.3 CREATE VIEW Syntax 427 Chapter 19 of the rows or a subset of the columns. The best explanation for this type of view is security where, for instance, different customers shar- ing the same database can only view their own data. With a few restrictions (examined later in this chapter), you can update the table on which the view is built by updating the view. You can also insert and delete rows in the base table through the view.  Constraint View . A constraint view can be used to insert a new row into the underlying table as long as the row would be returned by the query, or the row exists for the view. For example, if the view only looks at ARTIST rows in the USA, you could not insert an ARTIST row where the artist is in France. The same rule applies to rows that are updated via the constraint view. Most constraint views are based on simple views, although certain complex views can also be used as constraint views. Constraint views are most often used as an easy way to enforce business rules in applications without the application developer doing any extra coding. Note: This approach applies views to ease of application coding rather than security. Views are possibly more applicable in client-server environments. Scalability issues may arise for large, very busy OLTP databases.  Complex View . A complex view contains a query on more than one table. This type of view allows you to wrap complexities inside the view’s query so they are hidden from the user or application devel- oper. Complex views are most often used for simplifying end-user reporting by providing a table-like structure for users to query. For example, you could create a view that displays the CD title, artist name, and song title (which are found in three different tables). Complex views usually cannot be used to insert, update, or delete rows from the underlying tables. 19.3 CREATE VIEW Syntax Figure 19.1 shows the syntax of the CREATE VIEW statement. The same syntax applies to all types of views. The next three sections look at how to create each of the three types of views: simple, constraint, and complex. Chap19.fm Page 427 Thursday, July 29, 2004 10:13 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 428 19.3 CREATE VIEW Syntax 19.3.1 Creating Simple Views A simple view is the easiest type of view to create. CREATE VIEW USARTISTS AS SELECT ARTIST_ID, NAME, CITY, STATE_PROVINCE, ZIP FROM ARTIST WHERE COUNTRY = 'USA'; The view we have just created can be queried as if it were a table. When you execute a query on a view, the entire query contained within the view definition is executed to retrieve the columns and rows of the subquery, Then your query is applied to the view and the final results are displayed. Query the view by executing these format commands and query. Figure 19.2 shows the result. COLUMN NAME FORMAT A20 COLUMN CITY FORMAT A15 COLUMN STATE_PROVINCE FORMAT A10 SELECT * FROM USARTISTS; You can use a view in a join as if it were another table. For example, you can list all U.S. artist names and their song titles with this query: SELECT NAME, TITLE FROM USARTISTS NATURAL JOIN SONG ORDER BY 1,2; Figure 19.1 CREATE VIEW Syntax Is Fairly Simple. Chap19.fm Page 428 Thursday, July 29, 2004 10:13 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 19.3 CREATE VIEW Syntax 429 Chapter 19 Now let’s take the simple view and add a constraint clause. The result will be a constraint view. 19.3.2 Creating Constraint Views A simple view usually allows you to update data in the underlying table through the view. You will examine this capability later in this chapter. There is a problem that sometimes crops up when using views to insert or update data: You can create a record that does not fit the view’s query and therefore does not appear in the view. For example, imagine that you use the USARTISTS view to update the country from USA to Canada for one of the artists. You want to check the results, but querying the view no longer displays the record. It is as if the record disappeared after you updated it. Obviously, the record is in the table and simply is not displayed in the view. However, this fact may not be obvious to other users who are not familiar with the query that is used by the view. To prevent users from updating or inserting records not fitting within the view, you create a constraint view. Another good reason to use a con- straint view is that it provides a form of security. Views are frequently used to limit a user’s access to certain rows and columns within the base table. The user should not be able to update rows not appearing in the view, but Figure 19.2 Querying a View Is Just Like Querying a Table. Chap19.fm Page 429 Thursday, July 29, 2004 10:13 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. [...]... Behind the scenes, however, the Oracle Database 10g Optimizer merges the query that defines Chapter 19 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 436 19.5 Working with Views Figure 19.7 A Query Combined with the View’s Query the view with the query that uses the view, into a single query This query is parsed and stored in the shared SQL memory Then the query is executed... tried to update one of the rows in the view with a country other than USA Now, let’s look at some more interesting things you can do using complex views 19.3.3 Creating Complex Views Complex views have more than one base table Complex views include a wide variety of queries Two common ones are views with joins and views with inline subqueries 19.3.3.1 Views with Joins Let’s dive right in by creating... it with the WITH READ ONLY clause as in the following example: CREATE OR REPLACE VIEW OLDMUSIC_VIEW AS SELECT MUSICCD_ID, TITLE CDNAME , PRESSED_DATE, PLAYING_TIME FROM MUSICCD WHERE PRESSED_DATE < '01-JUL-01' WITH READ ONLY; Simple views and simple constraint views are really not too much different from inserting into the base table itself, simply having more limitations 19.5.2.1 DML and Views with. .. CREATE VIEW Syntax without the constraint clause, this could happen and could be a violation of your business rules Create a constraint view that looks like the simple view, except it includes the WITH CHECK OPTION clause, by running the following command: CREATE VIEW CONSTRAINT_USARTISTS AS SELECT ARTIST_ID, NAME, CITY, STATE_PROVINCE, ZIP, COUNTRY FROM ARTIST WHERE COUNTRY = 'USA' WITH CHECK OPTION... GUESTAPPEARANCE GA ON (S.SONG_ID = GA.SONG_ID); We would like to see the name of the artist making a guest appearance The following query joins the view with the ARTIST table, which would become a very complex query without the view The complexity is simply passed on to Oracle Database, potentially hurting performance in larger, busier environments Figure 19.8 shows part of the result SELECT V.ARTIST, V.TITLE,... the base table or tables referenced by the view 19.5 Working with Views Most views are used to query the base tables on which they are built Some views are used to insert, update, and delete data in the base tables The next sections show you how to query views and how to make changes to data using views Following this section, we deal with Oracle Database metadata data dictionary views 19.5.1 Querying... Performance views are generally named as V$name V$ views store and track all types of performance statistics and data in the database Performance views relate to tuning an Oracle Database2 and are largely out of the scope for this book on Oracle SQL All of the metadata views overlay and access the metadata from system tables stored either in the SYS, SYSTEM, or SYSAUX schemas The database system tables are... OBJECT_TYPE FORMAT A24 HEADING "Type" SELECT OBJECT_NAME, OBJECT_TYPE, STATUS FROM USER_OBJECTS ORDER BY 1,2; In the current release of Oracle Database 10g, the query SELECT COUNT(*) FROM DICTIONARY yields a count of 600 data dictionary metadata and performance views As you work with Oracle Database, you will learn more about at least some of these views This chapter has described views, changing views, and... remove this watermark 19.8 Endnotes 445 Figure 19.11 The USER_OBJECTS Metadata View Oracle Database metadata data dictionary views was also discussed The next chapter examines constraints, which can be placed on both tables and views 19.8 Endnotes 1 Oracle Performance Tuning for 9i and 10g (ISBN: 1-55558-305-9) 2 Oracle Performance Tuning for 9i and 10g (ISBN: 1-55558-305-9) Chapter 19 Please purchase... using the CREATE TABLE statement Let’s experiment with different types of constraints Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 20.2 Managing Constraints 451 Figure 20.2 CREATE TABLE with Detailed Constraints Syntax 20.2.1.1 Primary Key and Unique Constraints A unique constraint forces a column value to be unique within all rows of a table A primary key is forced . variety of queries. Two common ones are views with joins and views with inline subqueries. 19.3.3.1 Views with Joins Let’s dive right in by creating. 19.5 Working with Views the view with the query that uses the view, into a single query. This query is parsed and stored in the shared SQL memory. Then

Ngày đăng: 21/01/2014, 18:20

Từ khóa liên quan

Mục lục

  • Oracle SQL : Jumpstart with Examples

    • Cover

    • Table of Contents

    • Foreword

    • Preface

    • Acknowledgements

    • 1 Introduction to Oracle SQL

    • 2 New Features of Oracle SQL

    • 3 Oracle Database Architecture

    • 4 The SELECT Statement

    • 5 Filtering Rows

    • 6 Sorting Rows

    • 7 Operators, Conditions, and Pseudocolumns

    • 8 Using SQL*Plus

    • 9 Single-Row Functions

    • 10 Joining Tables

    • 11 Grouping and Summarizing Data

    • 12 Subqueries

    • 13 Unusual Query Types

    • 14 Expressions

    • 15 Data Manipulation Language (DML)

Tài liệu cùng người dùng

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

Tài liệu liên quan