Presentation is loading. Please wait.

Presentation is loading. Please wait.

Relational Algebra Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4.

Similar presentations


Presentation on theme: "Relational Algebra Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4."— Presentation transcript:

1 Relational Algebra Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4

2 Relational Algebra Operations
Each operation is either unary (requires 1 relation as input) or binary (requires 2 relations as input. Each operation produces a new relation as output. This allows nested operations.

3 Categories of RA Operations
Set operators – union, intersection, difference, cross-product (Cartesian product) Special operators – selection, projection, join, division

4 Union R ∪ S produces relation that contains those tuples that appear in R or S (or both). Union compatibility – relations must have same number of fields and corresponding fields (left to right) must have same domains; field names are not relevant

5 Example Student (ID, Name, Address) Instructor (ID, Name, Address)
List the ID number, name, and address of everyone who is either an instructor or a student. Student ∪ Instructor

6 Intersection R ∩ S produces relation that contains those tuples that appear in R and S. The input relations must be union compatible.

7 Example Student (ID, Name, Address) Instructor (ID, Name, Address)
List the ID number, name, and address of everyone who is both an instructor and a student. Student ∩ Instructor

8 Set Difference R - S produces relation that contains those tuples that appear in R but not in S. The input relations must be union compatible.

9 Example Student (ID, Name, Address) Instructor (ID, Name, Address)
List the ID number, name, and address of all instructors who are not students. Instructor - Student

10 Cross-Product R  S produces relation that contains tuples with the combined relations of R and S. R  S contains one tuple <r, s> for each pair of tuples r  R, s  S where <r, s> is the concatenation of tuples r and s.

11 Example Student (ID, Name, Address) Course (CourseNum, CourseName)
List all information about each student and all the courses that the student may take. Student  Course

12 Renaming Operator Assume resulting relations inherit the names of the input relations. Sometimes there are naming conflicts. E.g.: R (a, b, c) S (a, d, e, f) What are the attributes of R  S?

13 Renaming Operator ρ (rho) – renaming operator ρ (R (F̅ ), E)
R – new relation resulting from RA expression E F̅ – renaming list of form: oldname → newname or position → newname

14 Example Sailors (SID, SName, Rating, Age) <22, Dustin, 7, 45.0>
<31, Lubber, 8, 55.5> <58, Rusty, 10, 35.0> Reserves (SID, BID, Date) <22, 101, 10/10/96> <58, 103, 11/12/96> What are the tuples in Sailors  Reserves? Based on Figures 4.1, 4.3, and 4.11

15 Example (continued) ↓ ↓ (SID) SName Rating Age BID Day 22 Dustin 7
45.0 101 10/10/96 58 103 11/12/96 31 Lubber 8 55.5 Rusty 10 35.0

16 Example (continued) ρ (C (1 → SID1, 5 → SID2), Sailors  Reserves)
Produces relation with schema: C(sid1: integer, sname: string, rating: integer, age: real, sid2: integer, bid: integer, day: dates) Most of the time we will not include the data types.

17 Nested Operations For each student who is not an instructor, list all the information about that student and all the information about the courses that are available. (Student – Instructor)  Course

18 Selection Given relation R and some condition (called select condition or criterion), the select operator produces a relation that contains those tuples of R that satisfy the condition.

19 Example Given: Employee (SSN, EmpName, Salary, DeptCode)
List the information about those employees who work for department #16. DeptCode = 16 (Employee)

20 Select Condition May be Boolean combination of terms (i.e., expression using logical connectives  and  where terms have form: attribute op constant or attribute1 op attribute2 where op is <, ≤, >, ≥, =, or ≠.

21 Projection Projection produces relation that contains only those attributes from input relation that are specified.

22 Examples Given: Employee (SSN, EmpName, Salary, DeptCode)
List the names and salaries of employees. EmpName, Salary (Employee) List names and salaries of employees who work for department #16 and make at least $50,000. EmpName, Salary (DeptCode = 16  Salary ≥ (Employee))

23 Join Join produces a new relation by combining related tuples from two relations into single tuples. Most general version has a join condition that is identical to select condition in form. Note that: R || c S = c (R  S) where c is the join condition.

24 Theta Join In general, join condition may be any type of logical condition (<, >, =, etc.). Join with a general join condition is called a theta join. Equality comparisons are much more common than those involving inequalities.

25 Equijoin Equijoin is a join in which join condition consists solely of equality (or equalities connected by ). Duplicate attributes in join condition are dropped from resulting relation.

26 Examples Given: Student (ID, Name, Major, FacID)
Faculty (FacID, FacName, Phone) For each student, list the ID number, name, major, advisor’s ID number, and advisor’s name. Student ||Student.FacID = Faculty.FacID Faculty List the name of Jane Smith’s advisor. FacName (Faculty ||Faculty.FacID = Student.FacID (Name=‘Jane Smith’ (Student)))

27 Natural Join Natural join is an equijoin in which all attributes with same name are included in join condition. For example, given: R1 (a, b, c, d) R2 (b, d, e) Natural join: R1 || R2 is equivalent to equijoin: R1 || R1.b = R2.b  R1.d = R2.d R2

28 Example Given: Student (ID, Name, Major, FacID)
Faculty (FacID, FacName, Phone) For each student, list the ID number, name, major, advisor’s ID number, and advisor’s name. Student ||Student.FacID = Faculty.FacID Faculty

29 Division The division of a binary relation R by a unary relation S is a unary relation whose tuples consist of all values of one attribute of R such that the other attribute of R matches all values of S. Useful for queries such as: List those CSE instructors who have taught every course offered by the department. List those suppliers who supply all parts for a given component. List those sailors who have reserved all boats.

30 Example Given: Graduates (ID, Degree) DegreesOffered (Degree)
List the ID numbers of those graduates who have received every type of degree offered by this university. Graduates / Diploma

31 Generalization of Division
Dividing relation of degree x+y by relation of degree y (where the two relations have those y attributes in common) yields a relation of degree x. Think of first x attributes of first relation as composite attribute called attrx. Think of y attributes of second relation as composite attribute called attry. Given R1 (x, y) and R2 (y), R1/R2 is unary relation with attribute x. Will contain all the x values such that, for every y value in R2, there is a tuple <x, y> in R1.

32 Going Beyond RA Commercial relational query languages provide operations beyond those provided by RA. Examples: update operations, aggregate functions, recursive closure operations, outer joins, outer unions

33 Insertions and Constraint Violations
Key constraint – attempt to insert new tuple with same PK value as existing tuple Entity integrity constraint – attempt to insert new tuple with null PK Referential integrity constraint – attempt to insert new tuple with FK value that does not exist in relation to which it refers

34 Deletions and Constraint Violations
Referential integrity constraint – attempt to delete tuple with PK value that appears as FK value in another relation

35 Modifications and Constraint Violations
Key constraint – attempt to modify tuple so that PK value will be same as PK value of another tuple Entity integrity constraint – attempt to modify tuple so that PK value will be null Referential integrity constraint – attempt to modify tuple’s FK value so that it no longer equals PK value in relation it references; attempt to modify tuple’s PK value so that FK values are no longer valid

36 Aggregate Functions Apply mathematical function to collections of values (specified in “group by” clause) from database. Examples: SUM, AVERAGE, MAXIMUM, MINIMUM, COUNT Queries: List those employees who have more than 2 dependents. List the average salary of employees in each department. Section 5.5 of Chapter 5

37 Recursive Closure Recursive closure operation is applied to recursive relationship between tuples of the same type. Example: employees supervising other employees Employee (ID, Name, JobTitle, SupervisorID) Queries: Given a particular employee, retrieve all his supervisees, all their supervisees, all their supervisees, etc., from level to level. List all the components of a particular piece of equipment, where each component can be broken down into subcomponents.

38 Null Values Null values may mean: unkown
inapplicable Example: number of dependents of new employee Query: Retrieve those employees with more than 2 dependents. Should condition be true or false for tuple with null value? Neither! Should be unknown. Requires 3-valued logic. Section 5.6 of Chapter 5

39 Outer Join Outer join is a join in which tuples in one relation with no match (according to join condition) in other relation are included padded with null values as needed. Left outer join – all tuples in 1st (or left) relation are included even if no match in 2nd relation Right outer join – all tuples in 2nd (or right) relation are included even if no match in 1st relation Full outer join – all tuples in both relations are included

40 Example – Outer Join Given: Employee (ID, Name, DeptID)
Department (DeptID, DeptName, MgrID) Query: For those employees who manage a department, list their names and the names of the departments they manage. Left outer join (followed by projection): Name, DeptName (Employee ]|ID = MgrID Department Adapted from Fundamentals of Database Systems by Elmasri and Navathe

41 Outer Union Outer union takes union of tuples from two relations that are partially compatible, keeping those attributes that are not union compatible and padding with null values for those tuples that don’t have values for those attributes. Example: Student (ID, Name, Dept, Advisor) Faculty (ID, Name, Dept, Rank) Outer union will be relation with attributes ID, Name, Dept, Advisor, and Rank. Tuples taken from Student relation will have null values for Rank; tuples taken from Faculty relation will have null values for Advisor.


Download ppt "Relational Algebra Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4."

Similar presentations


Ads by Google