Presentation is loading. Please wait.

Presentation is loading. Please wait.

April 20022CS3X1 Database Design Relational algebra John Wordsworth Department of Computer Science The University of Reading Room.

Similar presentations


Presentation on theme: "April 20022CS3X1 Database Design Relational algebra John Wordsworth Department of Computer Science The University of Reading Room."— Presentation transcript:

1 April 20022CS3X1 Database Design Relational algebra John Wordsworth Department of Computer Science The University of Reading J.B.Wordsworth@rdg.ac.uk Room 129, Ext 6544

2 April 20022CS3X2 Lecture objectives Explain what relational algebra is. Describe the effect of projection and selection on a relation. Desribe the effect of union and difference on a poir of compatible relations. Explain how the Catrtesian product is used to combine relations. Distinguish the various kinds of join in relational algebra.

3 April 20022CS3X3 What is relational algebra? Relational algebra is a collection of operations on sets of tuples (ordered sets of values). A table in a relational model is regarded as a set of tuples (decorated with attribute names). Relational algebra operations can be used to express solutions to information problems. Relational algebra is the (formal) basis of most (user-friendly) DMLs.

4 April 20022CS3X4 Projection Projection: choosing one or more attributes (columns) from every row, discarding other columns. Duplicate rows? Remove them! The example shows a relation of degree 4 being projected over two attributes:  height,weight (Staff)

5 April 20022CS3X5 Selection Selection: choosing one or more complete rows according to a criterion stated as a logical expression on the attribute values The example shows three rows (tuples) being selected, by an expression such as  salary > 1000 (Staff)

6 April 20022CS3X6 Example of selection and projection Write down the result of the following:  Name = “Iain”  Name = “Kevin” (T1)  Height, Weight (  Name = “Iain” (T1)) T1

7 April 20022CS3X7 Union and difference Union: taking two relations A, B over the same attribute-set (with matching domains) and forming A  B Difference: taking two union-compatible relations A, B and forming C, where C = {x | x  A  x  B}

8 April 20022CS3X8 Cartesian product Takes two relations A, B and forms C such that: degree(C) = degree(A) + degree(B) card(C) = card(A). card(B) a tuple in C consists of a tuple of A concatenated with a tuple from B; C comprises all such pairings

9 April 20022CS3X9 Cartesian product = 

10 April 20022CS3X10 Cartesian product: oddities Note duplicate Dept columns Of the 8 rows in C, only 2 mean anything: –rows in which the Dept entries are the same There are 2 rows in B that don’t correspond to any row in A –rows in which Dept = “MV” and Dept = “BY” –nevertheless all tuples participate.

11 April 20022CS3X11 The theta-join Applies a selection predicate to a Cartesian product expression. The predicate must be a comparison of the values of two attributes, one from each table. Comparison means >, m, <, [, =, g An equijoin is a theta join with an equality. R predicate S = r predicate (R x S)

12 April 20022CS3X12 The natural join The two tables have at least one attribute in common. The equijoin predicate requires each common attribute to have the same value in both tables. Only one copy of each of the common attributes is retained.

13 April 20022CS3X13 Natural join example Borrower Book

14 April 20022CS3X14 Semijoin A semijoin is a projection of the attributes of the first table from one of the foregoing kinds of join. R predicate S = o A ( R predicate S ) where A is the set of attributes of R.

15 April 20022CS3X15 Outer join A left (right) outer join contains all the rows in the first (second) table extended with the attributes of the second (first) table. If the row of the first (second) table matched a row of the second (first) table, that row is extended with the values of the attributes in the corresponding row of the second (first) table. Otherwise it is extended with null values. A full outer join is possible.

16 April 20022CS3X16 Expressing solutions to problems What information do you need at the end? Where are these items to be found? How are the various relations connected? What is the right order to do the operations? –selection –projection –then join

17 April 20022CS3X17 Key points Relational algebra is the basis of most DMLs. Projection and selection are operations on a single table. Union and difference are operations on pairs of tables. The Cartesian product is the basis of several kinds of join operation.


Download ppt "April 20022CS3X1 Database Design Relational algebra John Wordsworth Department of Computer Science The University of Reading Room."

Similar presentations


Ads by Google