Views
Managing Change of Data • Course information CourseInfo (course name, time, professor, classroom) – May be used in many applications: student information system, faculty evaluation system, … • Table Courses (course name, time, professor, classroom) • What if the table is split to Crs (cid, time, pid, classroom), CrsList (cid, cname), and Prof (pid, pname, salary)? – Updating all related applications is costly and impractical – Some attributes like salary should not be released to public CMPT 354: Database I -- Views 2
Interfaces to Applications • Logical level should be stable – Shared by many applications, performance concerns • Diverse applications – Different applications may want different forms of data – First_name, last_name – Full_name CMPT 354: Database I -- Views 3
Two Needs for Views • Sensitive information projection – An instructor should be able to see the names and student-ids of the students in the class, and to assign grades in the course, but not other information – A subset of students, a subset of attributes • Integration of multiple data sources – A virtual table balance (customer-number, total_deposit, total_loan) may be often needed for many analysis tasks – Writing sub-queries in many queries is tedious – query reusing • A view: a relation that is not of the conceptual model but is made visible to a user as a “virtual relation” CMPT 354: Database I -- Views 4
Example • A view consisting of branches and their customers create view all_customer as ( select branch_name, customer_name from depositor, account where depositor.account_number = account.account_number ) union ( select branch_name, customer_name from borrower, loan where borrower.loan_number = loan.loan_number ) • Using a view: find all customers of the Perryridge branch select customer_name from all_customer where branch_name = ‘Perryridge’ CMPT 354: Database I -- Views 5
View Definition create view v as < query expression > • <query expression> is any legal SQL expression. The view name is represented by v • The view name can be used to refer to the virtual relation that the view generates – Immaterialized: view definition causes the storage of an expression; the expression is substituted into queries using the view CMPT 354: Database I -- Views 6
Defining Views Using Other Views • One view may be used in the expression defining another view create view all_customer as (select branch_name, customer_name from depositior, account where depositor.account_number = account.account_number) union (select brnch_name, customer_name from borrower, loan where borrower.loan-number=loan.loan_number) create view perryridge_customer as select customer_name from all_customer where branch_name = “Perryridge” CMPT 354: Database I -- Views 7
View Dependency • A view v1 depends directly on a view v2 if v2 is used in the expression defining v1 • A view v1 depends on view v2 if either v1 depends directly on v2 or there is a path of dependencies from v1 to v2 • A view v is recursive if it depends on itself – Subtle situations, some control structures are needed to make it work – Common Table Expression in SQL Server 2005 CMPT 354: Database I -- Views 8
View Expansion • A way to define the meaning of views defined in terms of other views • Let view v1 be defined by an expression e1 that may itself contain uses of views • View expansion of an expression repeats the following replacement step Repeat Find any view relation vi in e1 Replace the view relation vi by the expression defining vi Until no more view relations are present in e1 • As long as the view definitions are not recursive, this loop will terminate CMPT 354: Database I -- Views 9
Deletion • Delete all account tuples at the Perryridge branch delete from account where branch_name = ‘Perryridge’ • Delete all accounts at every branch located in the city ‘Needham’ delete from account where branch_name in (select branch_name from branch where branch_city = ‘Needham’) CMPT 354: Database I -- Views 10
Aggregate and Deletion • Delete the record of all accounts with balances below the average at the bank delete from account where balance < ( select avg ( balance ) from account ) • Subtlety: as we delete tuples from deposit, the average balance changes • Solution – First, compute avg balance and find all tuples to delete – Next, delete all tuples found above (without recomputing avg or retesting the tuples) CMPT 354: Database I -- Views 11
Insertion • Add a new tuple to account insert into account values (‘A-9732’, ‘Perryridge’,1200) – Another method insert into account (branch_name, balance, account_number) values (‘Perryridge’, 1200, ‘A-9732’) • Add a new tuple to account with balance set to null insert into account values (‘A-777’,‘Perryridge’, null ) CMPT 354: Database I -- Views 12
Insertion – Order of Evaluation • Provide as a gift for all loan customers of the Perryridge branch, a $200 savings account, let the loan number serve as the account number for the new savings account insert into account select loan_number, branch_name, 200 from loan where branch_name = ‘Perryridge’ insert into depositor select customer_name, loan_number from loan, borrower where branch_name = ‘ Perryridge’ and loan.account_number = borrower.account_number • The select-from-where statement is evaluated fully before any of its results are inserted into the relation – otherwise queries like “insert into table1 select * from table1” would cause problems CMPT 354: Database I -- Views 13
Updates • Increase all accounts with balances over $10,000 by 6%, all other accounts receive 5%. – Write two update statements: update account set balance = balance ∗ 1.06 where balance > 10000 update account set balance = balance ∗ 1.05 where balance ≤ 10000 – The order is important CMPT 354: Database I -- Views 14
Conditional Updates • Increase all accounts with balances over $10,000 by 6%, all other accounts receive 5% update account set balance = case when balance <= 10000 then balance *1.05 else balance * 1.06 end CMPT 354: Database I -- Views 15
Update Using a View • Create a view of all loan data in the loan relation, hiding the amount attribute create view branch_loan as select branch_name, loan_number from loan • Add a new tuple to branch_loan insert into branch_loan values (‘Perryridge’, ‘L-307’) – Effect: insert tuple (‘L-307’, ‘Perryridge’, null ) into the loan relation CMPT 354: Database I -- Views 16
Updates Through Views • Some updates through views are impossible to translate into updates on the database tables create view v as select customer_name, amount from borrower, loan where borrower.loan_number = loan.loan_number insert into v values (‘John’, 1900) – Cannot determine the loan_number in the two tablesa • Most SQL implementations allow updates only on simple views (without aggregates) defined on a single relation CMPT 354: Database I -- Views 17
Summary • View • Deletion, insertion, and update CMPT 354: Database I -- Views 18
To-Do List • Using the pubs database, create a view of firstnames and lastnames of authors, add a new author “Helen” “Johnson” using the view. What do you learn from this query? CMPT 354: Database I -- Views 19
Database Schema branch ( branch_name, branch_city, assets ) customer ( customer_name, customer_street, customer_city ) loan ( loan_number, branch_name, amount ) borrower ( customer_name, loan_number ) account ( account_number , branch_name, balance ) depositor ( customer_name, account_number ) CMPT 354: Database I -- Views 20
Recommend
More recommend