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

creating views

30 247 0
Tài liệu đã được kiểm tra trùng lặp

Đ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

Thông tin cơ bản

Tiêu đề Creating Views
Trường học Oracle Corporation
Chuyên ngành Database Management
Thể loại Giáo trình
Định dạng
Số trang 30
Dung lượng 199,72 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 14Ć2... Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 14Ć4... Introduction to Oracle: SQL and PL/SQL Using

Trang 1

Creating Views

14

Trang 2

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 14Ć2

Trang 3

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

Trang 4

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 14Ć4

Trang 5

D Provide data independence for ad hoc users and application programs One viewcan 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.”

Trang 6

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 14Ć6

Trang 7

[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

alias specifies names for the expressions selected by

the view’s query The number of aliases mustmatch the number of expressions selected by theview

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, includingjoins, 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 previouslygranted on it

Trang 8

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 14Ć8

Trang 9

Creating a View continued

There are two classifications for views: simple and complex The basic difference isrelated to the DML operations

Simple Views Compared to Complex Views

Characteristic Simple Views Complex Views

Contain groups of data (DISTINCT

or group functions)

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

10 Havel Warehouse Manager

24 Dancs Stock Clerk

25 Schwartz Stock Clerk

Trang 10

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 14Ć10

Trang 11

Creating a View continued

Control the column names by including column aliases within the subquery

Example

Create a view containing the employee number, first name with the alias FIRST, lastname with the alias LAST, and salary with the alias MONTHLY_SALARY for

department 41

SQL> CREATE VIEW salvu41

2 AS SELECT id, first_name FIRST, last_name LAST,

Modify EMPVU45 view Change the employee number to have a heading

ID_NUMBER, last name to a heading EMPLOYEE, and title to a heading JOB

SQL> CREATE OR REPLACE VIEW empvu45

2 (id_number, employee, job)

3 AS SELECT id, last_name, title

4 FROM s_emp

5 WHERE dept_id = 45;

View 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

Trang 12

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 14Ć12

Trang 13

Creating a View continued

Create a complex view that contains group functions to display values from twotables

Example

Create a view of the department names, minimum salary, maximum salary, and

average salary by department Display the structure of the view and its contents

SQL> CREATE VIEW dept_sum_vu

2 (name, minsal, maxsal, avgsal)

3 AS SELECT d.name, min(e.salary),

4 max(e.salary), avg(e.salary)

5 FROM s_emp e, s_dept d

6 WHERE e.dept_id = d.id

Trang 14

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 14Ć14

Trang 15

Performing DML Operations on a View

You can perform DML operations on data through a view provided those operationsfollow the rules outlined below:

D You can remove a row from a view unless it contains any of the following:

D Group functions

D A GROUP BY clause

D The DISTINCT command

D You can modify data in a view unless it contains any of the above and any of thefollowing:

D Columns defined by expressions, for example, SALARY * 12

D The ROWNUM pseudocolumn

D You can add data through a view unless it contains any of the above and there areNOT NULL columns in the base table that are not selected by the view Allrequired values must be present in the view Remember that you are adding values

directly into the underlying table through the view.

For more information, see

Oracle 7 Server SQL Reference, Release 7.3, “CREATE VIEW.”

Trang 16

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 14Ć16

Trang 17

Performing DML Operations on a View continued

You can ensure that when you add or update data in a simple view, the added orupdated data can be queried through the view

Note: No rows are updated because if the department number were to change to 42,

the view would no longer be able to see that employee Therefore, with theWITH CHECK OPTION clause, the view can only see department 41

employees, and does not allow the department number for those employees to

be changed through the view

Trang 18

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 14Ć18

Trang 19

Performing DML Operations on a View continued

You can ensure that no DML operations occur on your view by creating it with theWITH READ ONLY option

Example

Modify the EMPVU45 view Do not allow DML operations to occur on this view.Attempt to remove a row from the view

SQL> CREATE OR REPLACE VIEW empvu45

2 (id_number, employee, job)

3 AS SELECT id, last_name, title

Trang 20

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 14Ć20

Trang 21

Confirming View Names and Structures

You can confirm the names and defining SELECT statements of views by queryingthe USER_VIEWS data dictionary table

FROM s_emp e, s_dept

EMPVU41 179 SELECT ”ID”,”LAST_NAME”,”FIRST_NAM E”,”USERID”,”START_DATE”,”COMMENTS” ,”MANAGER_

Trang 22

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 14Ć22

Trang 23

Removing a View

Use the DROP VIEW command to remove a view The command removes the viewdefinition from the database Dropping views has no affect on the tables on which theview was based Views or other applications based on deleted views become invalid.Only the creator or a user with the DROP ANY VIEW privilege can remove a view.Syntax

where: view is the name of the view

Trang 24

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 14Ć24

Trang 25

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 ofthe view is stored in the data dictionary You can see the definition of the view in theUSER_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 than one table, or contain groups orfunctions

D Can be replaced if one of the same name exists

D Contain a check constraint

D Can be read-only

Trang 26

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 14Ć26

Trang 27

Practice Overview

In this practice, you will create simple and complex views, and attempt to performDML 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 modify data in the view

D Displaying view definitions

D Removing views

Trang 28

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 14Ć28

Trang 29

Practice 14

1. Create a view called EMP_VU based on the employee number, last name, anddepartment number from the WORKER table Change the heading for the lastname to EMPLOYEE

a. Display the content of the EMP_VU view

b. Write a script to display the definition of a view Pass the name of the view to

the script Save the script as p14q1.sql Execute the script to view the

definition of the EMP_VU view

c. Change the department number for Smith to 37 in the EMP_VU view

d. Confirm that Smith is now assigned to department 37

2. Create a view called MNS_VU that contains the employee number, full name, anddepartment name for all employees in the Marketing and Sales departments in theWORKER and DEPARTMENT tables

a. Display the structure and contents of the MNS_VU view

b. Display the definition of the MNS_VU view by executing the p14q1.sql

script

c. Display the department name and number of employees in each department

3. Modify EMP_VU view contain only those employees in department 37 Add acheck constraint so that the department number cannot be modified

a. Display the contents of the EMP_VU view

b. Change the department number for Smith back to 54 through the EMP_VUview Was your operation successful? Why or why not?

If you have time, complete the following exercises:

4. Modify the MNS_VU so that the 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 thedata dictionary

Trang 30

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 14Ć30

Ngày đăng: 16/10/2013, 13:15

Xem thêm

TỪ KHÓA LIÊN QUAN

w