Presentation is loading. Please wait.

Presentation is loading. Please wait.

RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.

Similar presentations


Presentation on theme: "RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science."— Presentation transcript:

1 RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science

2 Database Scheme A relational database scheme, or schema, corresponds to a set of table definitions. Eg: product(p_id, name, category, description) supply(p_id, s_id, qnty_per_month) supplier(s_id, name, address, ph#) * remember the difference between a DB instance and a DB scheme.

3 SAMPLE SCHEMAS AND INSTANCES The Schemas: Sailors(sid: integer, sname: string, rating: integer, age: real) Boats(bid: integer, bname: string, color: string) Reserves(sid: integer, bid: integer, day: date) The Instances:

4 What is Relational Algebra? zRelational algebra is a procedural query language. zIt consists of the select, project, union, set difference, Cartesian product, and rename operations. zSet intersection, division, natural join, and assignment combine the fundamental operations. zSQL is based on relational algebra

5 zIt is an abstract language. We use it to express the set of operations that any relational query language must perform. zTwo types of operations: z 1.set-theoretic operations: tables are essentially sets of rows  2.native relational operations: focus on the structure of the rows Query languages are specialized languages for asking questions,or queries,that involve the data in database. What are the query languages ?

6 Query languages zprocedural vs. non-procedural zcommercial languages have some of both zwe will study: yrelational algebra ( which is procedural, i.e. tells you how to process a query ) yrelational calculus ( which is non-procedural i.e. tells what you want )

7 SEMANTICS OF THE SAMPLE RELATIONS zSailors: Entity set; lists the relevant properties of sailors. zBoats zBoats: Entity set; lists the relevant properties of boats. zReserves: Relationship set: links sailors and boats by describing the boat number and date for which a sailor made a reservation. Example of the declarative sentences for which rows stand: Row 1: “Sailor ’22’ reserved boat number ‘101’ on 10/10/98”.

8 Selection and Projection zSelection Operator : σ rating>8 (S2) Retrieves from the current instance of relation named S2 those rows where the value of the attribute ‘rating’ is greater than 8. Applying the above selection operator to the sample instance of S2 shown in figure 4.2 yields the relational instance on figure 4.4 as shown below: z π condition

9 Projection Operator π sname,rating (S2) Retrieves from the current instance of the relation named S2 those columns whose names are ‘sname’ and ‘rating’. Applying the above operator to the sample instance of S2 shown in figure 4.2 yields the relational instance on figure 4.5 as shown below: N. B.: Note that the projection operator can produce duplicate rows in the resulting instance.

10 - Projection Operator (cont’d) Similarly π age (S2) yields the following relational instance Note here the elimination of duplicates SQL would yield For π age (S2): age 35.0 55.0 35.0

11 Introduction z one of the two formal query languages of the relational model z collection of operators for manipulating relations z Operators: two types of operators y Set Operators: Union(  ),Intersection(  ), Difference(-), Cartesian Product (x)  New Operators: Select (  ), Project (  ), Join ( ⋈ )

12 Introduction – cont’d z A Relational Algebra Expression: a sequence of relational algebra operators and operands (relations), formed according to a set of rules. z The result of evaluating a relational algebra expression is a relation.

13 Selection z Denoted by  c ( R ) z Selects the tuples (rows) from a relation R that satisfy a certain selection condition c. z It is a unary operator z The resulting relation has the same attributes as those in R.

14 Example 1: SNOSNAMEAGESTATE S1MIKE21IL S2STEVE20LA S3MARY18CA S4MING19NY S5OLGA21NY S:  state=‘IL’ (S)

15 Example 2: CNOCNAMECREDITDEPT C1 Databas e 3CS C2 Statistic s 3MATH C3Tennis1SPORTS C4Violin4MUSIC C5Golf2SPORTS C6Piano5MUSIC C:  CREDIT  3 (C)

16 Example 3 SNOCNOGrade S1C190 S1C280 S1C375 S1C470 S1C5100 S1C660 S2C190 S2C280 S3C290 S4C280 S4C485 S4C5100 E:  SNO=‘S1’and CNO=‘C1’ (E)

17

18 Selection - Properties z Selection Operator is commutative  C1 (  C2 (R)) =  C2 (  C1 (R)) z The Selection is an unary operator, it cannot be used to select tuples from more than one relations.

19 Projection z Denoted by  L (R), where L is list of attribute names and R is a relation name or some other relational algebra expression. z The resulting relation has only those attributes of R specified in L. z The projection is also an unary operation.  Duplicate rows are not permitted in relational algebra. Duplication is removed from the result. zDuplicate rows can occur in SQL, though they may be controlled by explicit keywords.

20 Projection - Example z Example 1:  STATE (S) SNOSNAMEAGESTATE S1MIKE21IL S2STEVE20LA S3MARY18CA S4MING19NY S5OLGA21NY STATE IL LA CA NY

21 Projection - Example Example 2:  CNAME, DEPT (C) CNOCNAMECREDITDEPT C1 Databas e 3CS C2 Statistic s 3MATH C3Tennis1SPORTS C4Violin4MUSIC C5Golf2SPORTS C6Piano5MUSIC CNAMEDEPT Databas e CS Statistic s MATH TennisSPORTS ViolinMUSIC GolfSPORTS PianoMUSIC

22 Projection - Example Example 3:  S# (  STATE=‘NY' (S)) SNOSNAMEAGESTATE S1MIKE21IL S2STEVE20LA S3MARY18CA S4MING19NY S5OLGA21NY SNO S4 S5

23 SET Operations z UNION: R 1  R 2 z INTERSECTION: R 1  R 2 z DIFFERENCE: R 1 - R 2 z CARTESIAN PRODUCT: R 1  R 2

24 Union Compatibility z For operators , , -, the operand relations R1(A1, A2,..., An) and R2(B1, B2,..., Bn) must have the same number of attributes, and the domains of the corresponding attributes must be compatible; that is, dom(Ai)=dom(Bi) for i=1,2,...,n. z The resulting relation for , , or - has the same attribute names as the first operand relation R1 (by convention).

25 Union Compatibility - Examples z Are S(SNO, SNAME, AGE, STATE) and C(CNO, CNAME, CREDIT, DEPT) union compatible? z Are S(S#, SNAME, AGE, STATE) and C(CNO, CNAME, CREDIT_HOURS, DEPT_NAME) union compatible?

26

27 UNION, SET DIFFERENCE & SET INTERSECT zUnion puts all tuples of two relations in one relation. To use this operator, two conditions must hold: 1.The two relations must be of the same arity. 2.The domain of i th attribute of the two participating relation must be the same. zSet difference operator computes tuples that are in one relation, but not in another. zSet intersect operator computes tuples that are common in two relations: zThe five fundamental operations of the relational algebra are: select, project, cartesian product, Union, and set difference zAll other operators can be constructed using these operators

28 EXAMPLE zAssume a database with the following three relations: Sailors (sid, sname, rating) Boats (bid, bname, color) Reserve (sid, bid, date) zQuery 1: Find the bid of red colored boats:

29 EXAMPLE zAssume a database with the following three relations: Sailors (sid, sname, rating) Boats (bid, bname, color) Reserve (sid, bid, date) zQuery 1: Find the bid of red colored boats: y∏ bid (б color=red (Boats))

30 EXAMPLE zAssume a database with the following three relations: Sailors (sid, sname, rating) Boats (bid, bname, color) Reserve (sid, bid, date) zQuery 1: Find the name of sailors who have reserved Boat number 2.

31 EXAMPLE zAssume a database with the following three relations: Sailors (sid, sname, rating) Boats (bid, bname, color) Reserve (sid, bid, date) zQuery 1: Find the name of sailors who have reserved Boat number 2. y∏ sname (б bid=2 (Sailors (sid) Reserve))

32 EXAMPLE zAssume a database with the following three relations: Sailors (sid, sname, rating) Boats (bid, bname, color) Reserve (sid, bid, date) zQuery 1: Find the name of sailors who have reserved both a red and a green boat.

33 Union, Intersection, Difference z T= R U S : A tuple t is in relation T if and only if t is in relation R or t is in relation S z T = R  S: A tuple t is in relation T if and only if t is in both relations R and S z T= R - S :A tuple t is in relation T if and only if t is in R but not in S

34 Set-Intersection  Denoted by the symbol .  Results in a relation that contains only the tuples that appear in both relations. zR  S = R – (R – S) zSince set-intersection can be written in terms of set-difference, it is not a fundamental operation.

35 Examples A1A2 1Red 3White 4green B1B2 3White 2Blue RS

36 Examples A1A2 1Red 3White 4Green 2Blue A1A2 3White R  S R  S S - R B1B2 2Blue A1A2 1Red 4Green R - S

37 RELATIONAL ALGEBRA (SECTION 3.2)

38 RELATIONAL ALGEBRA INTRODUCTION zAssume the following two relations: Emp (SS#, name, age, salary, dno) Dept (dno, dname, floor, mgrSS#)  Relational algebra is a procedural query language, i.e., user must define both “ how ” and “ what ” to retrieve. zRelational algebra consists of a set of operators that consume either one or two relations as input. An operator produces one relation as its output. zUnary operators include: select, project, and rename zBinary operators include: cartesian product, equality join, natural join, join, semi-join, division, union, and set difference.

39 SELECT OPERATOR zSelect (б): selects tuples that satisfy a predicate; e.g., retrieve the employees whose salary is 30,000 б Salary=30,000 (Employee) zConjunctive ( ) and disjunctive ( ) selection predicates are allowed; e.g., retrieve employees whose salary is higher than 30,000 and are younger than 25 years old: б Salary>30,000 age<25 (Employee) zNote that only selection predicates are allowed. A selection predicate is either (1) a comparison (=, ≠, ≤, ≥, ) between an attribute and a constant (e.g., salary = 30,000) or (2) a comparison between two different attributes of the same relation (e.g., salary = age × 100). zNote: This operator is different than the SELECT command of SQL. < < <

40 EXAMPLE zEmp table: SS#NameAgeSalarydno 1Joe24200002 2Mary20250003 3Bob22270004 4Kathy30300005 5Shideh440001

41 EXAMPLE zEmp table: zб Salary=30,000 (Employee) SS#NameAgeSalarydno 1Joe24200002 2Mary20250003 3Bob22270004 4Kathy30300005 5Shideh440001

42 EXAMPLE zEmp table: zб Salary=30,000 (Employee) SS#NameAgeSalarydno 1Joe24200002 2Mary20250003 3Bob22270004 4Kathy30300005 5Shideh440001 SS#NameAgeSalarydno 4Kathy30300005

43 EXAMPLE zEmp table: zб Age>22 (Employee) SS#NameAgeSalarydno 1Joe24200002 2Mary20250003 3Bob22270004 4Kathy30300005 5Shideh440001

44 EXAMPLE zEmp table: zб Age>22 (Employee) SS#NameAgeSalarydno 1Joe24200002 2Mary20250003 3Bob22270004 4Kathy30300005 5Shideh440001 SS#NameAgeSalarydno 1Joe24200002 4Kathy30300005

45 PROJECT OPERATOR zProject (∏) retrieves a column. It is a unary operator that eliminate duplicates. e.g., name of employees: ∏ name (Employee) e.g., name of employees earning more than 30,000: ∏ name ( б Salary>30,000 (Employee))

46 EXAMPLE zEmp table: SS#NameAgeSalarydno 1Joe24200002 2Mary20250003 3Bob22270004 4Kathy30300005 5Shideh440001

47 EXAMPLE zEmp table: z∏ age (Emp) SS#NameAgeSalarydno 1Joe24200002 2Mary20250003 3Bob22270004 4Kathy30300005 5Shideh440001 Age 24 20 22 30 4

48 EXAMPLE zEmp table: z∏ name,age ( б Salary=4000 (Emp) ) SS#NameAgeSalarydno 1Joe24200002 2Mary20250003 3Bob22270004 4Kathy30300005 5Shideh440001

49 EXAMPLE zEmp table: z∏ name,age (б Salary=4000 (Emp) ) SS#NameAgeSalarydno 1Joe24200002 2Mary20250003 3Bob22270004 4Kathy30300005 5Shideh440001 SS#NameAgeSalarydno 5Shideh440001

50 EXAMPLE zEmp table: z∏ name,age ( б Salary=4000 (Emp) ) SS#NameAgeSalarydno 1Joe24200002 2Mary20250003 3Bob22270004 4Kathy30300005 5Shideh440001 NameAge Shideh4

51 CARTESIAN PRODUCT zCartesian Product (R 1 × R 2 ) combines two relations by concatenating their tuples together, evaluating all possible combinations. If the name of a column is identical for two relations, this ambiguity is resolved by attaching the name of each relation to a column. e.g., Emp × Dept y(SS#, name, age, salary, Emp.dno, Dept.dno, dname, floor, mgrSS#) zIf t(Emp) and t(Dept) is the cardinality of the Employee and Dept relations respectively, then the cardinality of Emp × Dept is: t(Emp) × t(Dept)

52 CARTESIAN PRODUCT (Cont … ) zExample: Emp table: Dept table: 345John Doe2325,0001 943Jane Java2528,0002 876Joe SQL2232,0001 SS#Nameagesalarydno 1Toy1345 2Shoe2943 dnodnamefloormgrSS#

53 CARTESIAN PRODUCT (Cont … ) zCartesian product of Emp and Dept: Emp × Dept: 34 5 John Doe 2325,00 0 11Toy1345 94 3 Jane Java 2528,00 0 21Toy1345 87 6 Joe SQL2232,00 0 11Toy1345 John Doe 2325,00 0 12Shoe2943 Jane Java 2528,00 0 22Shoe2943 87 6 Joe SQL2232,00 0 12Shoe2943 SS#NameagesalaryEmp.dnoDept.dnodnamefloormgrSS#

54 CARTESIAN PRODUCT zExample: retrieve the name of employees that work in the toy department:

55 CARTESIAN PRODUCT zExample: retrieve the name of employees that work in the toy department:  ∏ name (б Emp.dno=Dept.dno (Emp × б dname= ‘ toy ’ (Dept)))

56 CARTESIAN PRODUCT (Cont … )  ∏ name (б dname= ‘ toy ’ (б Emp.dno=Dept.dno (Emp × Dept))) 345John Doe2325,00011Toy1345 943Jane Java2528,00021Toy1345 876Joe SQL2232,00011Toy1345 John Doe2325,00012Shoe2943 Jane Java2528,00022Shoe2943 876Joe SQL2232,00012Shoe2943 SS#NameagesalaryEmp.dnoDept.dnodnamefloormgrSS#

57 CARTESIAN PRODUCT (Cont … )  ∏ name (б dname= ‘ toy ’ (б Emp.dno=Dept.dno (Emp × Dept))) 345John Doe 2325,00011Toy1345 876Joe SQL2232,00011Toy1345 943Jane Java 2528,00022Shoe2943 SS#NameagesalaryEmp.dnoDept.dnodnamefloormgrSS#

58 CARTESIAN PRODUCT (Cont … )  ∏ name (б dname= ‘ toy ’ (б Emp.dno=Dept.dno (Emp × Dept))) 345John Doe 2325,00011Toy1345 876Joe SQL2232,00011Toy1345 SS#NameagesalaryEmp.dnoDept.dnodnamefloormgrSS#

59 CARTESIAN PRODUCT (Cont … )  ∏ name (б dname= ‘ toy ’ (б Emp.dno=Dept.dno (Emp × Dept))) John Doe Joe SQL Name

60 RENAME OPERATOR zRename operator changes the name of its input table to its subscript, yρ e2 (Emp) yChanges the name of Emp table to e2

61 EQUALITY JOIN, NATURAL JOIN, JOIN, SEMI-JOIN zEquality join connects tuples from two relations that match on certain attributes. The specified joining columns are kept in the resulting relation.  ∏ name (б dname= ‘ toy ’ (Emp Dept))) zNatural join connects tuples from two relations that match on the specified common attributes  ∏ name (б dname= ‘ toy ’ (Emp Dept))) zHow is an equality join between Emp and Dept using dno different than a natural join between Emp and Dept using dno?  Equality join: SS#, name, age, salary, Emp.dno, Dept.dno, …  Natural join: SS#, name, age, salary, dno, dname, … zJoin is similar to equality join using different comparison operators yA S op = {=, ≠, ≤, ≥, } att op att (dno)

62 EXAMPLE JOIN zEquality Join, (Emp Dept))) SS#NameAgeSalarydno 1Joe24200002 2Mary20250001 3Bob22270001 4Kathy30300002 5Shideh440001 EMP dnodnamefloormgrss# 1Toy15 2Shoe21 Dept (dno) SS#NameAgeSalaryEMP.dnoDept.dn o dnamefloormgrss # 1Joe242000022Shoe21 2Mary202500011Toy15 3Bob222700011Toy15 4Kathy303000022Shoe21 5Shideh4400011Toy15

63 EXAMPLE JOIN zNatural Join, (Emp Dept))) SS#NameAgeSalarydno 1Joe24200002 2Mary20250001 3Bob22270001 4Kathy30300002 5Shideh440001 EMP dnodnamefloormgrss# 1Toy15 2Shoe21 Dept (dno) SS#NameAgeSalarydnodnamefloormgrss# 1Joe24200002Shoe21 2Mary20250001Toy15 3Bob22270001Toy15 4Kathy30300002Shoe21 5Shideh440001Toy15

64 EXAMPLE JOIN zJoin, (Emp ρ x (Emp)))) SS#NameAgeSalarydno 1Joe24200002 2Mary20250001 3Bob22270001 4Kathy30300002 5Shideh440001 EMP dnodnamefloormgrss# 1Toy15 2Shoe21 Dept Salary > 5 * salary SS#NameAgeSalarydnox.SS#x.Namex.Ag e x.Salar y x.dno 2Mary202500012Shideh440001 3Bob222700013Shideh440001 4Kathy303000024Shideh440001

65 EQUALITY JOIN, NATURAL JOIN, JOIN, SEMI-JOIN (Cont … ) zExample: retrieve the name of employees who earn more than Joe:  ∏ name (Emp (sal>x.sal) б name= ‘ Joe ’ (ρ x (Emp))) zSemi-Join selects the columns of one relation that joins with another. It is equivalent to a join followed by a projection: yEmp (dno) Dept ≡ ∏ SS#, name, age, salary, dno (Emp Dept)


Download ppt "RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science."

Similar presentations


Ads by Google