Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 30 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
30
Dung lượng
199,72 KB
Nội dung
CreatingViews 14 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder14Ć2 CreatingViews 14Ć3 Objectives In this lesson, you will see how views can be used to present data to users in a variety of ways. In addition, you will see how integrity constraints can be enforced, if using a view to insert, update, or delete data. At the end of this lesson, you should be able to D Explain the concept of a view. D Use data dictionary views. D Create simple and complex views. D Create a view with an option to enforce constraints. D Modify a view. D Remove a view. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder14Ć4 CreatingViews 14Ć5 Overview Present logical subsets or combinations of data by creatingviews of tables. What Is a View? A view is a logical table based on a table or another view. A view contains no data of its own, but is rather like a “window” through which data from tables can be viewed or changed. The tables upon which a view is based are called base tables. The view is stored as a SELECT statement in the data dictionary. Advantages of Views D Restrict access to the database because the view can display a selective portion of the database. D Allow users to make simple queries to retrieve the results from complicated queries. For example, views allow users to query information from multiple tables without knowing how to write a join statement. D Provide data independence for ad hoc users and application programs. One view can be used to retrieve data from several tables. D Provide groups of users access to data according to their particular criteria. For more information, see Oracle7 Server SQL Reference, Release 7.3, “CREATE VIEW.” Introduction to Oracle: SQL and PL/SQL Using Procedure Builder14Ć6 CreatingViews 14Ć7 Creating a View Create a view by embedding a subquery within the CREATE VIEW statement. Abridged Syntax CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW view [(alias[, alias] .)] AS subquery [WITH CHECK OPTION [CONSTRAINT constraint]] [WITH READ ONLY] where: OR REPLACE recreates the view if it already exists. FORCE creates the view regardless of whether the base tables exist or not. NOFORCE creates the view only if the base tables exist. This is the default. view is the name of the view. alias specifies names for the expressions selected by the view’s query. The number of aliases must match the number of expressions selected by the view. subquery is a complete SELECT statement. You can use aliases for the columns in the SELECT list. WITH CHECK OPTION specifies that only rows accessible to the view may be inserted or updated. constraint is the name assigned to the CHECK OPTION constraint. WITH READ ONLY ensures that no DML operations can be performed on this view. Guidelines D The query that defines a view can contain complex SELECT syntax, including joins, groups, and subqueries. D The query that defines the view cannot contain an ORDER BY clause. D If you do not specify a constraint name, the system will assign a default name in the format SYS_Cn. D You can use the OR REPLACE option to change the definition of the view without dropping and re-creating it, or regranting object privileges previously granted on it. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder14Ć8 CreatingViews 14Ć9 Creating a View continued There are two classifications for views: simple and complex. The basic difference is related to the DML operations. Simple Views Compared to Complex Views Characteristic Simple Views Complex Views Number of tables Only one One or more Contain functions No Yes Contain groups of data (DISTINCT or group functions) No Yes DML through the view Yes No Example Create a view containing the employee number, last name, and job title for employees in department 45. Display the contents. SQL> CREATE VIEW empvu45 2 AS SELECT id, last_name, title 3 FROM s_emp 4 WHERE dept_id = 45; View created. SQL> SELECT * 2 FROM empvu45; ID LAST_NAME TITLE ------ ------------ --------------------- 10 Havel Warehouse Manager 24 Dancs Stock Clerk 25 Schwartz Stock Clerk Introduction to Oracle: SQL and PL/SQL Using Procedure Builder14Ć10 [...]... exists D Contain a check constraint D Can be read-only CreatingViews 14Ć25 14Ć26 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Practice Overview In this practice, you will create simple and complex views, and attempt to perform DML statements to the views Practice Contents D Creating a simple view D Creating a complex view D Creating a view with a check constraint D Attempting to... operation not legal on this view CreatingViews 14Ć19 14Ć20 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Confirming View Names and Structures You can confirm the names and defining SELECT statements of views by querying the USER _VIEWS data dictionary table Example Describe the structure of the USER _VIEWS data dictionary table Display the names and contents of all views currently owned by... last, salary monthly_salary FROM s_emp WH CreatingViews 14Ć21 14Ć22 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Removing a View Use the DROP VIEW command to remove a view The command removes the view definition from the database Dropping views has no affect on the tables on which the view was based Views or other applications based on deleted views become invalid Only the creator or... rows can only be seen between 1:00 P.M and 4:00 P.M You can edit the script named p14q1.sql Execute the script Display the contents of the view 5 Remove all views from the data dictionary Confirm that no views exist in the data dictionary CreatingViews 14Ć29 14Ć30 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder ... where: view Creating Views view; is the name of the view 14Ć23 14Ć24 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Summary A view is based on a table or another view and acts as a window through which data on tables can be viewed or changed A view does not contain data The definition of the view is stored in the data dictionary You can see the definition of the view in the USER _VIEWS data... of the view is stored in the data dictionary You can see the definition of the view in the USER _VIEWS data dictionary table Advantages of Views D Restrict database access D Simplify queries D Provide data independence D Allow multiple views of the same data D Remove views without affecting the underlying data View Options D Can be a simple view based on one table D Can be a complex view based on more... created Note: When assigning column aliases in the CREATE VIEW clause, remember that the aliases are listed in the same order as the columns in the subquery Creating Views 14Ć11 14Ć12 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Creating a View continued Create a complex view that contains group functions to display values from two tables Example Create a view of the department names,... views Practice Contents D Creating a simple view D Creating a complex view D Creating a view with a check constraint D Attempting to modify data in the view D Displaying view definitions D Removing views CreatingViews 14Ć27 14Ć28 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Practice 14 1 Create a view called EMP_VU based on the employee number, last name, and department number from the... employees, and does not allow the department number for those employees to be changed through the view Creating Views 14Ć17 14Ć18 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Performing DML Operations on a View continued You can ensure that no DML operations occur on your view by creating it with the WITH READ ONLY option Example Modify the EMPVU45 view Do not allow DML operations... NUMBER NUMBER NUMBER * dept_sum_vu; NAME MINSAL MAXSAL AVGSAL -Administration 1550 2500 2025 Finance 1450 1450 1450 Operations 750 1450 1086.8 Sales 795 1525 1367.85714 Creating Views 14Ć13 14Ć14 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Performing DML Operations on a View You can perform DML operations on data through a view provided those operations . Creating Views 14 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder14Ć2 Creating Views 14Ć3 Objectives In this. PL/SQL Using Procedure Builder14Ć4 Creating Views 14Ć5 Overview Present logical subsets or combinations of data by creating views of tables. What Is a View?