Download Chapter 3 Effects of IT on Strategy and Competition

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Entity–attribute–value model wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Concurrency control wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Database wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

SQL wikipedia , lookup

Relational model wikipedia , lookup

Clusterpoint wikipedia , lookup

Database model wikipedia , lookup

PL/SQL wikipedia , lookup

Oracle Database wikipedia , lookup

Transcript
Chapter 13
Views
(up to p.519)
Jason C. H. Chen, Ph.D.
Professor of MIS
School of Business
Gonzaga University
Spokane, WA 99258 USA
[email protected]
Dr. Chen, Oracle Database System (Oracle)
1
Objectives
• Create a view by using CREATE VIEW command or
the CREATE OR REPLACE VIEW command
• Employ the FORCE and NOFORCE options
• State the purpose of the WITH CHECK OPTION
constraint
• Explain the effect of the WITH READ ONLY option
• Update a record in a simple view
• Re-create a view
Dr. Chen, Oracle Database System (Oracle)
2
Objectives (continued)
• Explain the implication of an expression in a
view for DML operations
• Update a record in a complex view
• Identify problems associated with adding records
to a complex view
• Identify the key-preserved table underlying a
complex view
• Drop a view
• Explain inline views and the use of ROWNUM
to perform a “TOP-N” analysis
• Create a materialized view to replicate data
Dr. Chen, Oracle Database System (Oracle)
3
A database view is
a logical (virtual)
table based on a
query.
Table-1
With the database view,
you can view database
records, but you can’t
insert new records or
modify or delete exiting
records.
Database Views
Table-2
Table-N
database
Single view table
Database view
Query
It does not
store data, but
presents it in a
format
different from
the one in
which it is
stored in the
underlying
tables.
Output:
Report, Graphs
Dr. Chen, Oracle Database System (Oracle)
4
Views
• Permanent objects (logical or virtual table)
no data
that store queries but ____
• Based on a source query that:
– can specify a subset of a single table’s fields or records
– can join multiple tables
• Two purposes
– Reduce complex query requirements
– Restrict users’ access to sensitive data (enforce
security; user has access to view but not
underlying table)
Dr. Chen, Oracle Database System (Oracle)
5
Creating and Using Database Views
• Views can be updateable if:
– SELECT clause contains only fieldnames, no functions
or calculations
– Can ____
not contain the ORDER BY, DISTINCT, or
GROUP BY clauses, group functions, or set operators
– search condition cannot contain a nested query
• Views are used like tables for selecting, inserting,
updating and deleting data (only updatable views can
be modified)
Dr. Chen, Oracle Database System (Oracle)
6
Database View (cont.)
SELECT view_name FROM user_views;
SELECT view_name FROM ALL_VIEWS
WHERE owner=‘SYSTEM’;
DROP VIEW <view_name>;
CREATE OR REPLACE VIEW <view_name> AS
<view query specification>;
Dr. Chen, Oracle Database System (Oracle)
7
Types of Views
Table 13-1
Overview of View Concepts
Dr. Chen, Oracle Database System (Oracle)
8
I. Creating a View
• You use the CREATE VIEW keywords to create a view
• Use OR REPLACE if the view already exists
• Use FORCE if the underlying table does not exist at the time
of creation
• Provide new column names if necessary
• WITH CHECK OPTION constraint – if used, prevents data
changes that will make the data subsequently inaccessible to
the view
• WITH READ ONLY – prevents DML operations
Figure 13-2
Dr. Chen, Oracle Database System (Oracle)
Syntax of the CREATE VIEW command
9
CREATE OR REPLACE VIEW book_vu AS
SELECT isbn, title, category
FROM books
WHERE category = 'COOKING';
SELECT *
FROM book_vu;
SQL> SELECT *
2 FROM book_vu;
ISBN
---------3437212490
0299282519
TITLE
-----------------------------COOKING WITH MUSHROOMS
THE WOK WAY TO COOK
CATEGORY
--------COOKING
COOKING
See next slide for detailed process
Dr. Chen, Oracle Database System (Oracle)
10
FROM clause in query
references a view
rather than a table
SELECT*
FROM book_vu;
BOOK_VU view
(view stored in database)
1
Stores query:
3
4
SELECT isbn, title, category
FROM books
WHERE category=‘COOKING’;
2
Processing steps:
1. A query references a view rather than a table in the FROM clause.
2. The query stored in the view is executed.
3. The results from the view query are stored in temporary storage.
4. The original query uses the temporary query results of the view as a table and
completes execution.
Dr. Chen, Oracle Database System (Oracle)
Figure 13-1 View Processing
11
DML Operations on a Simple View
• Any DML operations are allowed through simple
views unless created with WITH READ ONLY
option
• DML operations that violate constraints on the
underlying table are not allowed
-- chapter 13, Figure 13-4; p.501
CREATE VIEW inventory
AS SELECT isbn, title, retail price
FROM books
WITH READ ONLY;
SELECT *
FROM inventory;
Dr. Chen, Oracle Database System (Oracle)
-- chapter 13, Figure 13-5; p.502
CREATE VIEW region_ne
AS SELECT *
FROM customers
WHERE region = 'NE';
SELECT customer#, lastname, city, state,
region
FROM region_ne;
12
Why Failed to Update?
-- chapter 13, Figure 13-6; p.503
UPDATE inventory
SET price = 45.96
WHERE title LIKE '%POEM';
-- “SELECT” is working (READ)
SELECT *
FROM inventory
WHERE title LIKE '%POEM%';
SQL> UPDATE inventory
2
SET price = 45.96
3
WHERE title LIKE '%POEM';
SET price = 45.96
*
ERROR at line 2:
ORA-42399: cannot perform a DML
operation on a read-only view
ISBN
TITLE
PRICE
---------- ------------------ ---------2147428890 SHORTEST POEMS
39.95
SQL> UPDATE inventory
2 SET title = 'THE SHORTEST
POEMS'
UPDATE inventory
3 WHERE title LIKE '%POEM%';
SET title = 'THE SHORTEST POEMS' SET title = 'THE SHORTEST POEMS'
*
WHERE title LIKE '%POEM%';
ERROR at line 2:
ORA-42399: cannot perform a DML
Figure 13-6 Failed updates on the INVENTORY view operation on a read-only view
Dr. Chen, Oracle Database System (Oracle)
13
Combining Topics –
VIEW and NON-Equality Join
• Combing both VIEW (chapter 13) and NONEQUALITY JOINS (chapter 9; p.316)
• A non-equality join enables you to store a range's
minimum value in one column of a record and the
maximum value in another column. So instead of finding
a column-to-column match, you can use a non-equality
join to determine whether the item being shipped falls
between minimum and maximum ranges in the columns.
• If the join does find a matching range for the item, the
corresponding shipping fee (and others) can be returned
in the results
Dr. Chen, Oracle Database System (Oracle)
14
Business Scenario
• Once a year, JustLee Books offers weeklong promotion in
which customers receive a gift based on the value of each
book purchase. If a customer purchases a book with a retail
price of $12 or less, the customer receives a bookmark. If the
retail price is more than $12 but less than or equal to $25, the
customer receives a box of book-owner labels.
• For books retailing for more than $25 and less than or equal
to $56, the customer is entitled to a free book cover. For
books retailing for more than $56, the customer receives free
shipping.
SQL> SELECT * FROM promotion;
GIFT
MINRETAIL MAXRETAIL
--------------- ---------- ---------BOOKMARKER
0
12
BOOK LABELS
12.01
25
BOOK COVER
25.01
56
FREE SHIPPING
56.01
999.99
Dr. Chen, Oracle Database System (Oracle)
15
Task …
• Your job is to produce a report listing book title, publisher
name, profit and its gift (note individual gift that is based on
the retail price falls between minimal and maximal retail
prices)
• Hint:
• Because the rows in the BOOKS and PROMOTION tables
don't contain equivalent values (pk and fk), you must use a
non-equality join to determine which gift a customer
receives during the promotion.
Dr. Chen, Oracle Database System (Oracle)
16
-- First Create VIEW for ‘book’ and publisher
CREATE OR REPLACE VIEW book_profit_vu AS
SELECT b.title, pu.name Publisher_Name, b.retail, b.retail-b.cost Profit
FROM books b, publisher pu
pk
WHERE b.pubid = pu.pubid;
fk
SQL> SELECT * FROM book_profit_vu;
TITLE
-----------------------------REVENGE OF MICKEY
HOW TO MANAGE THE MANAGER
E-BUSINESS THE EASY WAY
BUILDING A CAR WITH TOOTHPICKS
HOLY GRAIL OF ORACLE
DATABASE IMPLEMENTATION
HANDCRANKED COMPUTERS
COOKING WITH MUSHROOMS
THE WOK WAY TO COOK
HOW TO GET FASTER PIZZA
BODYBUILD IN 10 MINUTES A DAY
PAINLESS CHILD-REARING
BIG BEAR AND LITTLE DOVE
SHORTEST POEMS
14Dr.rows
selected.
Chen, Oracle Database System (Oracle)
PUBLISHER_NAME
RETAIL
PROFIT
----------------------- ---------- ---------PRINTING IS US
22
7.8
PRINTING IS US
31.95
16.55
PUBLISH OUR WAY
54.5
16.6
PUBLISH OUR WAY
59.95
22.15
AMERICAN PUBLISHING
75.95
28.7
AMERICAN PUBLISHING
55.95
24.55
AMERICAN PUBLISHING
25
3.2
READING MATERIALS INC.
19.95
7.45
READING MATERIALS INC.
28.75
9.75
READING MATERIALS INC.
29.95
12.1
READING MATERIALS INC.
30.95
12.2
REED-N-RITE
89.95
41.95
REED-N-RITE
8.95
3.63
REED-N-RITE
39.95
18.1
17
-- use "NON-EQUALITY JOIN" (p.316)
SELECT title, Publisher_name, retail, p.gift, TO_CHAR(profit, '$999.99')
Profit
FROM book_profit_vu bpvu, promotion p
BETWEEN p.minretail ______
AND p.maxretail
WHERE bpvu.retail ___________
ORDER BY profit;
column title format A23
COLUMN name format A18
column proft format $999.99
column retail format $999.99
column gift format A13
SELECT title, Publisher_name, retail, p.gift, TO_CHAR(profit,
'$999.99') Profit
FROM book_profit_vu bpvu, promotion p
WHERE bpvu.retail BETWEEN p.minretail AND p.maxretail
ORDER BY profit;
Dr. Chen, Oracle Database System (Oracle)
18
TITLE
----------------------HANDCRANKED COMPUTERS
BIG BEAR AND LITTLE DOV
E
PUBLISHER_NAME
RETAIL GIFT
PROFIT
----------------------- -------- ------------- -------AMERICAN PUBLISHING
$25.00 BOOK LABELS
$3.20
REED-N-RITE
$8.95 BOOKMARKER
$3.63
COOKING WITH MUSHROOMS
REVENGE OF MICKEY
THE WOK WAY TO COOK
HOW TO GET FASTER PIZZA
BODYBUILD IN 10 MINUTES
A DAY
HOW TO MANAGE THE MANAG
ER
READING MATERIALS
PRINTING IS US
READING MATERIALS
READING MATERIALS
READING MATERIALS
INC.
INC.
INC.
INC.
BOOK
BOOK
BOOK
BOOK
BOOK
LABELS
LABELS
COVER
COVER
COVER
$7.45
$7.80
$9.75
$12.10
$12.20
$31.95 BOOK COVER
$16.55
E-BUSINESS THE EASY WAY PUBLISH OUR WAY
SHORTEST POEMS
REED-N-RITE
BUILDING A CAR WITH TOO PUBLISH OUR WAY
THPICKS
$54.50 BOOK COVER
$39.95 BOOK COVER
$59.95 FREE SHIPPING
$16.60
$18.10
$22.15
DATABASE IMPLEMENTATION AMERICAN PUBLISHING
HOLY GRAIL OF ORACLE
AMERICAN PUBLISHING
PAINLESS CHILD-REARING REED-N-RITE
$55.95 BOOK COVER
$75.95 FREE SHIPPING
$89.95 FREE SHIPPING
$24.55
$28.70
$41.95
14 rows selected.
Dr. Chen, Oracle Database System (Oracle)
PRINTING IS US
$19.95
$22.00
$28.75
$29.95
$30.95
GIFT
MINRETAIL MAXRETAIL
--------------- ---------- ---------BOOKMARKER
0
12
BOOK LABELS
12.01
25
BOOK COVER
25.01
56
FREE SHIPPING
56.01
999.99
19
A Version without VIEW
--1c.(TRADITIONAL JOIN; modified version from p.316)
SELECT b.title, pu.name "Publisher Name", b.retail, p.gift,
b.retail-b.cost proft
FROM books b, publisher pu, promotion p
WHERE b.pubid = pu.pubid
AND b.retail BETWEEN p.minretail AND p.maxretail
ORDER BY b.retail-b.cost;
Dr. Chen, Oracle Database System (Oracle)
20
DML Operations on a Simple View (continued)
-- chapter 13, Figure 13-7; p.504
CREATE OR REPLACE VIEW inventory
AS SELECT isbn, title, retail price
-- chapter 13, Figure 13-8; p.505
FROM books;
UPDATE inventory
SET price = 49.95
SELECT *
WHERE title LIKE '%BODYBUILD%';
FROM inventory
WHERE title LIKE '%BODYBUILD%';
SELECT *
FROM inventory
WHERE title LIKE '%BODYBUILD%';
SELECT *
FROM books
WHERE title LIKE '%BODYBUILD%';
Dr. Chen, Oracle Database System (Oracle)
21
Rule for DML operations
• As long as the view isn’t created with the WITH
READ ONLY option and any DML operation is
allowed if it doesn’t violate an existing constraint
on the underlying table.
• You can add, modify, and even delete data in an
underlying table as long as one of the following
constraints doesn’t prevent the operation:
– PRIMARY KEY - FOREIGN KEY
– UNIQUE
- NOT NULL
– WITH CHECK OPTION
Dr. Chen, Oracle Database System (Oracle)
Practice Figure 13-9 to
13-11 (pp.506-507)
22
Exercises
• Practice all the examples in the text.
• A Script file is available on the Bb (file
name: ch13Queries.sql)
• After completing all examples, do the HW.
In-class Exercise
• #1
Dr. Chen, Oracle Database System (Oracle)
--1.
CREATE VIEW contact
AS SELECT contact, phone
FROM publisher;
23
Homework - Hands-On Assignments
Upload the source (*.sql) and spooled file (*.txt) to the Bb
(under “Assignments & Projects”) by the deadline.
Read and Practice all examples on Chapters 13
• 1. Read Oracle assignment and create a script
file Oracle_ch13_Lname_Fname.sql for
questions (#1-5 and 9,10; p.537) on “Handson Assignments”.
• 2. Execute and test one problem at a time and
make sure they are all running successfully.
• 3. When you done, spool the script files (see
next slide for spooling instructions) and
UPLOAD the file
(Oracle_ch13_Spool_Lname_Fname.txt) to
me by the midnight Sunday.
• 4. Turn in a hardcopy next class.
Dr. Chen, Oracle Database System (Oracle)
24
How to Spool your Script and Output Files
After you tested the script file of Oracle_ch13_Lname_Fname.sql
successfully, follow the instructions below to spool both script and output
files:
Step 0. Run the following script file from SQL*Plus (since you have created
JLDB tables)
• 1. type the following on SQL>
– Spool c:\oradata\Oracle_ch13B_Spool_Lname_Fname.txt (make sure your name is
entered)
• 2. open Oracle_ch13_Lname_Fname.sql that you already tested
• 3. copy and paste all the SQL commands (including all comments) to the
SQL*PLUS
• 4. type Spool Off on the SQL>
The output should contain your personal information, all SQL commands and
their solution on the .txt file and saved in C: drive (oradata\ folder)
UPLOAD script and spooled files (*.sql and *.txt)
Dr. Chen, Oracle Database System (Oracle)
25
• PART II
Dr. Chen, Oracle Database System (Oracle)
26