Chapter 8 - Personal.kent.edu

Download Report

Transcript Chapter 8 - Personal.kent.edu

Database Processing
Structured Query
Language III
Chapter 7/8
David M. Kroenke
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
4-1
A View
• A view is a structured list of data
attributes from the entities or semantic
objects defined in the data model
• A view is a virtual table whose contents
are defined by a query
• A view can be materialized or formatted
as an on-line form or a hard-copy report
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
4-2
Why Views?
1.
2.
3.
4.
5.
Security
User authorized access
Query Simplicity
Multi-table queries become single-table results
Structural Simplicity
User personalized access
Insulation from Change
Presents consistent image of DB
Data Integrity
Data access, entry via view can be checked via DBMS
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
4-3
Why Not?
1. Performance
2. Update Restrictions
3. Views are not backups!
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
4-4
Creating Views
•
•
•
Must have permission
If columns specified, query select must
match
Data type, length & constraints derived
from source tables
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
4-5
Types of Views
•
•
•
•
•
Horizontal
Vertical
Row/Column Subset
Grouped
Joined
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
4-6
Horizontal Views
CREATE VIEW viewname AS query;
Show Eastern region salespeople
DROP VIEW eastreps;
CREATE VIEW eastreps AS
select * from salesreps
where rep_office in
( select office from offices
where region = ‘Eastern’);
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
4-7
Joined Views
Show the orders and amounts, but with names of
employees & customers instead of numbers.
DROP VIEW order_info;
CREATE VIEW order_info AS
select order_num, name, company, amount
from orders, salesreps, customers
where rep=empl_num
and cust=cust_num;
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
4-8
Updating a View
Update of View also updates underlying
table, but view must meet certain
conditions:
• Must have update privileges
• View must be 1:1 w/underlying table
• One table only
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
4-9
Updating a View
Insert into Eastreps
(empl_num, name, rep_office, hire_date)
values (113, ‘Jake Kimball’, 11, ’14-apr-2003’);
Insert into Order_Info
(order_num, name, company, amount)
values (999999, ‘New Rep’, ‘New Co’, 50000);
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
4-10
Triggers
Triggers are stored procedures that are invoked at
a given time based on an action.
– Time: Before, After, Instead Of
– Actions: Insert, Delete, Update
Advantages:
Auditing, Cascade, Enforce Relationships,
Procedure Invocation
Disadvantages:
Performance
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
4-11
Triggers
create trigger after_insert_bkup
after insert on orders
begin
insert into ordersbkup
select * from orders
where not exists
(select * from ordersbkup
where ordersbkup.order_num = orders.order_num);
end;
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
4-12
Correlated Subquery
• A correlated subquery looks similar to a regular
subquery
• A regular subquery can be processed from the
bottom up
• For a correlated subquery, the processing is
nested, i.e., a row from an upper query
statement is used in comparison with rows in a
lower-level query
• Useful in checking for duplicates
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
4-13
Database Redesign
• Three principles for database redesign:
– Measure twice and cut once: understand the current
structure and contents of the database before making
any structure changes
– Test the new changes on a test database before
making real changes
– Create a complete backup of the operational
database before making any structure changes
• Technique: Reverse Engineering (RE)
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
4-14
Reverse Engineering (RE)
• Reverse engineering (RE) is the process of
reading and producing a data model from
a database schema
• A reverse engineered (RE) data model:
– Provides a basis to begin the database redesign
project
– Is neither truly a conceptual nor an internal schema
as it has characteristics of both
– Should be carefully reviewed because it almost
always has missing information
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
4-15
Database Backup and Test
Databases
• Before making any changes to an operational
database:
– A complete backup of the operational database
should be made
– Any proposed changes should be thoroughly tested
• Three different copies of the database schema
used in the redesign process:
– A small test database for initial testing
– A large test database for secondary testing
– The operational database
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
4-16
Database Redesign Changes
• Changing tables and columns
–
–
–
–
Changing table names
Adding and dropping table columns
Changing data type or constraints
Adding and dropping constraints
• Changing relationships
– Changing cardinalities
– Adding and deleting relationships
– Adding and removing relationship for denormalization
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
4-17