RELATIONAL ALGEBRA (Chapter 2)

Slides:



Advertisements
Similar presentations
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Advertisements

Relational Algebra Rohit Khokher. Relational Algebra Set Oriented Operations UnionIntersectionDifference Cartesian Product Relation Oriented Operations.
Relational Algebra Dashiell Fryer. What is Relational Algebra? Relational algebra is a procedural query language. Relational algebra is a procedural query.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A Modified by Donghui Zhang.
INFS614, Fall 08 1 Relational Algebra Lecture 4. INFS614, Fall 08 2 Relational Query Languages v Query languages: Allow manipulation and retrieval of.
SQL and Relational Algebra Zaki Malik September 02, 2008.
1 Relational Algebra & Calculus. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
Relational Algebra 1 Chapter 5.1 V3.0 Napier University Dr Gordon Russell.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
Database Systems Chapter 6 ITM Relational Algebra The basic set of operations for the relational model is the relational algebra. –enable the specification.
FALL 2004CENG 351 File Structures and Data Managemnet1 Relational Algebra.
RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
1 Relational Algebra. 2 Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model supports.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
Introduction to Database Systems 1 Relational Algebra Relational Model: Topic 3.
Relational Algebra. Relational Query Languages n Query = “retrieval program” n Language examples: ù Theoretical : 1. Relational Algebra 2. Relational.
RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
Rutgers University Relational Algebra 198:541 Rutgers University.
Relational Algebra Chapter 4 - part I. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
CSCD343- Introduction to databases- A. Vaisman1 Relational Algebra.
Relational Algebra.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
1 ICS 184: Introduction to Data Management Lecture Note 10 SQL as a Query Language (Cont.)
Relational Algebra - Chapter (7th ed )
CS 370 Database Systems Lecture 11 Relational Algebra.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra.
CS 4432query processing1 CS4432: Database Systems II Lecture #11 Professor Elke A. Rundensteiner.
1 Relational Algebra & Calculus Chapter 4, Part A (Relational Algebra)
1 Relational Algebra and Calculas Chapter 4, Part A.
Relational Algebra.
ICS 321 Fall 2011 The Relational Model of Data (i) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 8/29/20111Lipyeow.
The Relational Algebra and Calculus
From Relational Algebra to SQL CS 157B Enrique Tang.
1 Relational Algebra Chapter 4, Sections 4.1 – 4.2.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 4 Relational Algebra.
Advanced Relational Algebra & SQL (Part1 )
Database Management Systems, R. Ramakrishnan1 Relational Algebra Module 3, Lecture 1.
Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
1 Overview of Query Evaluation Chapter Outline  Query Optimization Overview  Algorithm for Relational Operations.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
1 CS122A: Introduction to Data Management Lecture #7 Relational Algebra I Instructor: Chen Li.
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
Relational Algebra. CENG 3512 Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model supports.
Relational Calculus Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4.
Relational Algebra & Calculus
Ritu CHaturvedi Some figures are adapted from T. COnnolly
Database Systems Chapter 6
Chapter (6) The Relational Algebra and Relational Calculus Objectives
Relational Algebra Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4.
Database Systems (資料庫系統)
Chapter # 6 The Relational Algebra and Calculus
Relational Algebra Chapter 4 1.
Relational Algebra.
SQL The Query Language R & G - Chapter 5
Relational Algebra Chapter 4, Part A
Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition
Relational Algebra 461 The slides for this text are organized into chapters. This lecture covers relational algebra, from Chapter 4. The relational calculus.
Database Systems 10/13/2010 Lecture #4.
Relational Algebra.
LECTURE 3: Relational Algebra
Relational Algebra Chapter 4 1.
Relational Algebra Chapter 4, Sections 4.1 – 4.2
Chapter 2: Intro to Relational Model
EXAMPLE Emp table: SS# Name Age Salary dno 1 Joe Mary 20
Overview of Query Evaluation
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 (Chapter 2) 2018/11/13

Overall Organization Query Parser Query Optimizer Query Interpretor Relational Algebra operators: , , , , , , , ,  Index structures Abstraction of records Buffer Pool Manager File System 2018/11/13

RELATIONAL ALGEBRA INTRODUCTION Assume the following two relations: Emp (SS#, name, age, salary, dno) Dept (dno, dname, floor, mgrSS#) Relational algebra is a procedural query language, i.e., user must define both “how” and “what” to retrieve. Relational algebra consists of a set of operators that consume either one or two relations as input. An operator produces one relation as its output. Unary operators include: select, project, and rename Binary operators include: cartesian product, equality join, natural join, join, semi-join, division, union, and set difference. 2018/11/13

< < SELECT OPERATOR бSalary>30,000 age<25(Employee) < Select (б): selects tuples that satisfy a predicate; e.g., retrieve the employees whose salary is 30,000 бSalary=30,000(Employee) Conjunctive ( ) and disjunctive ( ) selection predicates are allowed; e.g., retrieve employees whose salary is higher than 30,000 and are younger than 25 years old: бSalary>30,000 age<25(Employee) Note that only selection predicates are allowed. A selection predicate is either (1) a comparison (=, ≠, ≤, ≥, <, >) between an attribute and a constant (e.g., salary = 30,000) or (2) a comparison between two different attributes of the same relation (e.g., salary = age × 100). Note: This operator is different than the SELECT command of SQL. < < < 2018/11/13

EXAMPLE Emp table: SS# Name Age Salary dno 1 Joe 24 20000 2 Mary 20 25000 3 Bob 22 27000 4 Kathy 30 30000 5 Shideh 4000 Emp table: 2018/11/13

EXAMPLE бSalary=30,000(Employee) Emp table: SS# Name Age Salary dno 1 Joe 24 20000 2 Mary 20 25000 3 Bob 22 27000 4 Kathy 30 30000 5 Shideh 4000 Emp table: бSalary=30,000(Employee) 2018/11/13

EXAMPLE бSalary=30,000(Employee) Emp table: SS# Name Age Salary dno 1 Joe 24 20000 2 Mary 20 25000 3 Bob 22 27000 4 Kathy 30 30000 5 Shideh 4000 Emp table: бSalary=30,000(Employee) SS# Name Age Salary dno 4 Kathy 30 30000 5 2018/11/13

EXAMPLE бAge>22(Employee) Emp table: SS# Name Age Salary dno 1 Joe 24 20000 2 Mary 20 25000 3 Bob 22 27000 4 Kathy 30 30000 5 Shideh 4000 Emp table: бAge>22(Employee) 2018/11/13

EXAMPLE бAge>22(Employee) Emp table: SS# Name Age Salary dno 1 Joe 24 20000 2 Mary 20 25000 3 Bob 22 27000 4 Kathy 30 30000 5 Shideh 4000 Emp table: бAge>22(Employee) SS# Name Age Salary dno 1 Joe 24 20000 2 4 Kathy 30 30000 5 2018/11/13

PROJECT OPERATOR Project (∏) retrieves a column. It is a unary operator that eliminate duplicates. e.g., name of employees: ∏ name(Employee) e.g., name of employees earning more than 30,000: ∏ name(бSalary>30,000(Employee)) 2018/11/13

EXAMPLE Emp table: SS# Name Age Salary dno 1 Joe 24 20000 2 Mary 20 25000 3 Bob 22 27000 4 Kathy 30 30000 5 Shideh 4000 Emp table: 2018/11/13

EXAMPLE Emp table: ∏ age(Emp) SS# Name Age Salary dno 1 Joe 24 20000 2 Mary 20 25000 3 Bob 22 27000 4 Kathy 30 30000 5 Shideh 4000 Emp table: ∏ age(Emp) Age 24 20 22 30 4 2018/11/13

EXAMPLE Emp table: ∏ name,age(бSalary=4000 (Emp) ) SS# Name Age Salary dno 1 Joe 24 20000 2 Mary 20 25000 3 Bob 22 27000 4 Kathy 30 30000 5 Shideh 4000 Emp table: ∏ name,age(бSalary=4000 (Emp) ) 2018/11/13

EXAMPLE Emp table: ∏ name,age(бSalary=4000 (Emp) ) SS# Name Age Salary dno 1 Joe 24 20000 2 Mary 20 25000 3 Bob 22 27000 4 Kathy 30 30000 5 Shideh 4000 Emp table: ∏ name,age(бSalary=4000 (Emp) ) SS# Name Age Salary dno 5 Shideh 4 4000 1 2018/11/13

EXAMPLE Emp table: ∏ name,age(бSalary=4000 (Emp) ) SS# Name Age Salary dno 1 Joe 24 20000 2 Mary 20 25000 3 Bob 22 27000 4 Kathy 30 30000 5 Shideh 4000 Emp table: ∏ name,age(бSalary=4000 (Emp) ) Name Age Shideh 4 2018/11/13

CARTESIAN PRODUCT Cartesian Product (R1 × R2) combines two relations by concatenating their tuples together, evaluating all possible combinations. If the name of a column is identical for two relations, this ambiguity is resolved by attaching the name of each relation to a column. e.g., Emp × Dept (SS#, name, age, salary, Emp.dno, Dept.dno, dname, floor, mgrSS#) If t(Emp) and t(Dept) is the cardinality of the Employee and Dept relations respectively, then the cardinality of Emp × Dept is: t(Emp) × t(Dept) 2018/11/13

CARTESIAN PRODUCT (Cont…) Example: Emp table: Dept table: SS# Name age salary dno 345 John Doe 23 25,000 1 943 Jane Java 25 28,000 2 876 Joe SQL 22 32,000 dno dname floor mgrSS# 1 Toy 345 2 Shoe 943 2018/11/13

CARTESIAN PRODUCT (Cont…) Cartesian product of Emp and Dept: Emp × Dept: SS# Name age salary Emp.dno Dept.dno dname floor mgrSS# 345 John Doe 23 25,000 1 Toy 943 Jane Java 25 28,000 2 876 Joe SQL 22 32,000 Shoe 2018/11/13

CARTESIAN PRODUCT Example: retrieve the name of employees that work in the toy department: 2018/11/13

CARTESIAN PRODUCT Example: retrieve the name of employees that work in the toy department: ∏name(бEmp.dno=Dept.dno(Emp × бdname=‘toy’(Dept))) 2018/11/13

CARTESIAN PRODUCT (Cont…) ∏name(бdname=‘toy’ (б Emp.dno=Dept.dno(Emp × Dept))) SS# Name age salary Emp.dno Dept.dno dname floor mgrSS# 345 John Doe 23 25,000 1 Toy 943 Jane Java 25 28,000 2 876 Joe SQL 22 32,000 Shoe 2018/11/13

CARTESIAN PRODUCT (Cont…) ∏name(бdname=‘toy’ (б Emp.dno=Dept.dno(Emp × Dept))) SS# Name age salary Emp.dno Dept.dno dname floor mgrSS# 345 John Doe 23 25,000 1 Toy 876 Joe SQL 22 32,000 943 Jane Java 25 28,000 2 Shoe 2018/11/13

CARTESIAN PRODUCT (Cont…) ∏name(бdname=‘toy’ (б Emp.dno=Dept.dno(Emp × Dept))) SS# Name age salary Emp.dno Dept.dno dname floor mgrSS# 345 John Doe 23 25,000 1 Toy 876 Joe SQL 22 32,000 2018/11/13

CARTESIAN PRODUCT (Cont…) ∏name(бdname=‘toy’ (б Emp.dno=Dept.dno(Emp × Dept))) Name John Doe Joe SQL 2018/11/13

RENAME OPERATOR Rename operator changes the name of its input table to its subscript, ρe2(Emp) Changes the name of Emp table to e2 2018/11/13

EQUALITY JOIN, NATURAL JOIN, JOIN, SEMI-JOIN Equality join connects tuples from two relations that match on certain attributes. The specified joining columns are kept in the resulting relation. ∏name(бdname=‘toy’(Emp Dept))) Natural join connects tuples from two relations that match on the specified common attributes How is an equality join between Emp and Dept using dno different than a natural join between Emp and Dept using dno? Equality join: SS#, name, age, salary, Emp.dno, Dept.dno, … Natural join: SS#, name, age, salary, dno, dname, … Join is similar to equality join using different comparison operators A S op = {=, ≠, ≤, ≥, <, >} att op att (dno) (dno) 2018/11/13

EXAMPLE JOIN Equality Join, (Emp Dept))) EMP Dept SS# Name Age Salary dno 1 Joe 24 20000 2 Mary 20 25000 3 Bob 22 27000 4 Kathy 30 30000 5 Shideh 4000 Equality Join, (Emp Dept))) dno dname floor mgrss# 1 Toy 5 2 Shoe EMP Dept (dno) SS# Name Age Salary EMP.dno Dept.dno dname floor mgrss# 1 Joe 24 20000 2 Shoe Mary 20 25000 Toy 5 3 Bob 22 27000 4 Kathy 30 30000 Shideh 4000 2018/11/13

EXAMPLE JOIN Natural Join, (Emp Dept))) EMP Dept SS# Name Age Salary dno 1 Joe 24 20000 2 Mary 20 25000 3 Bob 22 27000 4 Kathy 30 30000 5 Shideh 4000 Natural Join, (Emp Dept))) dno dname floor mgrss# 1 Toy 5 2 Shoe EMP Dept (dno) SS# Name Age Salary dno dname floor mgrss# 1 Joe 24 20000 2 Shoe Mary 20 25000 Toy 5 3 Bob 22 27000 4 Kathy 30 30000 Shideh 4000 2018/11/13

EXAMPLE JOIN Join, (Emp ρx(Emp)))) EMP Dept SS# Name Age Salary dno 1 Joe 24 20000 2 Mary 20 25000 3 Bob 22 27000 4 Kathy 30 30000 5 Shideh 4000 Join, (Emp ρx(Emp)))) dno dname floor mgrss# 1 Toy 5 2 Shoe EMP Dept Salary > 5 * salary SS# Name Age Salary dno x.SS# x.Name x.Age x.Salary x.dno 2 Mary 20 25000 1 Shideh 4 4000 3 Bob 22 27000 Kathy 30 30000 2018/11/13

EQUALITY JOIN, NATURAL JOIN, JOIN, SEMI-JOIN (Cont…) Example: retrieve the name of employees who earn more than Joe: ∏name(Emp (sal>x.sal)бname=‘Joe’(ρ x(Emp))) Semi-Join selects the columns of one relation that joins with another. It is equivalent to a join followed by a projection: Emp (dno)Dept ≡∏SS#, name, age, salary, dno(Emp Dept) 2018/11/13

UNION, SET DIFFERENCE & SET INTERSECT Union puts all tuples of two relations in one relation. To use this operator, two conditions must hold: The two relations must be of the same arity. The domain of ith attribute of the two participating relation must be the same. Set difference operator computes tuples that are in one relation, but not in another. Set intersect operator computes tuples that are common in two relations: The five fundamental operations of the relational algebra are: select, project, cartesian product, Union, and set difference All other operators can be constructed using these operators 2018/11/13

EXAMPLE Assume a database with the following three relations: Sailors (sid, sname, rating) Boats (bid, bname, color) Reserve (sid, bid, date) Query 1: Find the bid of red colored boats: 2018/11/13

EXAMPLE Assume a database with the following three relations: Sailors (sid, sname, rating) Boats (bid, bname, color) Reserve (sid, bid, date) Query 1: Find the bid of red colored boats: ∏bid(бcolor=red(Boats)) 2018/11/13

EXAMPLE Assume a database with the following three relations: Sailors (sid, sname, rating) Boats (bid, bname, color) Reserve (sid, bid, date) Query 1: Find the name of sailors who have reserved Boat number 2. 2018/11/13

EXAMPLE Assume a database with the following three relations: Sailors (sid, sname, rating) Boats (bid, bname, color) Reserve (sid, bid, date) Query 1: Find the name of sailors who have reserved Boat number 2. ∏sname(бbid=2(Sailors (sid)Reserve)) 2018/11/13

EXAMPLE Assume a database with the following three relations: Sailors (sid, sname, rating) Boats (bid, bname, color) Reserve (sid, bid, date) Query 1: Find the name of sailors who have reserved both a red and a green boat. 2018/11/13

EXAMPLE Assume a database with the following three relations: Sailors (sid, sname, rating) Boats (bid, bname, color) Reserve (sid, bid, date) Query 1: Find the name of sailors who have reserved both a red and a green boat. ∏sid( бcolor=red(Boats) (Sailors Reserve) )  ∏sid( бcolor=green(Boats) (Sailors Reserve) ) 2018/11/13

EXAMPLE Assume a database with the following three relations: Emp(SS#, name, salary, age, mgrss#) Query 3: Find employees whose salary is higher than their manager’s salary. 2018/11/13

EXAMPLE Assume a database with the following relations: Emp(SS#, name, salary, age, mgrss#) Query 3: Find name of those employees whose salary is higher than their manager’s salary. ∏Emp.name( бemp.salary>mgr.salary(Emp ρmgr(Emp)) ) mgrSS#=SS# 2018/11/13

EXAMPLE SS# Name Age Salary mgrSS# 1 Joe 30 30000 Null 2 Bob 23 25000 Shideh 4 40000 Kathy 15 32000 ∏Emp.name( бemp.salary>mgr.salary(Emp ρmgr(Emp)) ) mgrSS#=SS# SS# Name Age Salary mgrSS# Mgr.SS# Mgr.Name Mgr.Age Mgr.Salary Mgr.mgrSS# 2 Bob 23 25000 1 Joe 30 30000 Null 3 Shideh 4 40000 Kathy 15 32000 2018/11/13

EXAMPLE SS# Name Age Salary mgrSS# 1 Joe 30 30000 Null 2 Bob 23 25000 Shideh 4 40000 Kathy 15 32000 ∏Emp.name( бemp.salary>mgr.salary(Emp ρmgr(Emp)) ) mgrSS#=SS# SS# Name Age Salary mgrSS# Mgr.SS# Mgr.Name Mgr.Age Mgr.Salary Mgr.mgrSS# 3 Shideh 4 40000 1 Joe 30 30000 Null Kathy 15 32000 2018/11/13

EXAMPLE SS# Name Age Salary mgrSS# 1 Joe 30 30000 Null 2 Bob 23 25000 Shideh 4 40000 Kathy 15 32000 ∏Emp.name( бemp.salary>mgr.salary(Emp ρmgr(Emp)) ) mgrSS#=SS# Name Shideh Kathy 2018/11/13

OTHER ALGEBRAIC OPERATORS Assignment: One may use the assignment operator to assign the relation produced by an algebraic expression to a new relation, e.g., ToyEmp  Emp бname=‘toy’ (Dept) Deletion is performed using the assignment operator: R  R – E Example: Fire all employees whose salary is higher than 100,000 Emp  Emp – бsalary>100,000 (Emp) Insertion is also performed using the assignment operator: R  R  E Example: Hire Joe Emp  Emp  (5, Joe, 15, 30000, 1) 2018/11/13

OTHER ALGEBRAIC OPERATORS Update operator changes the value of records based on its input expression AE(R) Example, Give all employees a 10% raise: salary1.1*salary(Emp) 2018/11/13

A PROGRAM Recall Give the toy employees a 10% raise: Emp(SS#,name,age,salary, dno) Dept(dno,dname,floor,mgrss#) Give the toy employees a 10% raise: 2018/11/13