Query Processing: Relational Algebra

Slides:



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

Ver 1,12/09/2012Kode :CCs 111,sistem basisdataFASILKOM Chapter 2: Relational Model Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan.
CS4432: Database Systems II Query Operator & Algebraic Expressions 1.
Formal Relational Query Languages Chapter 6
Oct 28, 2003Murali Mani Relational Algebra B term 2004: lecture 10, 11.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Extended.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Tuple Relational.
Database Systems Chapter 6 ITM Relational Algebra The basic set of operations for the relational model is the relational algebra. –enable the specification.
The Relational Algebra and Calculus. Relational Algebra Overview Relational algebra is the basic set of operations for the relational model These operations.
Relational Algebra Instructor: Mohamed Eltabakh 1.
Relational Algebra Example Database Application (COMPANY) Relational Algebra –Unary Relational Operations –Relational Algebra Operations From Set Theory.
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational.
Relational Model Concepts. The relational model represents the database as a collection of relations. Each relation resembles a table of values. A table.
Chapter 6: Formal Relational Query Languages. 6.2 Chapter 6: Formal Relational Query Languages Relational Algebra Tuple Relational Calculus Domain Relational.
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.
CIS552Relational Model1 Structure of Relational Database Relational Algebra Extended Relational-Algebra-Operations Modification of the Database.
METU Department of Computer Eng Ceng 302 Introduction to DBMS The Relational Algebra by Pinar Senkul resources: mostly froom Elmasri, Navathe and other.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
Bayu Adhi Tama, ST., MTI. Introduction Relational algebra and relational calculus are formal languages associated with the relational.
Advanced Database Systems
CS 370 Database Systems Lecture 11 Relational Algebra.
3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Tuple Relational Calculus Domain Relational.
The Relational Algebra and Calculus
Chapter 6 The Relational Algebra Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
Chapter 5 Relational Algebra and Relational Calculus Pearson Education © 2009.
Chapter 2: Relational Model II Relational Algebra basics Relational Algebra basics.
Chapter 5 Relational Algebra Pearson Education © 2014.
Computing & Information Sciences Kansas State University Friday, 26 Jan 2008CIS 560: Database System Concepts Lecture 2 of 42 Friday, 29 August 2008 William.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
2.1 Chapter 2: Relational Model. 2.2 Chapter 2: Relational Model Structure of Relational Databases Fundamental Relational-Algebra-Operations Additional.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Extended.
Advanced Relational Algebra & SQL (Part1 )
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Module A: Formal Relational.
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
ICOM 5016 – Introduction to Database Systems Lecture 6 Dr. Manuel Rodriguez Department of Electrical and Computer Engineering University of Puerto Rico,
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus.
ASET Relational Algebra continues…. ASET Rename Operation Allows us to name, and therefore to refer to, the results of relational-algebra expressions.
IS 230Lecture 7Slide 1 Relational Algebra Lecture 8 Text Book – Chapter.
Database System Concepts, 6 th Ed. Chapter 6: Formal Relational Query Languages.
Relational Algebra Instructor: Mohamed Eltabakh 1.
Query Languages Language in which user requests information from the database. Categories of languages Procedural Non-procedural, or declarative “Pure”
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 6 th Edition Chapter 8: Relational Algebra.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 6: Formal Relational.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 6: Formal Relational.
Module 2: Intro to Relational Model
Chapter # 6 The Relational Algebra and Calculus
Fundamental of Database Systems
Relational Algebra - Part 1
Chapter 6: Formal Relational Query Languages
Relational Algebra - Select & Project
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Chapter 2: Relational Model
Lecture 4 of 42 Relational Joins Wednesday, 30 January 2008
The Relational Algebra and Relational Calculus
Session 3 Welcome: To session 3-the second learning sequence
Chapter 6: Formal Relational Query Languages
Instructor: Mohamed Eltabakh
Chapter 2: Intro to Relational Model
Relational Algebra.
Chapter 2: Intro to Relational Model
Chapter 6: Formal Relational Query Languages
Example of a Relation attributes (or columns) tuples (or rows)
Chapter 2: Intro to Relational Model
Relational Model B.Ramamurthy 5/28/2019 B.Ramamurthy.
Lecture 2 Relational Database
Presentation transcript:

Query Processing: Relational Algebra

Relational Algebra Basic operators – Selection (  ), select a subset of rows from relation – Project (  ), deletes unwanted columns from relation – Union (  ), tuples in relation 1 and in relation 2 – set difference ( – ), tuples in relation 1 but not in relation 2 – Cartesian product (x), allows us to combine 2 relations – Rename (  ), renaming the relations – Join ( ) – Aggreagte Function The operators take one or two relations as inputs and produce a new relation as a result.

Select Operation – Basic Concept Notation:  p (r) Used to select a subset of the tuples from a relation that satisfy a selection condition. Defined as:  p (r) = {t | t  r and p(t)} Where p is a formula in propositional calculus consisting of terms connected by :  (and),  (or),  (not) Each term is one of: op or where op is one of: =, , >, . <.  Select * from r where p

Select Operation – Example Relation r  country_name = ‘Indonesia’ (r) country_idcountry_nameregion_id 1US1 2Indonesia3 3Canada1 4Spain2 5England2 country_idcountry_nameregion_id 2Indonesia3

Select Operation – Exercise List of departements located in Indonesia Query Select * from departments d, locations l, countries c where country_name=‘Indonesia’ and c.country_id=l.country_id and l.location_id=d.location_id Rel. Algebra ?

Project Operation – Basic Concept Notation: where A 1, A 2 are attribute names and r is a relation name. The result is defined as the relation of k columns obtained by erasing the columns that are not listed Duplicate rows removed from result, since relations are sets Example: To eliminate the country_id and region_id attribute of countries  country_name (countries)

Project Operation – Example Relation r employee_idfirst_namelast_name phone_number last_name

Project Operation – Exercise List of name of employees and department name where they work in Query Select first_name,last_name,departement_name from employees e, departments d where e.department_id=d.department_id Rel. Algebra ?

Union Operation – Basic Concept Notation: r  s Defined as: r  s = {t | t  r or t  s} For r  s to be valid. 1. r, s must have the same arity (same number of attributes) 2. The attribute domains must be compatible (example: 2 nd column of r deals with the same type of values as does the 2 nd column of s) Example:  manager_id (departments)   manager_id (employees)

Union Operation – Example Relation r Relation s country_nameregion_id USAmerica IndonesiaAsia CanadaAmerica SpainEurope EnglandEurope country_nameregion_id ItalyEurope ThailandAsia South AfricaAfrica IndonesiaAsia r  s country_nameregion_id USAmerica IndonesiaAsia CanadaAmerica SpainEurope EnglandEurope ItalyEurope ThailandAsia South AfricaAfrica

Type Compatibility Two Relations are union-compatible if they have the same degree (i.e.,the same number of attributes) and the corresponding attributes are defined on the same domains. Suppose we have these tables : developingCountries (c_id, c_name, region_id) Countries (country_id, country_name, region_id) – These are union-compatible tables Union, intersection and set difference require union-compatible tables

Intersection Operation – Basic Concept The result of this operation, denoted by R ∩ S, is a relation that includes all tuples that appear in both R and S. The two operands must be "type compatible" Example: Relation r country_nameregion_id USAmerica IndonesiaAsia CanadaAmerica SpainEurope EnglandEurope Relation s country_nameregion_id IndonesiaAsia SpainEurope EnglandEurope ItalyEurope ThailandAsia South AfricaAfrica r ∩ s country_nameregion_id IndonesiaAsia SpainEurope EnglandEurope

Set Difference Operation – Basic Concept Notation r – s Defined as: r – s = {t | t  r and t  s} Set differences must be taken between compatible relations. – r and s must have the same arity – attribute domains of r and s must be compatible

Set Difference Operation – Example Relation r country_nameregion_id USAmerica IndonesiaAsia CanadaAmerica SpainEurope EnglandEurope Relation s country_nameregion_id IndonesiaAsia SpainEurope EnglandEurope ItalyEurope ThailandAsia South AfricaAfrica r - s country_nameregion_id USAmerica CanadaAmerica

Cartesian-Product Operation – Basic Concept Notation r x s Defined as: r x s = {t q | t  r and q  s} Assume that attributes of r(R) and s(S) are disjoint. (That is, R  S =  ). If attributes of r(R) and s(S) are not disjoint, then renaming must be used.

Cartesian-Product Operation – Example Cartesian Product : combine information from 2 tables, produces every possible combination Relation r country_nameregion_name SpainEurope EnglandEurope Relation s r x s last_name country_nameregion_namelast_name

Composition of Operations Can build expressions using multiple operations Example:  country_name,last_name  region_name=‘Europe’ (r x s) country_nameregion_namelast_name country_namelast_name SpainMourinho SpainRodgers SpainMoyes EnglandMourinho EnglandRodgers EnglandMoyes

Properties Notice that both union and intersection are commutative operations; that is R ∪ S = S ∪ R, and R ∩ S = S ∩ R Both union and intersection can be treated as n-ary operations applicable to any number of relations as both are associative operations; that is R ∪ (S ∪ T) = (R ∪ S) ∪ T, and (R ∩ S) ∩ T = R ∩ (S ∩ T) The minus operation is not commutative; that is, in general R - S ≠ S – R

Rename Operation Allows us to name, and therefore to refer to, the results of relational- algebra expressions. Allows us to refer to a relation by more than one name. Example:  x (E) returns the expression E under the name X If a relational-algebra expression E has arity n, then returns the result of expression E under the name X, and with the attributes renamed to A 1, A 2, …., A n. It returns a new relation with the same schema and content of the original, just different name (for the relation, attributes or both) The original relation is unchanged!

Join operation Natural join : Outer join :

Division Operation – Basic Concept Notation: r  s Suited to queries that include the phrase “for all”. Let r and s be relations on schemas R and S respectively where – R = (A 1, …, A m, B 1, …, B n ) – S = (B 1, …, B n ) The result of r  s is a relation on schema R – S = (A 1, …, A m ) r  s = { t | t   R-S (r)   u  s ( tu  r ) } Where tu means the concatenation of tuples t and u to produce a single tuple

Division Operation – Example Relation s country_name Spain England Relation r last_name country_namelast_name

Aggregate Functions – Basic Concept Aggregation function takes a collection of values and returns a single value as a result. avg: average value min: minimum value max: maximum value sum: sum of values count: number of values Aggregate operation in relational algebra – E is any relational-algebra expression – G 1, G 2 …, G n is a list of attributes on which to group (can be empty) – Each F i is an aggregate function – Each A i is an attribute name

Aggregate Functions – Example first_namejob_idsalary Jose Brendan David Pep Gerard Carlo Cristiano Lionel Andres Di Stefano Frank Sandro Wayne Fernando Moratti Xavi max(salary) job_idavg(salary) Relation r Job_id g avg(salary) (r)

a.Cari nama employee yang bekerja pada Bank Niaga. b.Cari nama dan kota tempat tinggal semua employee yang bekerja di Bank Niaga. c. Cari nama, alamat dan kota tempat tinggal semua employee yang bekerja di Bank Niaga dan berpenghasilan lebih dari Rp d. Cari nama semua employee yang tinggal di kota yang sama dengan perusahaan dimana mereka bekerja. e. Cari nama semua employee yang tinggal di kota dan jalan yang sama dengan manager mereka. f. Cari nama semua employee yang tidak bekerja di Bank Niaga. h. Asumsikan perusahaan berlokasi di beberapa kota. Cari semua perusahaan yang berlokasi di setiap kota dimana Small Bank Corporation berada. Diketahui skema basis data sbb: employee (person-name, street, city) works (person-name, company-name, salary) company (company-name, city) manages (person-name, manager-name)