Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operations in the Relational Model These operation can be expressed in an algebra, called “relational algebra”. In this algebra relations are the operands.

Similar presentations


Presentation on theme: "Operations in the Relational Model These operation can be expressed in an algebra, called “relational algebra”. In this algebra relations are the operands."— Presentation transcript:

1 Operations in the Relational Model These operation can be expressed in an algebra, called “relational algebra”. In this algebra relations are the operands and by applying any of operators to be described later, we can build progressively more complex expressions.

2 Operations Operations of relational algebra fall into four broad classes: 1.The usual set operations union intersection difference 2.Operations that remove parts of a relation: selection eliminates some rows(tuples) projection eliminates some columns 3.Operations that combine the tuples of two relations: Cartesian product, which pairs the tuples of two relations in all possible ways join” operation, which selectively pair tuples from two relations. 4.An operation called “renaming”.

3 Conditions for Set Operations on Relations We can apply three most common operations on sets- union, intersection, difference- to relations provided that these two conditions hold: 1.R and S must have schemas with identical sets of attributes. 2.Before applying the operations, the columns of R and S must be ordered so that the order of attributes is the same for both relations.

4 Set Operations on Relations R  S, the union of R and S, is the set of elements that are in R or S or both. R  S, the intersection of R and S, is the set of elements that are in both R and S. R  S, the difference of R and S, is the set of elements that are in R but not in S. Note that R  S is different from S  R.

5 Projection Produces from a relation R a new relation that has only some of R’s columns.  A1, A2,…,An (R) is a relation that has only the columns for attributes A 1, A 2,…, A n of R. Example: Compute the expression  title, year, length (Movies) on the table: title yearlengthfilmType studioName producerC# Star wars 1977124color Fox 12345 Mighty Ducks 1991104color Disney 67890 Wayne’s World 199295color Paramount 99999

6 Example (Continued) Resulting relation: titleyearlength Star wars1977124 Mighty Ducks1991104 Wayne’s World199295 What about  filmtype (Movies)

7 Selection The selection operator, applied to a relation R, produces a new relation with a subset of R’s tuples. The tuples in the result are those that satisfy some condition C. We denote this operation  C ( R ). The schema for the resulting relation is the same as R’s schema. Example: The expression  length  100 (Movie) is: title yearlengthfilmType studioName producerC# Star wars 1977124color Fox 12345 Mighty Ducks 1991104color Disney 67890

8 Example Suppose we want the set of tuples in the previous relation Movie such that represent Fox movies at least 100 minutes long. We can get these with a more complicated condition:  length  100 And studioName=’Fox’ (Movie) The tuple title yearlengthfilmType studioName producerC# Star wars 1977124color Fox 12345 Is the only one in the resulting relation.

9 Cartesian Product The Cartesian Product (or just product) of two sets R and S is the set of pairs that can be formed by choosing the first element of the pair to be any element of R and the second an element of S. This product is denoted as R  S. The relation schema for the resulting schema is the union of the schemas for R and S. However, if R and S should happen to have some attributes in common, then we need to invent new names for at least one of each pair identical attributes. Example: RA BSB C D 1 22 5 6 3 44 7 8 9 10 11

10 Example(Continued) The resulting relation will be: R  S AR.BS.BCD 12256 12478 1291011 34256 34478 3491011

11 Natural Join Denoted as R S. Let A 1, A 2,…,A n be the attributes in both the schema of R and the schema of S. Then a tuple r from R and a tuple s from S are successfully paired if and only if r and s agree on each of the attributes A 1, A 2, …, A n. Example: The natural join of the relation R and S from previous example is: ABCD 1256 3478

12 Theta-Joins R C S. The result of this operation is constructed as follows: Take the product of R and S. Select from the product only those tuples that satisfy the condition C. Schema for the result is the union of the schema of R and S with, “R” or “S” prefix as necessary.

13 Example Compute the natural and theta join for two relations U and V: ABCB CD 123234 678235 9787810 Relation U Relation V

14 Example A BC D AU.B U.C V.B V.C D 1 2 3 4 1 2 3 2 3 4 1 2 3 5 1 2 3 2 3 5 6 7 8 10 1 2 3 7 8 10 9 7 8 10 6 7 8 7 8 10 Result U V 9 7 8 7 8 10 Result of U A<D V

15 Combing Operations to Form Queries “What are the title and years of movies made by Fox that are at least 100 minutes long?” One way to compute the answer to this query is: Select those Movie tuples that have length  100. Select those Movie tuples that have studioName =‘Fox’. Compute the intersection of first and second steps. Project the relation from the third step onto attributes title and year.

16 Example (Continued)  title,year (  length  100 (Movie)   studioName=‘Fox’ (Movie))  title,year   length  100  studioName=‘Fox’Movie

17 Another Example Consider two relations Movie1 and Movie2, With schemas: Movie1(title, year, length, filmType, studioName) Movie2(title, year, starName) Suppose we want to know: “Find the stars of the movies that are at least 100 minutes long.” First we join the two relations: Movie1, Movie2 Second we select movies with length at least 100 min. Then we project the starName.

18 Renaming Operator In order to control the names of the attributes used for relations that are constructed by one or more applications of the relational algebra operations, it is often convenient to use an operator that explicitly renames relations. We shall use the operator  S(A1,A2,…,An) (R) to rename a relation R. The resulting relation has exactly the same tuples as R, but the name of the relation is S. Moreover, the attributes of the result relation S are named A 1, A 2, …, A n, in order from the left.

19 Problem Product(maker, model, type) PC(model, speed, ram, hd, rd, price) Laptop(model, speed, ram, hd, screen, price) Printer(model, color, type, price) (Exercise 5.2.1)

20

21 Problem a) What PC models have a speed of at least 1000? b) Which manufacturers make laptops with a hard disk of at least one gigabyte? c) Find the model number and price of all products (of any type) made by manufacturer B. d) Find the model numbers of all color laser printers. e) Find those manufacturers that sell Laptops, but not PC's. !f) Find those hard-disk sizes that occur in two or more PC's. !g) Find those pairs of PC models that have both the same speed and RAM. A pair should be listed once. !!h)Find those manufacturers of at least two different computers (PC or Laptops) with speed of at least 700.


Download ppt "Operations in the Relational Model These operation can be expressed in an algebra, called “relational algebra”. In this algebra relations are the operands."

Similar presentations


Ads by Google