©Silberschatz, Korth and Sudarshan5.1Database System Concepts Chapter 5: Other Relational Query Languages Tuple Relational Calculus Domain Relational Calculus.

Slides:



Advertisements
Similar presentations
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 5: Other Relational.
Advertisements

Chapter 3 Tuple and Domain Relational Calculus. Tuple Relational Calculus.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Tuple Relational.
E-R Diagram for a Banking Enterprise
BRANCH-SCHEME (BRANCH-NAME, ASSETS, BRANCH-CITY)
Relational Calculus Ameetinder Singh CS 157A. Tuple Relational Calculus  non-procedural query language as compared to relational algebra that is procedural.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Tuple Relational.
Midterm 2 Revision Prof. Sin-Min Lee Department of Computer Science San Jose State University.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Extended Relational-Algebra-Operations Generalized Projection Outer Join Aggregate Functions.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Extended.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Tuple Relational.
SPRING 2004CENG 3521 E-R Diagram for the Banking Enterprise.
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. SQL - part 2 - Database Management Systems I Alex Coman, Winter 2006.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
©Silberschatz, Korth and Sudarshan1Database System Concepts Tuple and Domain Calculus Tuple Relational Calculus Domain Relational Calculus.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
©Silberschatz, Korth and Sudarshan6.1Database System Concepts Chapter 6: Integrity and Security Domain Constraints Referential Integrity Assertions Triggers.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
3.1 Chapter 3: SQL. 3.2 Chapter 3: SQL Basic Query Structure Set Operations Aggregate Functions Null Values Nested Subqueries Complex Queries Views Modification.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Null Values It is possible for tuples to have a null value for some of their attributes The.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Tuple Relational.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Tuple Relational.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Banking Example  branch (branch-name, branch-city, assets)  customer (customer-name, customer-street,
Lecture 6 Structured Query Language SQL Lecture 6 Structured Query Language SQL Instructor: Haya Sammaneh.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
3.1 Chapter 3: SQL Schema used in examples p (omit 3.8.2, , 3.11)
Basic Structure Given sets A 1, A 2,..., A n a relation r is a subset of A 1 x A 2 x... x A n Thus a relation is a set of n-tuples (a 1, a 2,..., a n )
Structured Query Language 2 Presented by: Annisa, M.Kom. Source: Database System Concepts 5 th edition.
Temple University – CIS Dept. CIS331– Principles of Database Systems V. Megalooikonomou Relational Model III (based on notes by Silberchatz,Korth, and.
Computing & Information Sciences Kansas State University Tuesday, 06 Feb 2007CIS 560: Database System Concepts Lecture 10 of 42 Tuesday, 06 February 2007.
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational.
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 5 th Edition, Oct 5, 2006 Outer Join n An extension of the join operation that avoids loss.
1 The Relational Model By Elena Ciriani CS157A February 19, 2004 Professor Lee.
ICOM 5016 – Introduction to Database Systems Lecture 5b Dr. Manuel Rodriguez Department of Electrical and Computer Engineering University of Puerto Rico,
CIS552Relational Model1 Structure of Relational Database Relational Algebra Extended Relational-Algebra-Operations Modification of the Database.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts Chapter 5: Other Relational Languages Query-by-Example (QBE)
Computing & Information Sciences Kansas State University Tuesday, 13 Feb 2007CIS 560: Database System Concepts Lecture 12 of 42 Tuesday, 13 February 2007.
Computing & Information Sciences Kansas State University Thursday, 08 Feb 2007CIS 560: Database System Concepts Lecture 11 of 42 Thursday, 08 February.
3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Tuple Relational Calculus Domain Relational.
Midterm 2 Revision Prof. Sin-Min Lee Department of Mathematics and Computer Science San Jose State University.
Relational Model By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany) DIRECTOR ARUNAI ENGINEERING COLLEGE TIRUVANNAMALAI.
ICOM 5016 – Introduction to Database Systems Lecture 8 Dr. Manuel Rodriguez Department of Electrical and Computer Engineering University of Puerto Rico,
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Extended.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts, 5 th Edition, Oct 5, 2006 SQL Data Definition Basic Query Structure Set Operations Aggregate.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 5 th Edition, Oct 5, 2006 Example.
©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.
Computing & Information Sciences Kansas State University Wednesday, 05 Sep 2007CIS 560: Database System Concepts Lecture 6 of 42 Wednesday, 05 September.
Chapter 4: SQL Complex Queries Complex Queries Views Views Modification of the Database Modification of the Database Joined Relations Joined Relations.
Chapter 2: Relational Model II Relational Algebra basics Relational Algebra basics.
2.1 Chapter 2: Relational Model. 2.2 Chapter 2: Relational Model Structure of Relational Databases Fundamental Relational-Algebra-Operations Additional.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Extended.
Database System Concepts, 5th Ed. Bin Mu at Tongji University Chapter 5: Other Relational Languages.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Module A: Formal Relational.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Extended Relational-Algebra-Operations Generalized Projection Aggregate Functions Outer Join.
Computing & Information Sciences Kansas State University Friday, 08 Sep 2006CIS 560: Database System Concepts Lecture 07 of 42 Tuesday, 30 January 2007.
Midterm 2 Revision Prof. Sin-Min Lee Department of Computer Science San Jose State University.
Computing & Information Sciences Kansas State University Wednesday, 17 Sep 2008CIS 560: Database System Concepts Lecture 9 of 42 Wednesday, 18 September.
Relational Algebra HW2 Turn in as a hardcopy at the start of next class period. You may work this assignment in groups.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Week 3: Relational Model Structure of Relational Databases Relational Algebra Extended Relational-Algebra-Operations.
IS 230Lecture 7Slide 1 Relational Algebra Lecture 8 Text Book – Chapter.
Chapter 3: Relational Model III Additional Relational Algebra Operations Additional Relational Algebra Operations Views Views.
©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra Tuple Relational.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Tuple Relational.
Database System Concepts ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
Query Languages Language in which user requests information from the database. Categories of languages Procedural Non-procedural, or declarative “Pure”
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan Chapter 2: Relational Model.
Relational Model By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany)
Presentation transcript:

©Silberschatz, Korth and Sudarshan5.1Database System Concepts Chapter 5: Other Relational Query Languages Tuple Relational Calculus Domain Relational Calculus

©Silberschatz, Korth and Sudarshan5.2Database System Concepts Tuple Relational Calculus A nonprocedural query language, where each query is of the form { t | P (t) }  read as “the set of all tuples t such that predicate P is true for t”  P is a formula similar to that of the predicate calculus

©Silberschatz, Korth and Sudarshan5.3Database System Concepts Predicate Calculus Formula The predicate P(t) will contain:  Attribute, tuple and relation variables and constants If t is a tuple variable, t[A] denotes the value of t on attribute A t  r denotes that tuple t is in relation r  Comparison operators: (e.g., , , , , ,  )  Connectives: and (  ), or (v)‚ not (  )  Implication (  ): x  y, if x if true, then y is true x  y  x v y  Quantifiers:  t  r (Q(t))  ”there exists” a tuple t in relation r such that Q(t) is true  t  r (Q(t))  Q(t) is true “for all” tuples t in relation r => See the book for a more complete and precise definition (pages 166 & 167)

©Silberschatz, Korth and Sudarshan5.4Database System Concepts Banking Example branch (branch-name, branch-city, assets) customer (customer-name, customer-street, customer-city) account (account-number, branch-name, balance) loan (loan-number, branch-name, amount) depositor (customer-name, account-number) borrower (customer-name, loan-number)

©Silberschatz, Korth and Sudarshan5.5Database System Concepts Example Queries Find the loan-number, branch-name, and amount for loans of over $1200 How about the following? {t |  s  loan (t = s  s [amount]  1200)} {t | t  loan  t [amount]  1200} {t | t [amount]  1200}

©Silberschatz, Korth and Sudarshan5.6Database System Concepts Example Queries Find the loan number for each loan having an amount greater than $1200 Why not the following? Notice that a relation on schema [loan-number] is implicitly defined by the query {t |  s  loan (t[loan-number] = s[loan-number]  s[amount]  1200)} {t | t  loan  t [amount]  1200}

©Silberschatz, Korth and Sudarshan5.7Database System Concepts Example Queries Find the names of all customers having a loan, an account, or both at the bank {t |  s  borrower( t[customer-name] = s[customer-name])   u  depositor( t[customer-name] = u[customer-name])} Find the names of all customers who have a loan and an account at the bank {t |  s  borrower( t[customer-name] = s[customer-name])   u  depositor( t[customer-name] = u[customer-name])}

©Silberschatz, Korth and Sudarshan5.8Database System Concepts Example Queries Find the names of all customers having a loan at the Perryridge branch {t |  s  borrower( t[customer-name] = s[customer-name]   u  loan(u[branch-name] = “Perryridge”  u[loan-number] = s[loan-number]))  not  v  depositor (v[customer-name] = t[customer-name]) } Find the names of all customers who have a loan at the Perryridge branch, but no account at any branch of the bank {t |  s  borrower(t[customer-name] = s[customer-name]   u  loan(u[branch-name] = “Perryridge”  u[loan-number] = s[loan-number]))}

©Silberschatz, Korth and Sudarshan5.9Database System Concepts Example Queries Find the names of customers and their cities of residence for those customer having a loan from the Perryridge branch. Note that the above contains a parenthetical mistake. {t |  s  loan(s[branch-name] = “Perryridge”   u  borrower (u[loan-number] = s[loan-number]  t [customer-name] = u[customer-name])   v  customer (u[customer-name] = v[customer-name]  t[customer-city] = v[customer-city])))}

©Silberschatz, Korth and Sudarshan5.10Database System Concepts Safety of Expressions It is possible to write tuple calculus expressions that generate infinite relations.  For example, {t |  t  r} results in an infinite relation if the domain of any attribute of relation r is infinite To guard against the problem, we restrict the set of allowable expressions to what are called “safe” expressions. An expression {t | P(t)} in the tuple relational calculus is safe if every component of t (i.e., the result) appears in one of the relations, tuples, or constants that appear in P  NOTE: this is more than just a syntax condition.  Example: { t | t[A]=5  true } is not safe --- it defines an infinite set with attribute values that do not appear in any relation or tuples or constants in P.

©Silberschatz, Korth and Sudarshan5.11Database System Concepts Example Queries Find the names of all customers who have an account at all branches located in Brooklyn: Note that the above query is unsafe, but why?  Consider a branch relation that consists of no Brooklyn branches. {t |  s  branch(s[branch-city] = “Brooklyn”   u  account(s[branch-name] = u[branch-name]   v  depositor(v[account-number] = u[account-number]  t[customer-name] = v[customer-name])))}

©Silberschatz, Korth and Sudarshan5.12Database System Concepts Another, Safe Version Find the names of all customers who have an account at all branches located in Brooklyn (safe version): {t |  c  customer (t[customer.name] = c[customer-name])   s  branch(s[branch-city] = “Brooklyn”   u  account(s[branch-name] = u[branch-name]   v  depositor(v[account-number] = u[account-number]  t[customer-name] = v[customer-name])))}

©Silberschatz, Korth and Sudarshan5.13Database System Concepts Domain Relational Calculus A nonprocedural query language equivalent in power to the tuple relational calculus Each query is an expression of the form: {  x 1, x 2, …, x n  | P(x 1, x 2, …, x n )}  x 1, x 2, …, x n represent domain variables  P represents a formula similar to that of the predicate calculus

©Silberschatz, Korth and Sudarshan5.14Database System Concepts Example Queries Find the loan-number, branch-name, and amount for loans of over $1200 {  c, a  |  l (  c, l   borrower   b(  l, b, a   loan  b = “Perryridge”))} or {  c, a  |  l (  c, l   borrower   l, “Perryridge”, a   loan)} Find the names of all customers who have a loan from the Perryridge branch and the loan amount: {  c  |  l, b, a (  c, l   borrower   l, b, a   loan  a > 1200)} Find the names of all customers who have a loan of over $1200 {  l, b, a  |  l, b, a   loan  a > 1200}

©Silberschatz, Korth and Sudarshan5.15Database System Concepts Example Queries Find the names of all customers having a loan, an account, or both at the Perryridge branch: {  c  |  s, n (  c, s, n   customer)   x,y,z(  x, y, z   branch  y = “Brooklyn”)   a,b(  x, y, z   account   c,a   depositor)} Find the names of all customers who have an account at all branches located in Brooklyn: {  c  |  l ({  c, l   borrower   b,a(  l, b, a   loan  b = “Perryridge”))   a(  c, a   depositor   b,n(  a, b, n   account  b = “Perryridge”))}

©Silberschatz, Korth and Sudarshan5.16Database System Concepts Safety of Expressions {  x 1, x 2, …, x n  | P(x 1, x 2, …, x n )} is safe if all of the following hold: 1.All values that appear in tuples of the expression are values from dom(P) (that is, the values appear either in P or in a tuple of a relation mentioned in P). 2.For every “there exists” subformula of the form  x (P 1 (x)), the subformula is true if and only if there is a value of x in dom(P 1 ) such that P 1 (x) is true. 3. For every “for all” subformula of the form  x (P 1 (x)), the subformula is true if and only if P 1 (x) is true for all values x from dom (P 1 ).

End of Chapter 3

©Silberschatz, Korth and Sudarshan5.18Database System Concepts Views Views are very important, but we will not consider them until chapter 4, so goto slide 88. In some cases, it is not desirable for all users to see the entire logical model (i.e., all the actual relations stored in the database.) 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, in the relational algebra, by  customer-name, loan-number (borrower loan) Any relation that is not of the conceptual model but is made visible to a user as a “virtual relation” is called a view.

©Silberschatz, Korth and Sudarshan5.19Database System Concepts View Definition A view is defined using the create view statement which has the form create view v as <query expression where is any legal relational algebra query expression. The view name is represented by 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; the expression is substituted into queries using the view.

©Silberschatz, Korth and Sudarshan5.20Database System Concepts View Examples Consider the view (named all-customer) consisting of branches and their customers. We can find all customers of the Perryridge branch by writing: create view all-customer as  branch-name, customer-name (depositor account)   branch-name, customer-name (borrower loan)  branch-name (  branch-name = “Perryridge” (all-customer))

©Silberschatz, Korth and Sudarshan5.21Database System Concepts 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)}

©Silberschatz, Korth and Sudarshan5.22Database System Concepts Updates Through Views (Cont.) The previous insertion must be represented by an insertion into the actual relation loan from 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 Some updates through views are impossible to translate into database relation updates  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!

©Silberschatz, Korth and Sudarshan5.23Database System Concepts Views Defined Using Other Views One view may be used in the expression defining another view A view relation v 1 is said to depend directly on a view relation v 2 if v 2 is used in the expression defining v 1 A view relation v 1 is said to depend on view relation v 2 if either v 1 depends directly to v 2 or there is a path of dependencies from v 1 to v 2 A view relation v is said to be recursive if it depends on itself.

©Silberschatz, Korth and Sudarshan5.24Database System Concepts View Expansion A way to define the meaning of views defined in terms of other views. Let view v 1 be defined by an expression e 1 that may itself contain uses of view relations. View expansion of an expression repeats the following replacement step: repeat Find any view relation v i in e 1 Replace the view relation v i by the expression defining v i until no more view relations are present in e 1 As long as the view definitions are not recursive, this loop will terminate

©Silberschatz, Korth and Sudarshan5.25Database System Concepts Result of  branch-name = “Perryridge” (loan)

©Silberschatz, Korth and Sudarshan5.26Database System Concepts Loan Number and the Amount of the Loan

©Silberschatz, Korth and Sudarshan5.27Database System Concepts Names of All Customers Who Have Either a Loan or an Account

©Silberschatz, Korth and Sudarshan5.28Database System Concepts Customers With An Account But No Loan

©Silberschatz, Korth and Sudarshan5.29Database System Concepts Result of borrower  loan

©Silberschatz, Korth and Sudarshan5.30Database System Concepts Result of  branch-name = “Perryridge” (borrower  loan)

©Silberschatz, Korth and Sudarshan5.31Database System Concepts Result of  customer-name

©Silberschatz, Korth and Sudarshan5.32Database System Concepts Result of the Subexpression

©Silberschatz, Korth and Sudarshan5.33Database System Concepts Largest Account Balance in the Bank

©Silberschatz, Korth and Sudarshan5.34Database System Concepts Customers Who Live on the Same Street and In the Same City as Smith

©Silberschatz, Korth and Sudarshan5.35Database System Concepts Customers With Both an Account and a Loan at the Bank

©Silberschatz, Korth and Sudarshan5.36Database System Concepts Result of  customer-name, loan-number, amount (borrower loan)

©Silberschatz, Korth and Sudarshan5.37Database System Concepts Result of  branch-name (  customer-city = “Harrison” ( customer account depositor))

©Silberschatz, Korth and Sudarshan5.38Database System Concepts Result of  branch-name (  branch-city = “Brooklyn” (branch))

©Silberschatz, Korth and Sudarshan5.39Database System Concepts Result of  customer-name, branch-name (depositor account)

©Silberschatz, Korth and Sudarshan5.40Database System Concepts The credit-info Relation

©Silberschatz, Korth and Sudarshan5.41Database System Concepts Result of  customer-name, (limit – credit-balance) as credit-available (credit-info).

©Silberschatz, Korth and Sudarshan5.42Database System Concepts The pt-works Relation

©Silberschatz, Korth and Sudarshan5.43Database System Concepts The pt-works Relation After Grouping

©Silberschatz, Korth and Sudarshan5.44Database System Concepts Result of branch-name  sum(salary) (pt-works)

©Silberschatz, Korth and Sudarshan5.45Database System Concepts Result of branch-name  sum salary, max(salary) as max- salary (pt-works)

©Silberschatz, Korth and Sudarshan5.46Database System Concepts The employee and ft-works Relations

©Silberschatz, Korth and Sudarshan5.47Database System Concepts The Result of employee ft-works

©Silberschatz, Korth and Sudarshan5.48Database System Concepts The Result of employee ft-works

©Silberschatz, Korth and Sudarshan5.49Database System Concepts Result of employee ft-works

©Silberschatz, Korth and Sudarshan5.50Database System Concepts Result of employee ft-works

©Silberschatz, Korth and Sudarshan5.51Database System Concepts Tuples Inserted Into loan and borrower

©Silberschatz, Korth and Sudarshan5.52Database System Concepts Names of All Customers Who Have a Loan at the Perryridge Branch

©Silberschatz, Korth and Sudarshan5.53Database System Concepts E-R Diagram

©Silberschatz, Korth and Sudarshan5.54Database System Concepts The branch Relation

©Silberschatz, Korth and Sudarshan5.55Database System Concepts The loan Relation

©Silberschatz, Korth and Sudarshan5.56Database System Concepts The borrower Relation

©Silberschatz, Korth and Sudarshan5.57Database System Concepts Determining Keys from E-R Sets Strong entity set. The primary key of the entity set becomes the primary key of the relation. Weak entity set. The primary key of the relation consists of the union of the primary key of the strong entity set and the discriminator of the weak entity set. Relationship set. The union of the primary keys of the related entity sets becomes a super key of the relation.  For binary many-to-one relationship sets, the primary key of the “many” entity set becomes the relation’s primary key.  For one-to-one relationship sets, the relation’s primary key can be that of either entity set.  For many-to-many relationship sets, the union of the primary keys becomes the relation’s primary key