1 / 57

Views

Views. Limited access to DB. Tailored schema Consider a person who needs to know a customer’s loan number but has no need to see the loan amount. This person should see a relation described as:  customer-name, loan-number ( borrower loan )

miyoko
Download Presentation

Views

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Views • Limited access to DB. • Tailored schema • Consider a person who needs to know a customer’s loan number but has no need to see the loan amount. This person should see a relation described as: customer-name, loan-number(borrower loan) • A relation that is made visible to a user as a “virtual relation” is called a view.

  2. View Definition • A view is defined using the create view statement which has the form create view v as <query expression> where <query expression> is any legal relational algebra query expression. The view name given as v. • Once a view is defined, the view name can be used to refer to the virtual relation that the view generates. • View definition is not the same as creating a new relation by evaluating the query expression Rather, a view definition causes the saving of an expression to be substituted into queries using the view.

  3. View Examples • Consider the view (named all-customer) consisting of branches and their customers. create view all-customer as branch-name, customer-name(depositor account)  branch-name, customer-name(borrowerloan) • We can find all customers of the Perryridge branch by writing: customer-name (branch-name= “Perryridge”(all-customer))

  4. Updates Through View • Database modifications expressed as views must be translated to modifications of the actual relations in the database. • Consider the person who needs to see all loan data in the loan relation except amount. The view given to the person, branch-loan, is defined as: create view branch-loan as branch-name, loan-number(loan) • Since we allow a view name to appear wherever a relation name is allowed, the person may write: branch-loan  branch-loan  {(“Perryridge”, L-37)}

  5. Updates Through Views (Cont.) • The previous insertion must be represented by an insertion into the actual relation loanfrom which the view branch-loan is constructed. • An insertion into loan requires a value for amount. The insertion can be dealt with by either. • rejecting the insertion and returning an error message to the user. • inserting a tuple (“L-37”, “Perryridge”, null) into the loan relation

  6. Updates Through Views (Cont.) • Some updates through views are impossible to translate. create view v as branch-name= “Perryridge”(account)) v  v  (L-99, Downtown, 23) • Others cannot be translated uniquely all-customer all-customer  (Perryridge, John) • Have to choose loan or account, and create a new loan/account number!

  7. Views Defined Using Other Views • One view may be used in the expression defining another view • A view relation v1 is said to depend directly on a view relation v2 if v2 is used in the expression defining v1 • A view relation v1 is said to depend on view relation v2if either v1 depends directly on v2 orthere is a path of dependencies from v1 to v2

  8. View Expansion • Let view v1 be defined by an expression e1 that may itself contain uses of view relations. • View expansion of an expression repeats the following replacement step: repeatFind any view relation vi in e1 Replace the view relation vi by the expression defining viuntil no more view relations are present in e1 • As long as the view definitions are not recursive, this loop will terminate

  9. Database Design and the Entity/Relationship Model

  10. Motivation • You’ve just been hired by Bank of America as their DBA for their online banking web site. • You are asked to create a database that monitors: • customers • accounts • loans • branches • transactions, … • Now what??!!!

  11. Being a DBADatabase Design Steps • 1.Requirements Specification • Determine the requirements of clients • 2. Conceptual Design • Express client requirements in terms of some high-level model (e.g., E/R model). • Confirm with clients that requirements are correct. • 3. Functional Requirements • Specify required data operations • priorities, response times • 4. Logical Design • Convert E/R model to relational, object-based, XML-based,… • 5. Physical Design • Specify file organizations, build indexes

  12. Conceptual DesignThe E/R Data Model • What is a Data Model? • Framework for organizing and interpreting data Example: E/R Data Model Relationship Entity2 Entity1 Attribute2a Attribute2b Attribute1a Attribute1b Attribute2c

  13. E/R Data ModelBasics • Entities • noun phrases (e.g., Bob Smith, Thayer St. Branch) • contained in entity sets (e.g. Employee, Branch) • have attributes (e.g., Employee = (essn, ename, …)) • Relationships • verb phrases (e.g., works_at, works_for) • relate 2 (binary) or more (n-ary) entities • relationship sets characterize relationships amongst entity sets • e.g., (Bob Smith, Thayer St Branch) Î Works_At

  14. Employee Entity Set Works_At Relationship Set Attribute phone E/R Data ModelAn Example Works_At Branch Employee essn phone bname bcity seniority since ename children Works_For manager worker Lots of notation to come. Color is irrelevant

  15. E/R Data ModelTypes of Attributes Works_At Branch Employee essn phone bname bcity seniority since ename children Works_For manager Default worker ename Multivalued children Derived seniority

  16. E/R Data ModelTypes of relationships Works_At Branch Employee essn phone bname bcity seniority since ename children Works_For manager Many-to-One (n:1) worker Works_For Many-to-Many (n:m) Works_At

  17. E/R Data ModelRecursive relationships Works_At Branch Employee essn phone bname bcity seniority since ename children Recursive relationships: Must be declared with roles Works_For manager worker • roles manager Employee Works_For worker

  18. E/R Data ModelDesign Issue #1: Entity Sets vs. Attributes (b) (a) Uses Employee Phone Employee vs phone_loc phone_no no loc • To resolve, determine how phones are used • 1. Can many employees share a phone? (If yes, then (b)) • 2. Can employees have multiple phones? (if yes, then (b), or (a) with multivalued attributes) • 3. Else(a)

  19. E/R Data ModelDesign Issue #2: Entity Sets vs. Relationship Sets • An Example: How to model bank loans Loans Borrows Branch Customer Loan Customer vs bcity bname cssn cname amt lno cssn cname (a) amt lno (b) • To resolve, determine how loans are issued • 1. Can there be more than one customer per loan? • If yes, then (a). Otherwise, loan info must be replicated for each customer (wasteful, potential update anomalies) • 2. Is loan a noun or a verb? • Both, but more of a noun to a bank. (hence (a) probably more appropriate)

  20. E/R Data ModelDesign Issue #3: Relationship Cardinalities • An Example: Borrows ? ? Customer Loan • Variations on Borrows: • 1. Can a customer hold multiple loans? • 2. Can a loan be jointly held by more than 1 customer?

  21. E/R Data ModelDesign Issue #3: Relationship Cardinalities Borrows ? ? Customer Loan • Cardinalities of Borrows: Borrows Borrows Borrows Borrows

  22. E/R Data ModelDesign Issue #3: Relationship Cardinalities (cont) • In general... 1:1 n:1 1:n n:m

  23. E/R Data ModelDesign Issue #4: N-aryvs Binary Relationship Sets • An Example: Works_At Ternary: Works_at Employee Branch Choose n-ary when possible! Dept (Joe, Thayer, Acct) Î Works_At vs Binary: WAB WAE Branch WA Employee WAD (Joe, w3) Î WAE (Thayer, w3) Î WAB (Acct, w3) Î WAD Dept

  24. E/R Data ModelKeys • Key = set of attributes identifying individual entities or relationships Employee essn ephone eaddress ename • A. Superkey: • any attribute set that distinguishes identities • e.g., {essn}, {essn, ename, eaddress} • B. Candidate Key: • “minimal superkey” (can’t remove attributes and preserve “keyness”) • e.g., {essn}, {ename, eaddress} • C. Primary Key: • candidate key chosen as the key by a DBA • e.g., {essn} (denoted by underline)

  25. E/R Data ModelRelationship Set Keys Works_At Employee Branch ... ... essn ename bname bcity since Q: What attributes are needed to represent relationships in Works_At? b1 e1 e2 b2 e3 A: {essn, bname, since}

  26. E/R Data ModelRelationship Set Keys (cont.) Works_At Employee Branch ... ... essn ename bname bcity since • Q: What are the candidate keys of Works_At? b1 e1 e2 b2 e3 A: {essn}

  27. E/R Data ModelRelationship Set Keys (cont.) ? Works_At ? Employee Branch ... ... essn ename bname bcity since • Q: What are the candidate keys if Works_Atis...? a. 1:n A: {bname} b. n:m A: {essn, bname} c. 1:1 A: {essn} or {bname}

  28. What’s Goin’ On? Works_in Branch Employee Bname gets you 1 entity in Branch 1:n essn1 bname1 1:n gets you a a unique piece of string, i.e. a key for Works_in. essn2 bname2 bname3

  29. E/R Data ModelRelationship Set Keys (cont.) • General Rules for Relationship Set Keys R E1 E2 ... ... P (E1) P (E2) • If R is: Candidate Keys P (E1) or P (E2) P (E2) P (E1) P (E1) È P (E2) R 1:1 1:n n:1 n:m

  30. Loan_Pmt E/R Data ModelExistence Dependencies and Weak Entity Sets • Idea: • Existence of one entity depends on another • Example: Loans and Loan Payments Payment Loan lno pno pamt pdate lamt Weak Entity Set Identifying Relationship Total Participation

  31. Loan_Pmt E/R Data ModelExistence Dependencies and Weak Entity Sets Payment Loan lno pno pamt pdate lamt • Weak Entity Sets • existence of payments depends upon loans • have no superkeys: different payment records (for different loans) can be identical • instead of keys, discriminators: discriminate between payments for given loan (e.g., pno)

  32. Loan_Pmt E/R Data ModelExistence Dependencies and Weak Entity Sets Payment Loan lno pno pamt pdate lamt • Identifying Relationships • We say: • Loan is dominant in Loan_Pmt • Payment is subordinate in Loan_Pmt • Payment is existence dependent on Loan

  33. Loan_Pmt E/R Data ModelExistence Dependencies and Weak Entity Sets Payment Loan lno lamt pno pamt pdate • All elements of Payment appear in Loan_Pmt

  34. R E/R Data ModelExistence Dependencies and Weak Entity Sets E2 E1 ... atta1 attam attb1 ... attbn • Q. Is {attb1, …, attbn} a superkey of E2? A: No • Q. Name a candidate key of E2 A: {atta1, attb1} • Q. Does total participation of E2 in R Þ E2 is existence-dep? A: No

  35. E/R Data ModelExtensions to the Model: Specialization and Generalization • An Example: • Customers can have checking and savings accts • Checking ~ Savings (many of the same attributes) • Old Way: Has1 Customer Savings Acct acct_no interest balance Has2 Checking Acct acct_no overdraft balance

  36. Isa E/R Data ModelExtensions to the Model: Specialization and Generalization • An Example: • Customers can have checking and savings accts • Checking ~ Savings (many of the same attributes) • Alternative Way: balance acct_no Has superclass Account Customer Savings Acct Checking Acct overdraft interest subclasses

  37. Isa Child Adult Senior E/R Data ModelExtensions to the Model: Specialization and Generalization • Subclass Distinctions: • 1. User-Defined vs. Condition-Defined • User: Membership in subclasses explicitly determined (e.g., Employee, Manager < Person) • Condition: Membership predicate associated with subclasses - e.g: Person age > 65 age < 18 18  age

  38. Isa Child Adult Senior E/R Data ModelExtensions to the Model: Specialization and Generalization • Subclass Distinctions: • 2. Overlapping vs. Disjoint • Overlapping: Entities can belong to >1 entity set (e.g., Adult, Senior) • Disjoint: Entities belong to exactly 1 entity set(e.g., Child) Person age > 65 age < 18 18  age

  39. E/R Data ModelSummary • Entities, Relationships (sets) • Both can have attributes (simple, multivalued, derived, composite) • Cardinality of relationship sets (1:1, n:1, n:m) • Keys: superkeys, candidate keys, primary key • DBA chooses primary key for entity sets • Automatically determined for relationship sets • Weak Entity Sets, Existence Dependence, Total/Partial Participation • Specialization (E/R + inheritance)

  40. E … a1 an E/R Diagrams and Relations

  41. R E2 E1 … … a1 b1 a1 an bm … E c1 ck … a1 an E/R Diagrams and Relations Not the whole story for Relationship Sets …

  42. R E2 E1 … … b1 a1 an a1 bm … ck c1 E/R Diagrams and Relations • What about… Could have: R = (a1, b1, c1, …, ck) but… • a1 is a key for R • a1 also a key forE1 = (a1, …, an) Rule of Thumb Fewer tables good, as long as no redundancy Instead: • Ignore R as a separate relation • Add b1, c1, …, cktoE1instead (i.e:E1 = (a1, …, an,, b1, c1, …, ck))

  43. R R ? ? E2 E1 … … a1 b1 a1 an bm … c1 ck E/R Diagrams and Relations

  44. R R R ? ? E2 E1 … … a1 b1 a1 an bm … c1 ck E/R Diagrams and Relations

  45. R R R R ? ? E2 E1 … … a1 b1 a1 an bm … c1 ck E/R Diagrams and Relations

  46. R R R R R ? ? E2 E1 … … a1 b1 a1 an bm … c1 ck E/R Diagrams and Relations

  47. Translating E/R Diagrams to Relations bcity assets bname balance acct_no Acct-Branch Account Branch Loan-Branch Depositor Borrower Customer Loan ccity amt cname lno cstreet Q. How many tables does this get translated into? A. 6 (account, branch, customer, loan, depositor, borrower)

  48. Translating E/R Diagrams to Relations bcity assets bname balance acct_no Acct-Branch Account Branch Loan-Branch Depositor Borrower Customer Loan ccity amt cname lno cstreet Q. What are the schemas?

  49. Bank Database

  50. Bank Database

More Related