Presentation is loading. Please wait.

Presentation is loading. Please wait.

COP4710 Database Systems Relational Algebra.

Similar presentations


Presentation on theme: "COP4710 Database Systems Relational Algebra."— Presentation transcript:

1 COP4710 Database Systems Relational Algebra

2 Why Do We Learn This? Querying the database: specify what we want from our database Find all the people who earn more than $1,000,000 and pay taxes in Tallahassee Could write in C++/Java, but a bad idea Instead use high-level query languages: Theoretical: Relational Algebra, Datalog Practical: SQL Relational algebra: a basic set of operations on relations that provide the basic principles

3 What is an “Algebra”? Mathematical system consisting of: Examples
Operands --- variables or values from which new values can be constructed Operators --- symbols denoting procedures that construct new values from given values Examples Arithmetic(Elementary) algebra, linear algebra, Boolean algebra …… What are operands? What are operators?

4 What is Relational Algebra?
An algebra Whose operands are relations or variables that represent relations Whose operators are designed to do common things that we need to do with relations in a database relations as input, new relation as output Can be used as a query language for relations!

5 Relational Operators at a Glance
Five basic RA operations: Basic Set Operations union, difference (no intersection, no complement) Selection: s Projection: p Cartesian Product: X When our relations have attribute names: Renaming: r Derived operations: Intersection, complement Joins (natural join, equi-join, theta join, semi-join, ……)

6 Set Operations Union: all tuples in R1 or R2, denoted as R1 U R2
R1, R2 must have the same schema R1 U R2 has the same schema as R1, R2 Example: Active-Employees U Retired-Employees If any, is duplicate elimination required? Difference: all tuples in R1 but not in R2, denoted as R1 – R2 R1 - R2 has the same schema as R1, R2 Example All-Employees - Retired-Employees

7 Selection Returns all tuples (rows) which satisfy a condition, denoted as sc(R) c is a condition: =, <, >, AND, OR, NOT Output schema: same as input schema Find all employees with salary more than $40,000: sSalary > (Employee) SSN Name Dept-ID Salary Alex 1 30K Bob 32K Chris 2 45K SSN Name Dept-ID Salary Chris 2 45K

8 Projection Unary operation: returns certain columns, denoted as P A1,…,An (R) Eliminates duplicate tuples ! Input schema R(B1, …, Bm) Condition: {A1, …, An} {B1, …, Bm} Output schema S(A1, …, An) Example: project social-security number and names: P SSN, Name (Employee) SSN Name Dept-ID Salary Alex 1 30K Bob 32K Chris 2 45K SSN Name Alex Bob Chris

9 Selection vs. Projection
Think of relation as a table How are they similar? How are they different? Horizontal vs. vertical? Duplicate elimination for both? What about in real systems? Why do you need both?

10 Cartesian Product Each tuple in R1 with each tuple in R2, denoted as R1 x R2 Input schemas R1(A1,…,An), R2(B1,…,Bm) Output schema is S(A1, …, An, B1, …, Bm) Two relations are combined! Very rare in practice; but joins are very common Example: Employee x Dependent

11 Example SSN Name 111060000 Alex 754320032 Brandy Employee-SSN
Dependent SSN Name Alex Brandy Employee-SSN Dependent-Name Chris David Employee x Dependent SSN Name Employee-SSN Dependent-Name Alex Chris David Brandy

12 Renaming Soc-sec-num, firstname(Employee)
Does not change the relational instance, denoted as Notation: r S(B1,…,Bn) (R) Changes the relational schema only Input schema: R(A1, …, An) Output schema: S(B1, …, Bn) Example: Soc-sec-num, firstname(Employee) SSN Name Alex Bob Chris Soc-sec-num firstname Alex Bob Chris

13 Set Operations: Intersection
Intersection: all tuples both in R1 and in R2, denoted as R1 R2 R1, R2 must have the same schema R1 R2 has the same schema as R1, R2 Example UnionizedEmployees RetiredEmployees Intersection is derived: R R2 = R1 – (R1 – R2) why ?

14 Theta Join A join that involves a predicate q, denoted as R1 q R2
Input schemas: R1(A1,…,An), R2(B1,…,Bm) Output schema: S(A1,…,An,B1,…,Bm) Derived operator: R1 q R2 = s q (R1 x R2) Take the (Cartisian) product R1 x R2 Then apply SELECTC to the result As for SELECT, C can be any Boolean-valued condition

15 Theta Join: Example Name Address Bar Beer Price AJ’s Bud 2.5 Miller
Sells Name Address AJ's 1800 Tennessee Michael's Pub 513 Gaines Bar Beer Price AJ’s Bud 2.5 Miller 2.75 Michael’s Pub Corona 3.0 BarInfo := Sells Sells.Bar=Bar.Name Bar Bar Beer Price Name Address AJ’s Bud 2.5 AJ's 1800 Tennessee Miller 2.75 Michael’s Pub Michael's Pub 513 Gaines Corona 3.0

16 Natural Join Notation: R1 R2
Input Schema: R1(A1, …, An), R2(B1, …, Bm) Output Schema: S(C1,…,Cp) Where {C1, …, Cp} = {A1, …, An} U{B1, …, Bm} Meaning: combine all pairs of tuples in R1 and R2 that agree on the join attributes: {A1,…,An} {B1,…, Bm} (called the join attributes)

17 Natural Join: Examples
Employee Dependent SSN Name Alex Brandy SSN Dependent-Name Chris David Employee Dependent = P SSN, Name, Dependent-Name(sEmployee.SSN=Dependent.SSN(Employee x Dependent) SSN Name Dependent-Name Alex Chris Brandy David

18 Natural Join: Examples
B X Y Z V B C Z U V W R S A B C X Z U V Y W

19 Equi-join Special case of theta join: condition c contains only conjunctions of equalities Result schema is the same as that of Cartesian product May have fewer tuples than Cartesian product Most frequently used in practice: R1 A=B R2 Natural join is a particular case of equi-join A lot of research on how to do it efficiently

20 A Joke About Join A join query walks up to two tables in a restaurant and asks : “Mind if I join you?”

21 Building Complex Expressions
Algebras allow us to express sequences of operations in a natural way Example In arithmetic algebra: (x + 4)*(y - 3) Relational algebra allows the same Three notations, just as in arithmetic: Sequences of assignment statements Expressions with several operators Expression trees

22 Sequences of Assignments
Create temporary relation names Renaming can be implied by giving relations a list of attributes Example: R3 := R C R2 can be written: R4 := R1 x R2 (R4: temporary relation) R3 := sC (R4)

23 Expressions with Several Operators
Example: the theta-join R3 := R1 JOINC R2 can be written: R3 := sC (R1 x R2) Precedence of relational operators: Unary operators --- select, project, rename --- have highest precedence, bind first Then come products and joins Then intersection Finally, union and set difference bind last But you can always insert parentheses to force the order you desire

24 Expression Trees Leaves are operands
either variables standing for relations or particular constant relations Interior nodes are operators, applied to their child or children

25 Expression Tree: Examples
Given Bars(name, addr), Sells(bar, beer, price), find the names of all the bars that are either on Tennessee St. or sell Bud for less than $3 UNION RENAMER(name) PROJECTname PROJECTbar SELECTaddr = “Tennessee St.” SELECT price<3 AND beer=“Bud” Bars Sells

26 Summary of Relational Algebra
Why bother ? Can write any RA expression directly in C++/Java, seems easy Two reasons: Each operator admits sophisticated implementations (think of and s C) Expressions in relational algebra can be rewritten: optimized s(age >= 30 AND age <= 35)(Employees) Method 1: scan the file, test each employee Method 2: use an index on age Employees Relatives Iterate over Employees, then over Relatives? Or iterate over Relatives, then over Employees? Sort Employees, Relatives, do “merge-join” “hash-join” etc.


Download ppt "COP4710 Database Systems Relational Algebra."

Similar presentations


Ads by Google