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

Slides:



Advertisements
Similar presentations
พีชคณิตแบบสัมพันธ์ (Relational Algebra) บทที่ 3 อ. ดร. ชุรี เตชะวุฒิ CS (204)321 ระบบฐานข้อมูล 1 (Database System I)
Advertisements

Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Relational Algebra Rohit Khokher. Relational Algebra Set Oriented Operations UnionIntersectionDifference Cartesian Product Relation Oriented Operations.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 (Continued) The Relational Algebra and Calculus.
Chapter 6 Additional Relational Operations Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
The Relational Algebra
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A Modified by Donghui Zhang.
Database Systems Chapter 6 ITM Relational Algebra The basic set of operations for the relational model is the relational algebra. –enable the specification.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Relational Algebra Chapter 4 - part I. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
The Relational Algebra and Calculus. Relational Algebra Overview Relational algebra is the basic set of operations for the relational model These operations.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra and Calculus.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Chapter 3 Section 3.4 Relational Database Operators
Relational Algebra Example Database Application (COMPANY) Relational Algebra –Unary Relational Operations –Relational Algebra Operations From Set Theory.
Relational Algebra - Chapter (7th ed )
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
METU Department of Computer Eng Ceng 302 Introduction to DBMS The Relational Algebra by Pinar Senkul resources: mostly froom Elmasri, Navathe and other.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra.
Module Coordinator Tan Szu Tak School of Information and Communication Technology, Politeknik Brunei Semester
Relational Algebra. 2 Outline  Relational Algebra Unary Relational Operations Relational Algebra Operations from Set Theory Binary Relational Operations.
Chapter 6 The Relational Algebra Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 4 Relational Algebra.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
DatabaseIM ISU1 Fundamentals of Database Systems Chapter 6 The Relational Algebra.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Al-Maarefa College for Science and Technology INFO 232: Database systems Chapter 3 “part 2” The Relational Algebra and Calculus Instructor Ms. Arwa Binsaleh.
Advanced Relational Algebra & SQL (Part1 )
The Relational Algebra. Slide 6- 2 Outline Relational Algebra Unary Relational Operations Relational Algebra Operations From Set Theory Binary Relational.
Slide 6- 1 Additional Relational Operations Aggregate Functions and Grouping A type of request that cannot be expressed in the basic relational algebra.
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
Relational Algebra National University of Computer and Emerging Sciences Lecture # 6 June 30,2012.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 6: Formal Relational.
Chapter 71 The Relational Data Model, Relational Constraints & The Relational Algebra.
Relational Algebra & Calculus
Ritu CHaturvedi Some figures are adapted from T. COnnolly
CSE202 Database Management Systems
Database Systems Chapter 6
Chapter (6) The Relational Algebra and Relational Calculus Objectives
Relational Model Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 3.
COMP3017 Advanced Databases
COP Introduction to Database Structures
Fundamental of Database Systems
Lecture 2 The Relational Model
Relational Algebra Chapter 4, Part A
Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition
Chapter 4 The Relational Algebra and Calculus
Relational Algebra 461 The slides for this text are organized into chapters. This lecture covers relational algebra, from Chapter 4. The relational calculus.
RELATIONAL ALGEBRA (Chapter 2)
Relational Algebra.
The Relational Algebra and Calculus
LECTURE 3: Relational Algebra
The Relational Algebra and Relational Calculus
376a. Database Design Dept. of Computer Science Vassar College
Relational Algebra Chapter 4 - part I.
Chapter 6: Formal Relational Query Languages
Chapter 2: Intro to Relational Model
CENG 351 File Structures and Data Managemnet
Relational Algebra & Calculus
Relational Algebra Chapter 4 - part I.
CS 405G: Introduction to Database Systems
Presentation transcript:

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

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.

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

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

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

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

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

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.

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

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.

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

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?

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

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

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

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.

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

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.

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

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 ≠.

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

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 ≥ 50000 (Employee))

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.

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.

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.

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)))

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

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

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.

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

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.

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

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

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

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

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

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.

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

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

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

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.