CSC271 Database Systems Lecture # 8. Summary: Previous Lecture  Relation algebra and operations  Selection (Restriction), projection  Union, set difference,

Slides:



Advertisements
Similar presentations
Relational Algebra and Relational Calculus
Advertisements

พีชคณิตแบบสัมพันธ์ (Relational Algebra) บทที่ 3 อ. ดร. ชุรี เตชะวุฒิ CS (204)321 ระบบฐานข้อมูล 1 (Database System I)
CSC271 Database Systems Lecture # 11.
RELATIONAL ALGEBRA Lecture Relational Algebra Operations to manipulate relations. Used to specify retrieval requests (queries). Query results in.
CSC271 Database Systems Lecture # 13. Summary: Previous Lecture  Grouping through GROUP BY clause  Restricted groupings  Subqueries  Multi-Table queries.
Database Systems Chapter 6 ITM Relational Algebra The basic set of operations for the relational model is the relational algebra. –enable the specification.
Relational Algebra - Basic Operations CS263 Lecture 11.
Lesson II The Relational Model © Pearson Education Limited 1995, 2005.
1 Minggu 3, Pertemuan 5 Relational Algebra (Cont.) Matakuliah: T0206-Sistem Basisdata Tahun: 2005 Versi: 1.0/0.0.
Chapter 5 Relational Algebra and Relational Calculus.
Instructor: Mohamed Eltabakh
Database Systems: A Practical Approach to Design, Implementation and Management International Computer Science S. Carolyn Begg, Thomas Connolly Lecture.
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.
1-1 Thomas Connolly and Carolyn Begg’s Database Systems: A Practical Approach to Design, Implementation, and Management Chapter 4 Part One: Relational.
Relational Algebra & Relational Calculus Dale-Marie Wilson, Ph.D.
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.
CSC 240 (Blum)1 Joins. CSC 240 (Blum)2 Relational algebra Recall relational algebra was the study of actions that are performed on one or more tables.
Chapter 3 Section 3.4 Relational Database Operators
SQL: Overview SQL Overview © Pearson Education Limited 1995, 2005.
CSC271 Database Systems Lecture # 12. Summary: Previous Lecture  Row selection using WHERE clause  WHERE clause and search conditions  Sorting results.
Relational Algebra - Chapter (7th ed )
Relational Algebra Chapter 4 CIS 458 Sungchul Hong.
CSC271 Database Systems Lecture # 9. Summary: Previous Lecture  Cartesian product  Join  Theta join, equijoin, natural join  Outer join (left, right,
Chapter 6 SQL: Data Manipulation (Advanced Commands) Pearson Education © 2009.
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.
Chapter 5 SQL: Data Manipulation © Pearson Education Limited 1995, 2005.
Bayu Adhi Tama, ST., MTI. Introduction Relational algebra and relational calculus are formal languages associated with the relational.
C HAPTER 4 Relational Algebra and Relational Calculus Transparencies © Pearson Education Limited 1995,
Advanced Database Systems
CS 3630 Database Design and Implementation. 2 DreamHome Branch (branchNo, street, city, state, zipcode, phone1, phone2, phone3) Staff (staffNo, firstName,
1 Pertemuan > > Matakuliah: >/ > Tahun: > Versi: >
Chapter 6 The Relational Algebra Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
Chapter 5 Relational Algebra and Relational Calculus Pearson Education © 2009.
Chapter 5 Relational Algebra Pearson Education © 2014.
1 Geog 357 – Introduction to GIS The Relational Language.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
1 Pertemuan > > Matakuliah: >/ > Tahun: > Versi: >
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
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.
CSC271 Database Systems Lecture # 23. Summary: Previous Lecture  Database design using ER modeling  Concepts of ER model  Entities  Relationships.
2 Database Design Chapter Operations of the Relational Model Updates: changes the database’s state. Insert Delete Update/modify Retrievals: does.
CSCI 6315 Applied Database Systems Review for Midterm Exam I Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
April 20022CS3X1 Database Design Relational algebra John Wordsworth Department of Computer Science The University of Reading Room.
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.
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
Relational Algebra National University of Computer and Emerging Sciences Lecture # 6 June 30,2012.
Chapter 4 Relational Algebra Chapter 4 in Textbook.
Teacher Workshop Database Design Pearson Education © 2014.
Chapter 11 SQL: Data Manipulation
Ritu CHaturvedi Some figures are adapted from T. COnnolly
Relational Algebra Lecture 2.
COMP3017 Advanced Databases
CS 3630 Database Design and Implementation
Relational Algebra and Relational Calculus
The Relational Database Model
Chapter Name SQL: Data Manipulation
DATABASE SYSTEM.
Chapter Name SQL: Data Manipulation
The Relational Algebra
Chapter Name SQL: Data Manipulation Transparencies JOINS
Relational Algebra and Relational Calculus
Chapter Name SQL: Data Manipulation
Presentation transcript:

CSC271 Database Systems Lecture # 8

Summary: Previous Lecture  Relation algebra and operations  Selection (Restriction), projection  Union, set difference, intersection

Cartesian Product  R X S  Defines a relation that is the concatenation of every tuple of relation R with every tuple of relation S  For example, if relation R has I tuples and N attributes and the relation S has J tuples and M attributes, the Cartesian product relation will contain (I * J) tuples with (N+M) attributes  For the attributes with the same name, the attribute names are prefixed with the relation name to maintain the uniqueness of attribute names within a relation

Example: Cartesian Product  List the names and comments of all clients who have viewed a property for rent  We need to combine two relations (Client, Viewing) Π clinetNo, fName, lName (Client) X Π clientNo, propertyNo, comment (Viewing)

Example: Cartesian Product

 Resultant relation contains more information than we require  To obtain the required list, we need to carry out a Selection operation on this relation to extract those tuples where Client.clientNo= Viewing.clientNo σ Client.clientNo= Viewing.clientNo ((Π clientNo, fName, lName (Client)) × (Π clientNo, propertyNo, comment (Viewing)))

Example: Cartesian Product

Decomposing Complex Operations  The relational algebra operations can be of arbitrary complexity  We can decompose such operations into a series of smaller relational algebra operations and give a name to the results of intermediate expressions  We use the assignment operation, denoted by ←, to name the results of a relational algebra operation

Example: Decomposition  Previous example of Cartesian Product  Decomposition could be as follows: TempV(clientNo, propertyNo, comment) ← Π clientNo, propertyNo, comment (Viewing) TempC(clientNo, fName, lName) ← Π clientNo, fName, lName (Client) Comment(clientNo, fName, lName, vclientNo, propertyNo, comment) ← TempC × TempV Result ← σ clientNo= vclientNo (Comment)

Rename Operation   S (E) or  S(a1, a2,..., an) (E)  The Rename operation provides a new name S for the expression E, and optionally names the attributes as a1, a2,..., an  Examples:  R (Client) ← Π clientNo, fName, lName (Client))  R(CNo, FirstName, LastName) ( Client )← Π clientNo, fName, lName (Client)

Join Operations  Join is a derivative of Cartesian Product  Equivalent to performing a Selection, using join predicate as selection formula, over Cartesian product of the two operand relations  One of the most difficult operations to implement efficiently in an RDBMS and one reason why RDBMSs have intrinsic performance problems

Join Operations  Various forms of join operation  Theta join  Equijoin (a particular type of Theta join)  Natural join  Outer join  Semijoin

Theta join (  -join)  R F S  Defines a relation that contains tuples satisfying the predicate F from the Cartesian product of R and S  The predicate F is of the form R.ai  S.bi where  may be one of the comparison operators (, >, =, ≠)  We can rewrite the Theta join in terms of the basic Selection and Cartesian product operations: R F S = σ F (R × S)  As with Cartesian product, the degree of a Theta join is the sum of the degrees of the operand relations R and S

Equijoin  R F(=) S  Defines a relation that contains tuples satisfying the predicate F from the Cartesian product of R and S  The predicate F is of the form R.ai  S.bi where  may be assigned only equality ( = ) operator

Example: Equijoin  List the names and comments of all clients who have viewed a property for rent  Same example/result but now using Equijoin instead of Cartesian product and Selection (Π clientNo, fName, lName (Client)) Client.clientNo= Viewing.clientNo (Π clientNo, propertyNo, comment (Viewing)) or Result ← TempC TempC.clientNo = TempV.clientNo TempV

Example: Equijoin

Natural Join  R S  An Equijoin of the two relations R and S over all common attributes x  One occurrence of each common attribute is eliminated from the result  The Natural join operation performs an Equijoin over all the attributes in the two relations that have the same name  The degree of a Natural join is the sum of the degrees of the relations R and S less the number of attributes in x

Example: Natural Join  List the names and comments of all clients who have viewed a property for rent  Same example using Natural join but produces a relation with one occurrence of clientNo attribute (Π clientNo, fName, lName (Client)) (Π clientNo, propertyNo, comment (Viewing)) Or Result ← TempC TempV

Example: Natural Join

Outer Join  We may want tuples from one of the relations to appear in the result even when there are no matching values in the other relation  This may be accomplished using the Outer join

Types of Outer Join  Types of outer join  Left outer join ( )  Keeps every tuple in the left-hand relation in the result  Right outer join ( )  Keeps every tuple in the right-hand relation in the result  Full outer join ( )  Keeps all tuples in both relations, padding tuples with nulls when no matching tuples are found

Left Outer Join  R S  (Left) outer join is join in which tuples from R that do not have matching values in common attributes of S are also included in result relation  Missing values in the second relation are set to null  To display rows in the result that do not have matching values in the join attributes

Example: Left Outer Join  Produce a status report on property viewings  We will produce a relation consisting of the properties that have been viewed with comments and those that have not been viewed using left outer join: (Π propertyNo, street, city (PropertyForRent)) Viewing

Example: Left Outer Join

Semijoin  R F S  Defines a relation that contains the tuples of R that participate in the join of R with S  The Semijoin operation performs a join of the two relations and then projects over the attributes of the first operand  Useful for computing joins in distributed systems  We can rewrite the Semijoin using the Projection and Join operations: R F S = Π A (R F S) Where A is the set of all attributes for R

Example: Semijoin  List complete details of all staff who work at the branch in Glasgow Staff Staff.branchNo = Branch.branchNo ( σ city= ‘Glasgow’ (Branch))

Division  R ÷ S  Assume relation R is defined over the attribute set A and relation S is defined over the attribute set B such that B ⊆ A (B is a subset of A)  Let C= A− B, that is, C is the set of attributes of R that are not attributes of S  Division operation defines a relation over the attributes C that consists of set of tuples from R that match combination of every tuple in S

Division  Division operation can be expressed in terms of basic operations: T1←Π C (R) T2←Π C ((T1× S) − R) T ← T1− T2

Example: Division  Identify all clients who have viewed all properties with three rooms (Π clientNo, propertyNo (Viewing)) ÷ (Π propertyNo (σ rooms = 3 (PropertyForRent)))

Aggregate Operation  ℑ AL (R)  Applies aggregate function list, AL, to R to define a relation over the aggregate list  AL contains one or more (, ) pairs  Main aggregate functions are:  COUNT, SUM, AVG, MIN, and MAX

Example: Aggregate Operation  How many properties cost more than £350 per month to rent?  R (myCount) ℑ COUNT propertyNo (σ rent >350 (PropertyForRent))  Find the minimum, maximum, and average staff salary ρ R (myMin, myMax, myAverage) ℑ MIN salary, MAX salary, AVERAGE salary (Staff)

Grouping Operation  GA ℑ AL (R)  Groups the tuples of relation R by the grouping attributes, GA, and then applies the aggregate function list AL to define a new relation  AL contains one or more (, ) pairs  The resulting relation contains the grouping attributes, GA, along with the results of each of the aggregate functions

Example: Grouping Operation  Find the number of staff working in each branch and the sum of their salaries ρ R (branchNo, myCount, mySum) branchNo ℑ COUNT staffNo, SUM salary (Staff)

Summary  Cartesian product  Join  Theta join, equijoin, natural join  Outer join (left, right, full)  Semijoin  Division  Aggregate and grouping operations

References  All the material (slides, diagrams etc.) presented in this lecture is taken (with modifications) from the Pearson Education website : 