Presentation is loading. Please wait.

Presentation is loading. Please wait.

RELATIONAL ALGEBRA (Chapter 2)

Similar presentations


Presentation on theme: "RELATIONAL ALGEBRA (Chapter 2)"— Presentation transcript:

1 RELATIONAL ALGEBRA (Chapter 2)
2018/11/13

2 Overall Organization Query Parser Query Optimizer Query Interpretor
Relational Algebra operators: , , , , , , , ,  Index structures Abstraction of records Buffer Pool Manager File System 2018/11/13

3 RELATIONAL ALGEBRA INTRODUCTION
Assume 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. Relational algebra consists of a set of operators that consume either one or two relations as input. An operator produces one relation as its output. Unary operators include: select, project, and rename Binary operators include: cartesian product, equality join, natural join, join, semi-join, division, union, and set difference. 2018/11/13

4 < < SELECT OPERATOR бSalary>30,000 age<25(Employee) <
Select (б): selects tuples that satisfy a predicate; e.g., retrieve the employees whose salary is 30,000 бSalary=30,000(Employee) Conjunctive ( ) 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, age<25(Employee) Note 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). Note: This operator is different than the SELECT command of SQL. < < < 2018/11/13

5 EXAMPLE Emp table: SS# Name Age Salary dno 1 Joe 24 20000 2 Mary 20
25000 3 Bob 22 27000 4 Kathy 30 30000 5 Shideh 4000 Emp table: 2018/11/13

6 EXAMPLE бSalary=30,000(Employee) Emp table: SS# Name Age Salary dno 1
Joe 24 20000 2 Mary 20 25000 3 Bob 22 27000 4 Kathy 30 30000 5 Shideh 4000 Emp table: бSalary=30,000(Employee) 2018/11/13

7 EXAMPLE бSalary=30,000(Employee) Emp table: SS# Name Age Salary dno 1
Joe 24 20000 2 Mary 20 25000 3 Bob 22 27000 4 Kathy 30 30000 5 Shideh 4000 Emp table: бSalary=30,000(Employee) SS# Name Age Salary dno 4 Kathy 30 30000 5 2018/11/13

8 EXAMPLE бAge>22(Employee) Emp table: SS# Name Age Salary dno 1 Joe
24 20000 2 Mary 20 25000 3 Bob 22 27000 4 Kathy 30 30000 5 Shideh 4000 Emp table: бAge>22(Employee) 2018/11/13

9 EXAMPLE бAge>22(Employee) Emp table: SS# Name Age Salary dno 1 Joe
24 20000 2 Mary 20 25000 3 Bob 22 27000 4 Kathy 30 30000 5 Shideh 4000 Emp table: бAge>22(Employee) SS# Name Age Salary dno 1 Joe 24 20000 2 4 Kathy 30 30000 5 2018/11/13

10 PROJECT OPERATOR Project (∏) 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)) 2018/11/13

11 EXAMPLE Emp table: SS# Name Age Salary dno 1 Joe 24 20000 2 Mary 20
25000 3 Bob 22 27000 4 Kathy 30 30000 5 Shideh 4000 Emp table: 2018/11/13

12 EXAMPLE Emp table: ∏ age(Emp) SS# Name Age Salary dno 1 Joe 24 20000 2
Mary 20 25000 3 Bob 22 27000 4 Kathy 30 30000 5 Shideh 4000 Emp table: ∏ age(Emp) Age 24 20 22 30 4 2018/11/13

13 EXAMPLE Emp table: ∏ name,age(бSalary=4000 (Emp) ) SS# Name Age Salary
dno 1 Joe 24 20000 2 Mary 20 25000 3 Bob 22 27000 4 Kathy 30 30000 5 Shideh 4000 Emp table: ∏ name,age(бSalary=4000 (Emp) ) 2018/11/13

14 EXAMPLE Emp table: ∏ name,age(бSalary=4000 (Emp) ) SS# Name Age Salary
dno 1 Joe 24 20000 2 Mary 20 25000 3 Bob 22 27000 4 Kathy 30 30000 5 Shideh 4000 Emp table: ∏ name,age(бSalary=4000 (Emp) ) SS# Name Age Salary dno 5 Shideh 4 4000 1 2018/11/13

15 EXAMPLE Emp table: ∏ name,age(бSalary=4000 (Emp) ) SS# Name Age Salary
dno 1 Joe 24 20000 2 Mary 20 25000 3 Bob 22 27000 4 Kathy 30 30000 5 Shideh 4000 Emp table: ∏ name,age(бSalary=4000 (Emp) ) Name Age Shideh 4 2018/11/13

16 CARTESIAN PRODUCT Cartesian Product (R1 × R2) 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 (SS#, name, age, salary, Emp.dno, Dept.dno, dname, floor, mgrSS#) If 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) 2018/11/13

17 CARTESIAN PRODUCT (Cont…)
Example: Emp table: Dept table: SS# Name age salary dno 345 John Doe 23 25,000 1 943 Jane Java 25 28,000 2 876 Joe SQL 22 32,000 dno dname floor mgrSS# 1 Toy 345 2 Shoe 943 2018/11/13

18 CARTESIAN PRODUCT (Cont…)
Cartesian product of Emp and Dept: Emp × Dept: SS# Name age salary Emp.dno Dept.dno dname floor mgrSS# 345 John Doe 23 25,000 1 Toy 943 Jane Java 25 28,000 2 876 Joe SQL 22 32,000 Shoe 2018/11/13

19 CARTESIAN PRODUCT Example: retrieve the name of employees that work in the toy department: 2018/11/13

20 CARTESIAN PRODUCT Example: retrieve the name of employees that work in the toy department: ∏name(бEmp.dno=Dept.dno(Emp × бdname=‘toy’(Dept))) 2018/11/13

21 CARTESIAN PRODUCT (Cont…)
∏name(бdname=‘toy’ (б Emp.dno=Dept.dno(Emp × Dept))) SS# Name age salary Emp.dno Dept.dno dname floor mgrSS# 345 John Doe 23 25,000 1 Toy 943 Jane Java 25 28,000 2 876 Joe SQL 22 32,000 Shoe 2018/11/13

22 CARTESIAN PRODUCT (Cont…)
∏name(бdname=‘toy’ (б Emp.dno=Dept.dno(Emp × Dept))) SS# Name age salary Emp.dno Dept.dno dname floor mgrSS# 345 John Doe 23 25,000 1 Toy 876 Joe SQL 22 32,000 943 Jane Java 25 28,000 2 Shoe 2018/11/13

23 CARTESIAN PRODUCT (Cont…)
∏name(бdname=‘toy’ (б Emp.dno=Dept.dno(Emp × Dept))) SS# Name age salary Emp.dno Dept.dno dname floor mgrSS# 345 John Doe 23 25,000 1 Toy 876 Joe SQL 22 32,000 2018/11/13

24 CARTESIAN PRODUCT (Cont…)
∏name(бdname=‘toy’ (б Emp.dno=Dept.dno(Emp × Dept))) Name John Doe Joe SQL 2018/11/13

25 RENAME OPERATOR Rename operator changes the name of its input table to its subscript, ρe2(Emp) Changes the name of Emp table to e2 2018/11/13

26 EQUALITY JOIN, NATURAL JOIN, JOIN, SEMI-JOIN
Equality 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))) Natural join connects tuples from two relations that match on the specified common attributes How 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, … Join is similar to equality join using different comparison operators A S op = {=, ≠, ≤, ≥, <, >} att op att (dno) (dno) 2018/11/13

27 EXAMPLE JOIN Equality Join, (Emp Dept))) EMP Dept SS# Name Age Salary
dno 1 Joe 24 20000 2 Mary 20 25000 3 Bob 22 27000 4 Kathy 30 30000 5 Shideh 4000 Equality Join, (Emp Dept))) dno dname floor mgrss# 1 Toy 5 2 Shoe EMP Dept (dno) SS# Name Age Salary EMP.dno Dept.dno dname floor mgrss# 1 Joe 24 20000 2 Shoe Mary 20 25000 Toy 5 3 Bob 22 27000 4 Kathy 30 30000 Shideh 4000 2018/11/13

28 EXAMPLE JOIN Natural Join, (Emp Dept))) EMP Dept SS# Name Age Salary
dno 1 Joe 24 20000 2 Mary 20 25000 3 Bob 22 27000 4 Kathy 30 30000 5 Shideh 4000 Natural Join, (Emp Dept))) dno dname floor mgrss# 1 Toy 5 2 Shoe EMP Dept (dno) SS# Name Age Salary dno dname floor mgrss# 1 Joe 24 20000 2 Shoe Mary 20 25000 Toy 5 3 Bob 22 27000 4 Kathy 30 30000 Shideh 4000 2018/11/13

29 EXAMPLE JOIN Join, (Emp ρx(Emp)))) EMP Dept SS# Name Age Salary dno 1
Joe 24 20000 2 Mary 20 25000 3 Bob 22 27000 4 Kathy 30 30000 5 Shideh 4000 Join, (Emp ρx(Emp)))) dno dname floor mgrss# 1 Toy 5 2 Shoe EMP Dept Salary > 5 * salary SS# Name Age Salary dno x.SS# x.Name x.Age x.Salary x.dno 2 Mary 20 25000 1 Shideh 4 4000 3 Bob 22 27000 Kathy 30 30000 2018/11/13

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

31 UNION, SET DIFFERENCE & SET INTERSECT
Union puts all tuples of two relations in one relation. To use this operator, two conditions must hold: The two relations must be of the same arity. The domain of ith attribute of the two participating relation must be the same. Set difference operator computes tuples that are in one relation, but not in another. Set intersect operator computes tuples that are common in two relations: The five fundamental operations of the relational algebra are: select, project, cartesian product, Union, and set difference All other operators can be constructed using these operators 2018/11/13

32 EXAMPLE Assume a database with the following three relations:
Sailors (sid, sname, rating) Boats (bid, bname, color) Reserve (sid, bid, date) Query 1: Find the bid of red colored boats: 2018/11/13

33 EXAMPLE Assume a database with the following three relations:
Sailors (sid, sname, rating) Boats (bid, bname, color) Reserve (sid, bid, date) Query 1: Find the bid of red colored boats: ∏bid(бcolor=red(Boats)) 2018/11/13

34 EXAMPLE Assume a database with the following three relations:
Sailors (sid, sname, rating) Boats (bid, bname, color) Reserve (sid, bid, date) Query 1: Find the name of sailors who have reserved Boat number 2. 2018/11/13

35 EXAMPLE Assume a database with the following three relations:
Sailors (sid, sname, rating) Boats (bid, bname, color) Reserve (sid, bid, date) Query 1: Find the name of sailors who have reserved Boat number 2. ∏sname(бbid=2(Sailors (sid)Reserve)) 2018/11/13

36 EXAMPLE Assume a database with the following three relations:
Sailors (sid, sname, rating) Boats (bid, bname, color) Reserve (sid, bid, date) Query 1: Find the name of sailors who have reserved both a red and a green boat. 2018/11/13

37 EXAMPLE Assume a database with the following three relations:
Sailors (sid, sname, rating) Boats (bid, bname, color) Reserve (sid, bid, date) Query 1: Find the name of sailors who have reserved both a red and a green boat. ∏sid( бcolor=red(Boats) (Sailors Reserve) ) ∏sid( бcolor=green(Boats) (Sailors Reserve) ) 2018/11/13

38 EXAMPLE Assume a database with the following three relations:
Emp(SS#, name, salary, age, mgrss#) Query 3: Find employees whose salary is higher than their manager’s salary. 2018/11/13

39 EXAMPLE Assume a database with the following relations:
Emp(SS#, name, salary, age, mgrss#) Query 3: Find name of those employees whose salary is higher than their manager’s salary. ∏Emp.name( бemp.salary>mgr.salary(Emp ρmgr(Emp)) ) mgrSS#=SS# 2018/11/13

40 EXAMPLE SS# Name Age Salary mgrSS# 1 Joe 30 30000 Null 2 Bob 23 25000
Shideh 4 40000 Kathy 15 32000 ∏Emp.name( бemp.salary>mgr.salary(Emp ρmgr(Emp)) ) mgrSS#=SS# SS# Name Age Salary mgrSS# Mgr.SS# Mgr.Name Mgr.Age Mgr.Salary Mgr.mgrSS# 2 Bob 23 25000 1 Joe 30 30000 Null 3 Shideh 4 40000 Kathy 15 32000 2018/11/13

41 EXAMPLE SS# Name Age Salary mgrSS# 1 Joe 30 30000 Null 2 Bob 23 25000
Shideh 4 40000 Kathy 15 32000 ∏Emp.name( бemp.salary>mgr.salary(Emp ρmgr(Emp)) ) mgrSS#=SS# SS# Name Age Salary mgrSS# Mgr.SS# Mgr.Name Mgr.Age Mgr.Salary Mgr.mgrSS# 3 Shideh 4 40000 1 Joe 30 30000 Null Kathy 15 32000 2018/11/13

42 EXAMPLE SS# Name Age Salary mgrSS# 1 Joe 30 30000 Null 2 Bob 23 25000
Shideh 4 40000 Kathy 15 32000 ∏Emp.name( бemp.salary>mgr.salary(Emp ρmgr(Emp)) ) mgrSS#=SS# Name Shideh Kathy 2018/11/13

43 OTHER ALGEBRAIC OPERATORS
Assignment: One may use the assignment operator to assign the relation produced by an algebraic expression to a new relation, e.g., ToyEmp  Emp бname=‘toy’ (Dept) Deletion is performed using the assignment operator: R  R – E Example: Fire all employees whose salary is higher than 100,000 Emp  Emp – бsalary>100,000 (Emp) Insertion is also performed using the assignment operator: R  R  E Example: Hire Joe Emp  Emp  (5, Joe, 15, 30000, 1) 2018/11/13

44 OTHER ALGEBRAIC OPERATORS
Update operator changes the value of records based on its input expression AE(R) Example, Give all employees a 10% raise: salary1.1*salary(Emp) 2018/11/13

45 A PROGRAM Recall Give the toy employees a 10% raise:
Emp(SS#,name,age,salary, dno) Dept(dno,dname,floor,mgrss#) Give the toy employees a 10% raise: 2018/11/13


Download ppt "RELATIONAL ALGEBRA (Chapter 2)"

Similar presentations


Ads by Google