Relational Calculus. Relational calculus query specifies what is to be retrieved rather than how to retrieve it. – No description of how to evaluate a.

Slides:



Advertisements
Similar presentations
1.3 Predicates and Quantifiers
Advertisements

From the Calculus to the Structured Query Language Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 22, 2005.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Calculus Chapter 4, Part B.
The Relational Calculus
IS698: Database Management Min Song IS NJIT. The Relational Data Model.
1 CHAPTER 4 RELATIONAL ALGEBRA AND CALCULUS. 2 Introduction - We discuss here two mathematical formalisms which can be used as the basis for stating and.
1 541: Relational Calculus. 2 Relational Calculus  Comes in two flavours: Tuple relational calculus (TRC) and Domain relational calculus (DRC).  Calculus.
CSC271 Database Systems Lecture # 13. Summary: Previous Lecture  Grouping through GROUP BY clause  Restricted groupings  Subqueries  Multi-Table queries.
Relational Algebra Content based on Chapter 4 Database Management Systems, (Third Edition), by Raghu Ramakrishnan and Johannes Gehrke. McGraw Hill, 2003.
1 Relational Algebra & Calculus. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
CREATE VIEW SYNTAX CREATE VIEW name [(view_col [, view_col …])] AS [WITH CHECK OPTION];
Relational Algebra Relational Calculus. Relational Algebra Operators Relational algebra defines the theoretical way of manipulating table contents using.
Lesson II The Relational Model © Pearson Education Limited 1995, 2005.
Relational Calculus. Another Theoretical QL-Relational Calculus n Comes in two flavors: Tuple relational calculus (TRC) and Domain relational calculus.
Chapter 5 Relational Algebra and Relational Calculus.
Relational Algebra & Calculus Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 21, 2004 Some slide content.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
1 Minggu 3, Pertemuan 6 Relational Calculus Matakuliah: T0206-Sistem Basisdata Tahun: 2005 Versi: 1.0/0.0.
SPRING 2004CENG 3521 E-R Diagram for the Banking Enterprise.
1 Relational Algebra and Calculus Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Chapter 6 SQL: Data Manipulation Cont’d. 2 ANY and ALL u ANY and ALL used with subqueries that produce single column of numbers u ALL –Condition only.
Introduction to Database Systems 1 Relational Calculus Relational Model : Topic 2.
Relational Algebra & Relational Calculus Dale-Marie Wilson, Ph.D.
Relational Algebra Wrap-up and Relational Calculus Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 11, 2003.
Rutgers University Relational Calculus 198:541 Rutgers University.
Relational Algebra.  Introduction  Relational Algebra Operations  Projection and Selection  Set Operations  Joins  Division  Tuple Relational Calculus.
1 Relational Algebra and Calculus Chapter 4. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.
Relational Query Languages. Languages of DBMS  Data Definition Language DDL  define the schema and storage stored in a Data Dictionary  Data Manipulation.
Lecture 3 [Self Study] Relational Calculus
The Relational Model: Relational Calculus
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Calculus Chapter 4, Section 4.3.
1 CS 430 Database Theory Winter 2005 Lecture 6: Relational Calculus.
CSC271 Database Systems Lecture # 12. Summary: Previous Lecture  Row selection using WHERE clause  WHERE clause and search conditions  Sorting results.
Chapter 4 Relational Algebra & Relational Calculus.
CSC271 Database Systems Lecture # 9. Summary: Previous Lecture  Cartesian product  Join  Theta join, equijoin, natural join  Outer join (left, right,
Chapter 8 Relational Calculus. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.8-2 Topics in this Chapter Tuple Calculus Calculus vs. Algebra.
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
Database Management Systems, R. Ramakrishnan1 Relational Calculus Chapter 4.
1 Relational Algebra. 2 Relational Query Languages v Query languages: Allow manipulation and retrieval of data from a database. v Relational model supports.
Relational Calculus. Non Procedural or Declarative Calculus has variables, constants, comparison ops, logical connectives and quantifiers. There are TWO.
C HAPTER 4 Relational Algebra and Relational Calculus Transparencies © Pearson Education Limited 1995,
Relational Calculus Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 17, 2007 Some slide content courtesy.
1 Relational Algebra & Calculus Chapter 4, Part A (Relational Algebra)
1 Relational Algebra and Calculas Chapter 4, Part A.
Relational Algebra.
From Relational Algebra to SQL CS 157B Enrique Tang.
Chapter 5 Relational Algebra and Relational Calculus Pearson Education © 2009.
The Relational Calculus (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)
Copyright © 2004 Ramez Elmasri and Shamkant Navathe The Relational Calculus The main reference of this presentation is the textbook and PPT from : Elmasri.
CS 285- Discrete Mathematics Lecture 4. Section 1.3 Predicate logic Predicate logic is an extension of propositional logic that permits concisely reasoning.
1 Copyright © Kyu-Young Whang Relational Calculus Chapter 4, Part B.
Slide 6- 1 Additional Relational Operations Aggregate Functions and Grouping A type of request that cannot be expressed in the basic relational algebra.
CSC271 Database Systems Lecture # 7. Summary: Previous Lecture  Relational keys  Integrity constraints  Views.
Albert Gatt LIN3021 Formal Semantics Lecture 3. Aims This lecture is divided into two parts: 1. We make our first attempts at formalising the notion of.
Database Management Systems, R. Ramakrishnan1 Relational Calculus Chapter 4, Part B.
Section 1.4. Propositional Functions Propositional functions become propositions (and have truth values) when their variables are each replaced by a value.
1 Schema for Student Registration System Student Student (Id, Name, Addr, Status) Professor Professor (Id, Name, DeptId) Course Course (DeptId, CrsCode,
Chapter 5 Relational Algebra and Relational Calculus Pearson Education © 2014.
SQL: Additional Notes. 2 Example 5.3 Use of DISTINCT List the property numbers of all properties that have been viewed. SELECT propertyNo FROM Viewing;
LECTURE THREE RELATIONAL ALGEBRA 11. Objectives  Meaning of the term relational completeness.  How to form queries in relational algebra. 22Relational.
CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
Relational Calculus Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4.
Relational Algebra & Calculus
DATABASE SYSTEM.
Relational Algebra and Relational Calculus
Relational Algebra & Calculus
Relational Calculus Chapter 4, Part B
Presentation transcript:

Relational Calculus

Relational calculus query specifies what is to be retrieved rather than how to retrieve it. – No description of how to evaluate a query. In first-order logic (or predicate calculus), predicate is a truth-valued function with arguments. When we substitute values for the arguments, function yields an expression, called a proposition, which can be either true or false. 2

Relational Calculus If predicate contains a variable (e.g. ‘x is a member of staff’), there must be a range for x. When we substitute some values of this range for x, proposition may be true; for other values, it may be false. When applied to databases, relational calculus has forms: tuple and domain. 3

Tuple Relational Calculus Interested in finding tuples for which a predicate is true. Based on use of tuple variables. Tuple variable is a variable that ‘ranges over’ a named relation: i.e., variable whose only permitted values are tuples of the relation. Specify range of a tuple variable S as the Staff relation as: Staff(S) To find set of all tuples S such that P(S) is true: {S | P(S)} 4

Instances of Branch and Staff Relations 5

Tuple Relational Calculus - Example To find details of all staff earning more than £10,000: {S | Staff(S)  S.salary > 10000} To find a particular attribute, such as salary, write: {S.salary | Staff(S)  S.salary > 10000} 6

Tuple Relational Calculus Can use two quantifiers to tell how many instances the predicate applies to: – Existential quantifier  (‘there exists’) – Universal quantifier  (‘for all’) Tuple variables qualified by  or  are called bound variables, otherwise called free variables. 7

Tuple Relational Calculus Existential quantifier used in formulae that must be true for at least one instance, such as: Staff(S)  (  B)(Branch(B)  (B.branchNo = S.branchNo)  B.city = ‘London’) Means ‘There exists a Branch tuple with same branchNo as the branchNo of the current Staff tuple, S, and is located in London’. 8

Tuple Relational Calculus Universal quantifier is used in statements about every instance, such as:  B) (Branch(B)  B.city  ‘Paris’) Means ‘For all Branch tuples, the address is not in Paris’. Can also use ~(  B) (B.city = ‘Paris’) which means ‘There are no branches with an address in Paris’. 9

Tuple Relational Calculus Formulae should be unambiguous and make sense. A (well-formed) formula is made out of atoms: R ( S i ), where S i is a tuple variable and R is a relation S i.a 1  S j.a 2 S i.a 1  c Can recursively build up formulae from atoms: An atom is a formula If F 1 and F 2 are formulae, so are their conjunction, F 1  F 2 ; disjunction, F 1  F 2 ; and negation, ~F 1 If F is a formula with free variable X, then (  X)(F) and (  X)(F) are also formulae. 10

Example - Tuple Relational Calculus List the names of all managers who earn more than £25,000. {S.fName, S.lName | Staff(S)  S.position = ‘Manager’  S.salary > 25000} List the staff who manage properties for rent in Glasgow. {S | Staff(S)  (  P) (PropertyForRent(P)  (P.staffNo = S.staffNo)  P.city = ‘Glasgow’)} 11

Tuple Relational Calculus Expressions can generate an infinite set. For example: {S | ~Staff(S)} To avoid this, add restriction that all values in result must be values in the domain of the expression. 12

Relation Between SQL and TRC Target T corresponds to SELECT list: the query result contains the entire tuple Body split between two clauses: – Teaching – Teaching(T) corresponds to FROM clause – T.Semester = ‘F2000’ corresponds to WHERE clause Teaching {T | Teaching(T) AND T.Semester = ‘F2000’} SELECT * Teaching FROM Teaching T WHERE T.Semester = ‘F2000’ 13

Query Result The result of a TRC query with respect to a given database is the set of all choices of tuples for the variable T that make the query condition a true statement about the database. 14

Relation Between TRC and SQL (cont’d) List the names of professors who have taught MGT123 – In TRC: ProfessorTeaching {P.Name | Professor(P) AND  T  Teaching (P.Id = T.ProfId AND T.CrsCode = ‘MGT123’) } – In SQL: SELECT P.Name ProfessorTeaching FROM Professor P, Teaching T WHERE P.Id = T.ProfId AND T.CrsCode = ‘MGT123’ Core of SQL is merely a syntactic sugar on top of TRC 15

What Happened to Quantifiers in SQL? SQL has no quantifiers: how come? It uses conventions: – Convention 1. Universal quantifiers are not allowed – Convention 2. Make existential quantifiers implicit: Any tuple variable that does not occur in SELECT is assumed to be implicitly quantified with  Compare: ProfessorTeaching … } {P.Name | Professor(P) AND  T  Teaching … } and SELECT P.Name ProfessorTeaching FROM Professor P, Teaching T … … … Implicit  16

SQL uses a subset of TRC with simplifying conventions for quantification Restricts the use of quantification and negation (so TRC is more general in this respect) SQL uses aggregates, which are absent in TRC (and relational algebra, for that matter). SQL is extended with relational algebra operators (MINUS, UNION, JOIN, etc.) – This is just more syntactic sugar, but it makes queries easier to write Relation Between TRC and SQL (cont’d) 17

The Role of Relational Algebra in a DBMS 18

Correspondence Between SQL and Relational Algebra List courses taught in S2000 Tuple variables clarify meaning. Join condition “C.CrsCode=T.CrsCode” – relates facts to each other Selection condition “ T.Sem=‘S2000’ ” – eliminates irrelevant rows Equivalent (using natural join) to: SELECT C.CrsName CourseTeaching FROM Course C, Teaching T WHERE C.CrsCode=T.CrsCode AND T.Sem=‘S2000’ Course  CrsName (Course Teaching  Sem=‘S2000’ (Teaching) ) CourseTeaching  CrsName (  Sem=‘S2000’ (Course Teaching) ) 19