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
224,07 KB
Nội dung
CreatingViews 14 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder14Ć2 Schedule: Timing Topic 35 minutes Lecture 30 minutes Practice 65 minutes Total Class Management Note: Files required for this lesson are: Demonstration: l14emp.sql, l14vu1.sql, l14vu2.sql Practice: None 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.” Class Management Note: Use the graphic on the facing page to illustrate that a view is often a subset of columns, or rows, from an existing table. A view of the S_EMP table might only include the ID, LAST_NAME, FIRST_NAME, and PHONE columns, thereby hiding other information. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder14Ć6 Technical Note: Privileges can be granted on views. No ORDER BY clause is allowed in the CREATE VIEW statement, but an ORDER BY clause can be used to query from the view. Users can define a view in terms of another view. 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... 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 the user SQL> DESCRIBE user _views Name Null? - -VIEW_NAME NOT NULL TEXT_LENGTH TEXT SQL> SELECT 2 FROM Type -VARCHAR2(30) NUMBER LONG * user _views; VIEW_NAME TEXT_LENGTH... 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... Management Note: DEMO: l14emp.sql PURPOSE: Demonstrate creating the view by using the aliases in the CREATE VIEW clause Display the structure of the view and the view contents Point out that to display any particular column, reference the alias name for that column CreatingViews 14Ć11 14Ć12 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Creating a View continued Create a complex view that... 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 ... 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... name in the form SYS_Cn, where n is an integer that makes the constraint name unique within the system CreatingViews 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... 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 Class Management Note: Duration: 30 minutes 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... where: view CreatingViews 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... last_name, title FROM s_emp WHERE dept_id = 45 WITH READ ONLY; View created SQL> DELETE FROM 2 WHERE empvu45 id_number = 10; ERROR at line 1: ORA-01732: data manipulation operation not legal on this view CreatingViews 14Ć19 Class Management Note: Because the TEXT column is a LONG datatype column, you see the first 80 characters in the TEXT column You can use SQL*Plus SET command to influence the display The . 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?. l14emp.sql, l14vu1.sql, l14vu2.sql Practice: None Creating Views 14Ć3 Objectives In this lesson, you will see how views can be used to present data to users in