Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999.

Slides:



Advertisements
Similar presentations
พีชคณิตแบบสัมพันธ์ (Relational Algebra) บทที่ 3 อ. ดร. ชุรี เตชะวุฒิ CS (204)321 ระบบฐานข้อมูล 1 (Database System I)
Advertisements

COMP 5138 Relational Database Management Systems Semester 2, 2007 Lecture 5A Relational Algebra.
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Tallahassee, Florida, 2014 COP4710 Database Systems Relational Algebra Fall 2014.
Chapter 6 Additional Relational Operations Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Relational Algebra 1 Chapter 5.1 V3.0 Napier University Dr Gordon Russell.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
Database Systems Chapter 6 ITM Relational Algebra The basic set of operations for the relational model is the relational algebra. –enable the specification.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
Relational Model and Relational Algebra Rose-Hulman Institute of Technology Curt Clifton.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
Database Systems: A Practical Approach to Design, Implementation and Management International Computer Science S. Carolyn Begg, Thomas Connolly Lecture.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
Relational Algebra Chapter 4 - part I. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
The Relational Algebra and Calculus. Relational Algebra Overview Relational algebra is the basic set of operations for the relational model These operations.
Databases Illuminated
RELATIONAL ALGEBRA Objectives
Relational Model & Relational Algebra. 2 Relational Model u Terminology of relational model. u How tables are used to represent data. u Connection between.
1 The Relational Data Model, Relational Constraints, and The Relational Algebra.
Relational Algebra Example Database Application (COMPANY) Relational Algebra –Unary Relational Operations –Relational Algebra Operations From Set Theory.
CHAPTER 2 RELATIONAL DATA MODEL. DEFINITION OF DATABASE Shared collection of logically related data (and a description of this data), designed to meet.
Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 2 : The relational model McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Chapter.
Relational Algebra - Chapter (7th ed )
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra and Calculus.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra.
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
1 The Relational Database Model. 2 Learning Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical.
METU Department of Computer Eng Ceng 302 Introduction to DBMS The Relational Algebra by Pinar Senkul resources: mostly froom Elmasri, Navathe and other.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra.
Relational Algebra – Part 2
1.1 CAS CS 460/660 Introduction to Database Systems Relational Algebra.
The Relational Algebra and Calculus
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Intro to Relational.
Chapter 6 The Relational Algebra Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
Chapter 5 Relational Algebra and Relational Calculus Pearson Education © 2009.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 4 Relational Algebra.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Chapter 2 Introduction to Relational Model. Example of a Relation attributes (or columns) tuples (or rows) Introduction to Relational Model 2.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Al-Maarefa College for Science and Technology INFO 232: Database systems Chapter 3 “part 2” The Relational Algebra and Calculus Instructor Ms. Arwa Binsaleh.
Advanced Relational Algebra & SQL (Part1 )
Chapter 2: Intro to Relational Model. 2.2 Example of a Relation attributes (or columns) tuples (or rows)
The Relational Algebra. Slide 6- 2 Outline Relational Algebra Unary Relational Operations Relational Algebra Operations From Set Theory Binary Relational.
Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.
CSC271 Database Systems Lecture # 7. Summary: Previous Lecture  Relational keys  Integrity constraints  Views.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Chapter 6 The Relational Algebra Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Intro to Relational.
LECTURE THREE RELATIONAL ALGEBRA 11. Objectives  Meaning of the term relational completeness.  How to form queries in relational algebra. 22Relational.
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 6 th Edition Chapter 8: Relational Algebra.
Ritu CHaturvedi Some figures are adapted from T. COnnolly
Database Systems Chapter 6
Relational Algebra Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4.
COMP3017 Advanced Databases
Module 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
The Relational Algebra and Relational Calculus
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Example of a Relation attributes (or columns) tuples (or rows)
Chapter 2: Intro to Relational Model
Unit Relational Algebra 1
Presentation transcript:

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Chapter 3 Relational algebra and calculus

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Query languages for relational databases Operations on databases: –queries: "read" data from the database –updates: change the content of the database Both can be modeled as functions from databases to databases Foundations can be studied with reference to query languages: –relational algebra, a "procedural" language –relational calculus, a "declarative" language –(briefly) Datalog, a more powerful language Then, we will study SQL, a practical language (with declarative and procedural features) for queries and updates

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Relational algebra A collection of operators that –are defined on relations –produce relations as results and therefore can be combined to form complex expressions Operators –union, intersection, difference –renaming –selection –projection –join (natural join, cartesian product, theta join)

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Union, intersection, difference Relations are sets, so we can apply set operators However, we want the results to be relations (that is, homogeneous sets of tuples) Therefore: –it is meaningful to apply union, intersection, difference only to pairs of relations defined over the same attributes

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Union Graduates Managers Graduates  Managers

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Intersection Graduates Managers Graduates  Managers

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Difference Graduates Managers Graduates - Managers

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Paternity  Maternity ??? PaternityMaternity A meaningful but impossible union the problem: Father and Mother are different names, but both represent a "Parent" the solution: rename attributes

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Renaming unary operator "changes attribute names" without changing values removes the limitations associated with set operators notation: –  Y  X. (r) example: –  Parent  Father. (Paternity) if there are two or more attributes involved then ordering is meaningful: –  Location,Pay  Branch,Salary. (Employees)

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Renaming, example Paternity  Parent  Father. (Paternity)

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Renaming and union PaternityMaternity  Parent  Father. (Paternity)   Parent  Mother. (Maternity)

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Renaming and union, with more attributes Employees  Location,Pay  Branch,Salary (Employees)   Location,Pay  Factory, Wages (Staff) Staff

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Selection and projection Two unary operators, in a sense orthogonal: –selection for "horizontal" decompositions –projection for "vertical" decompositions A B C Selection  A B C Projection  A B

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Selection Produce results –with the same schema as the operand –with a subset of the tuples (those that satisfy a condition) Notation: –  F (r) Semantics: –  F (r) = { t | t  r and t satisfies F}

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Selection, example Employees  Age 4000 (Employees)

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Selection, another example Citizens  PlaceOfBirth=Residence (Citizens)

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Projection Produce results –over a subset of the attributes of the operand –with values from all its tuples Notation (given a relation r(X) and a subset Y of X): –  Y (r) Semantics: –  Y (r) = { t[Y] | t  r }

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Projection, example Employees  Surname, FirstName (Employees)

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Projection, another example Employees  Department, Head (Employees)

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Cardinality of projections The result of a projections contains at most as many tuples as the operand It can contain fewer, if several tuples "collapse"  Y (r) contains as many tuples as r if and only if Y is a superkey for r; –this holds also if Y is "by chance" (not defined as a superkey in the schema, but superkey for the specific instance), see the example

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Tuples that collapse Students  Surname, DegreeProg (Students)

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Tuples that do not collapse, "by chance" Students  Surname, DegreeProg (Students)

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Join The most typical operator in relational algebra allows to establish connections among data in different relations, taking into advantage the "value-based" nature of the relational model Two main versions of the join: –"natural" join: takes attribute names into account –"theta" join They are all denoted by the symbol 

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone A natural join r1r1 r2r2 r 1  r 2

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Natural join: definition r 1 (X 1 ), r 2 (X 2 ) r 1  r 2 (natural join of r 1 and r 2 ) is a relation on X 1 X 2 (the union of the two sets): { t on X 1 X 2 | t [X 1 ]  r 1 and t [X 2 ]  r 2 } or, equivalently { t on X 1 X 2 | exist t 1  r 1 and t 2  r 2 with t [X 1 ] = t 1 and t [X 2 ] = t 2 }

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Natural join: comments The tuples in the result are obtained by combining tuples in the operands with equal values on the common attributes The common attributes often form a key of one of the operands (remember: references are realized by means of keys, and we join in order to follow references)

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Another natural join Offences  Cars

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Yet another join Compare with the union: –the same data can be combined in various ways PaternityMaternity Paternity  Maternity

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Joins can be "incomplete" If a tuple does not have a "counterpart" in the other relation, then it does not contribute to the join ("dangling" tuple) r1r1 r2r2 r 1  r 2

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Joins can be empty As an extreme, we might have that no tuple has a counterpart, and all tuples are dangling r1r1 r2r2 r 1  r 2

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone The other extreme If each tuple of each operand can be combined with all the tuples of the other, then the join has a cardinality that is the product of the cardinalities of the operands r1r1 r2r2 r 1  r 2

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone How many tuples in a join? Given r 1 (X 1 ), r 2 (X 2 ) the join has a cardinality between zero and the products of the cardilnalities of the operands: 0  | r 1  r 2 |  | r 1 |  | r 2 | (| r | is the cardinality of relation r) moreover: –if the join is complete, then its cardinality is at least the maximum of | r 1 | and | r 2 | –if X 1  X 2 contains a key for r 2, then | r 1  r 2 |  | r 1 | –if X 1  X 2 is the primary key for r 2, and there is a referential constraint between X 1  X 2 in r 1 and such a key, then | r 1  r 2 | = | r 1 |

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Outer joins A variant of the join, to keep all pieces of information from the operands It "pads with nulls" the tuples that have no counterpart Three variants: –"left": only tuples of the first operand are padded –"right": only tuples of the second operand are padded –"full": tuples of both operands are padded

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Outer joins r1r1 r2r2 r 1  LEFT r 2 r 1  RIGHT r 2 r 1  FULL r 2

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone N-ary join The natural join is –commutative: r 1  r 2 = r 2  r 1 –associative: (r 1  r 2 )  r 3 = r 1  (r 2  r 3 ) Therefore, we can write n-ary joins without ambiguity: r 1  r 2  …  r n

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone N-ary join r1r1 r2r2 r3r3 r 1  r 2  r 3

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Cartesian product The natural join is defined also when the operands have no attributes in common in this case no condition is imposed on tuples, and therefore the result contains tuples obtained by combining the tuples of the operands in all possible ways

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Cartesian product: example Employees Projects Employes  Projects

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Theta-join In most cases, a cartesian product is meaningful only if followed by a selection: –theta-join: a derived operator r 1  F r 2 =  F (r 1  r 2 ) –if F is a conjunction of equalities, then we have an equi-join

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Equi-join: example Employees Projects Employes  Project=Code Projects

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Queries A query is a function from database instances to relations Queries are formulated in relational algebra by means of expressions over relations

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone A database for the examples Employees Supervision

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Example 1 Find the numbers, names and ages of employees earning more than 40 thousand.

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Example 2 Find the registration numbers of the supervisors of the employees earning more than 40 thousand

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Example 3 Find the names and salaries of the supervisors of the employees earning more than 40 thousand

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Example 4 Find the employees earning more than their respective supervisors, showing registration numbers, names and salaries of the employees and supervisors

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Example 5 Find the registration numbers and names of the supervisors whose employees all earn more than 40 thousand

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Algebra with null values  Age>30 (People) which tuples belong to the result? The first yes, the second no, but the third? People

Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone Material on relational calculus and Datalog will be provided in the near future. Please contact Paolo Atzeni for more information