Download presentation
Presentation is loading. Please wait.
1
Query Processing: Relational Algebra ayu@telkomuniversity.ac.id
2
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.
3
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
4
Select Operation – Example Relation r country_name = ‘Indonesia’ (r) country_idcountry_nameregion_id 1US1 2Indonesia3 3Canada1 4Spain2 5England2 country_idcountry_nameregion_id 2Indonesia3
5
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 ?
6
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)
7
Project Operation – Example Relation r employee_idfirst_namelast_nameemailphone_number emp_001JoseMourinhomou@blue.com+612345678 emp_002BrendanRodgersbr@red.com+612345679 emp_003DavidMoyesmoyes@red.net+612345680 last_nameemail Mourinhomou@blue.com Rodgersbr@red.com Moyesmoyes@red.net
8
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 ?
9
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)
10
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
11
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
12
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
13
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
14
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
15
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.
16
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_nameemail Mourinhomou@blue.com Rodgersbr@red.com Moyesmoyes@red.net country_nameregion_namelast_nameemail SpainEuropeMourinhomou@blue.com SpainEuropeRodgersbr@red.com SpainEuropeMoyesmoyes@red.net EnglandEuropeMourinhomou@blue.com EnglandEuropeRodgersbr@red.com EnglandEuropeMoyesmoyes@red.net
17
Composition of Operations Can build expressions using multiple operations Example: country_name,last_name region_name=‘Europe’ (r x s) country_nameregion_namelast_nameemail SpainEuropeMourinhomou@blue.com SpainEuropeRodgersbr@red.com SpainEuropeMoyesmoyes@red.net EnglandEuropeMourinhomou@blue.com EnglandEuropeRodgersbr@red.com EnglandEuropeMoyesmoyes@red.net IndonesiaAsiaMourinhomou@blue.com IndonesiaAsiaRodgersbr@red.com IndonesiaAsiaMoyesmoyes@red.net country_namelast_name SpainMourinho SpainRodgers SpainMoyes EnglandMourinho EnglandRodgers EnglandMoyes
18
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
19
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!
20
Join operation Natural join : Outer join :
21
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
22
Division Operation – Example Relation s country_name Spain England Relation r last_nameemail Rodgersbr@red.com country_namelast_nameemail SpainMourinhomou@blue.com SpainRodgersbr@red.com IndonesiaMoyesmoyes@red.net IndonesiaMourinhomou@blue.com EnglandRodgersbr@red.com EnglandMoyesmoyes@red.net
23
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
24
Aggregate Functions – Example first_namejob_idsalary Jose225000 Brendan212000 David220000 Pep223000 Gerard221000 Carlo223000 Cristiano380000 Lionel378000 Andres375000 Di Stefano1150000 Frank1170000 Sandro1140000 Wayne365000 Fernando360000 Moratti1120000 Xavi370000 max(salary) 170000 job_idavg(salary) 1145000 271333 320666 Relation r Job_id g avg(salary) (r)
25
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 Rp2.000.000. 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)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.