# Learning Database Systems: Relational Algebra and Querying in Tallahassee, Florida

This article discusses the importance of learning database systems and specifically explores the use of Relational Algebra and querying in the context of Tallahassee, Florida. The article also highlights the practical application of SQL and the basic principles of using basic operations on relations.

## About Learning Database Systems: Relational Algebra and Querying in Tallahassee, Florida

PowerPoint presentation about 'Learning Database Systems: Relational Algebra and Querying in Tallahassee, Florida'. This presentation describes the topic on This article discusses the importance of learning database systems and specifically explores the use of Relational Algebra and querying in the context of Tallahassee, Florida. The article also highlights the practical application of SQL and the basic principles of using basic operations on relations.. The key topics included in this slideshow are Tallahassee, Florida, COP4710 Database Systems, Relational Algebra, Querying the database, SQL,. Download this presentation absolutely free.

## Presentation Transcript

1. Tallahassee, Florida, 2014 Tallahassee, Florida, 2014 COP4710 Database Systems COP4710 Database Systems Relational Algebra Fall 2014 Fall 2014

2. Why Do We Learn This? 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 1 1

3. What is an Algebra? What is an Algebra? Mathematical system consisting of: 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? 2 2

4. What is Relational Algebra? 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 3 3

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

6. Set Operations 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 and not in R2, denoted as R1 R2 R1, R2 must have the same schema R1 - R2 has the same schema as R1, R2 Example All-Employees - Retired-Employees 5 5

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

8. Projection Projection Unary operation: returns certain columns, denoted as 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: SSN, Name (Employee) 7 7 SSN Name Dept-ID Salary 111060000 Alex 1 30K 754320032 Bob 1 32K 983210129 Chris 2 45K SSN Name 111060000 Alex 754320032 Bob 983210129 Chris

9. Selection vs. Projection 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? 8 8

10. Cartesian Product 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 9 9

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

12. Renaming Renaming Does not change the relational instance, denoted as Notation: 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) 11 11 SSN Name 111060000 Alex 754320032 Bob 983210129 Chris Soc-sec-num firstname 111060000 Alex 754320032 Bob 983210129 Chris

13. Set Operations: Intersection 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 : R1 R2 = R1 (R1 R2) why ? 12 12

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

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

16. Natural Join 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 attributes: {A1,,An} {B1,, Bm} (called the join attributes ) 15 15

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

18. Natural Join: Examples Natural Join: Examples 17 17 R S R S

19. Equi-join Equi-join Special case of theta join: condition c contains only conjunction 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 R2 Natural join is a particular case of equi-join A lot of research on how to do it efficiently 18 18

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

21. Division Division A/B for A(x,y) and B(y) Contains all tuples (x) such that for every y tuple in B , there is an xy tuple in A Useful for expressing for all queries For A/B , compute all x values that are not disqualified by some y value in B x value is disqualified if by attaching y value from B , we obtain an xy tuple that is not in A 20 20 1. Disqualified x values: 2. A/B: Disqualified x values

22. Division: Example Division: Example 21 21 A B1 B2 B3 A/B1 A/B2 A/B3

23. Building Complex Expressions 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: 1. Sequences of assignment statements 2. Expressions with several operators 3. Expression trees 22 22

24. Sequences of Assignments Sequences of Assignments Create temporary relation names Renaming can be implied by giving relations a list of attributes Example: R3 := R1 JOIN C R2 can be written: R4 := R1 x R2 R3 := SELECT C (R4) 23 23

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

26. Expression Trees 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 25

27. Expression Tree: Examples 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 26 26 Bars Sells SELECT addr = Tennessee St. SELECT price<3 AND beer=Bud PROJECT name RENAME R(name) PROJECT bar UNION

28. Summary of Relational Algebra 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 C ) Expressions in relational algebra can be rewritten: optimized (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. 27 27