Download presentation
Presentation is loading. Please wait.
Published byBuck Taylor Modified over 9 years ago
1
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus
2
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Relational Algebra What is relational algebra A formal language with operations defined on relations A sequence of operations forms an expression A relational algebra expression generates a new relation f(R1, R2, …, Rn) R’ What’s the difference between SQL and relational algebra SQL specifies what you want, but not how to get it Relational algebra defines the order of operations Why is it important Used for query processing and optimization
3
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Relational Algebra Relational algebra and SQL Relational algebra was designed before SQL Most DBMS still use it in internal module Classification of operations Set-based operations: Union, Intersection, Difference, Cartesian product Relational model based: Project, Select (restrict), Join Additional : Aggregate functions, Outer join, etc. Another classification Input is 1 relation: Project, Select Input is 2 or more relation: Union, Intersection, Difference, Cartesian product, Join
4
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6.1. Unary Operation: SELECT Select Symbol: σ Syntax: σ (conditions) (input relation) Given ONE input relation, return ROWS that satisfy the conditions Conditions: a number of logical expressions (and / or/ not) Comparison:, <>, >=, <=, etc Not the SELECT in SQL !! (also called restrict)
5
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Q1: what is the result Q2: what is the equivalent SQL? SELECT * FROM EMPLOYEE WHERE (Dno=4 AND Salary>25000) OR (Dno=5 AND Salary>30000)
6
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6.1. Unary Operation: SELECT Some further questions: Multiple levels of “select” σ (Sex=‘M’) (σ (dno=4 and salary>30000) (EMPLOYEE)) Q: Will the orders of select affect the result? Commutative property
7
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6.1. Unary Operation: Project Project Symbol: π Syntax: π (attribute names) (input relation) Given ONE input relation, returns COLUMNS listed Columns : comma separated list This is the SELECT in SQL !!
8
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Q3: what is the result Q4: what is the equivalent SQL? SELECT Lname, Fname, Salary FROM EMPLOYEE ?
9
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
10
6.1.2 Unary Operation: Project Some further questions: Orders of the attributes in the new relation Duplicate elimination Elements in a set should be unique Any duplicate rows in the result of a Project will be removed SQL equivalent to project: SELECT DISTINCT Sex, Salary FROM EMPLOYEE SQL allows duplicated rows while relational algebra doesn’t! The list of attributes (column names) should appear in input relation
11
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6.1.3 Orders of operations/rename Two ways to write expressions with multiple operators Save the output of an expression as a new relation Write expressions nested operations TEMP σ (dno=4 and salary>30000) (EMPLOYEE) R(Last_name, Salary) π (L_name, salary) TEMP Rename operator (ρ) ρ S(B1, B2, …, Bn) (R) : S is the new name of R, B1~Bn are new column names of R.
12
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Q5: What is the corresponding SQL?
13
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus Summary
14
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6.2 Set Theory based operations Union (U) Intersection (∩) Set difference (-) Input: TWO relations Output: ONE relation
15
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Given two relations, Union: AUB return a new relation with all the rows in either relation Intersection: A∩B return a new relations with all the rows in both relations Minus: A-B returns a new relation with all the rows in A but not in B 6.2 Set Theory based operations A UNION B A INTERSECT B A MINUS B
16
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
17
Further question: Can we UNION any two relations? Union Compatibility Same number of attributes Same data type for each pair of attributes Duplicate elimination SQL: UNION, INTERSECT, EXCEPT 6.2 Set Theory based operations
18
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Generate all combinations of rows from two relations Symbol: X Relation A and B A has m attributes, B has n attributes A X B has m+n attributes (columns) |A|*|B| rows FEMALE_EMPS X DEPENDENT SQL: SELECT * FROM A, B 6.3 Cartesian Product (Cross product)
19
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Continued next page…
20
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
22
Cardinality of the result relation What is the min/max number of rows and columns of the result σ (Condition1 and Condition 2) (R) π (column1,column2, … column N) (R) R U S R ∩ S R – S R X S Some questions to discuss
23
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Aggregate functions: Compute statistics (e.g,. SUM, COUNT, MIN, MAX) on collections of values Not defined in original relational algebra. Group by Aggregate some attributes (e.g., salary) of rows in the same GROUP (e.g., department) F (R) How many people are there in EACH Department? F (EMPLOYEE) Aggregate functions and group by
24
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
25
Write relational algebra expressions for the following questions Text book 6.16 Exercises
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.