1 Relationele Databases 2002/2003 Hoorcollege 5 22 mei 2003 Jaap Kamps & Maarten de Rijke April Juli 2003 Plan voor Vandaag Praktische dingen 3.8, 3.9, 3.10, 4.1, 4.4 en 4.5 SQL Aantekeningen 3 Meer Queries. Theorie Silberschatz et al: hoofdstuk 4: SQL ( 4.5 4.11). 2 3 Docenten: Huishoudelijke Zaken 1. Jaap Kamps, Email: kamps@science.uva.nl 2. Maarten de Rijke, Email: mdr@science.uva.nl URL voor de cursus: http://www.science.uva.nl/~kamps/rdb/, of http://www.science.uva.nl/~mdr/teaching/rdb/0203/ Practicum http://www.science.uva.nl/~borkur/teaching/rdb0203/ Eerste inleveropdracht is nagekeken! Deadline tweede inleveropdracht: 6 juni 2002 3.8 Consider the relational database of Figure 3.39. Give an expression in the relational algebra for each query below. Figure 3.39: employee(person-name,street,city) works(person-name, company-name, salary) company(company-name, city) manages(person-name, manager-name) 4 5 3.8a. Modify the database such that Jones now lives in Newtown. employee Π person-name,street, Newtown ( σ person-name= Jones (employee)) (employee σ person-name= Jones (employee)) 3.8b. Give all employees of First Bank Corporation a 10-percent salary raise. works Π person-name,company-name,1.1 salary( σ company-name= First Bank Corporation (works)) (works σ company-name= First Bank Corporation (works)) 6 7 3.8c Give all managers in this database a 10-percent salary raise. De update syntax staat de referentie naar slechts 1 relatie toe. Omdat deze update zowel toegang moet hebben tot de relatie die we updaten, works, als een andere relatie, manages, moeten we dit stapsgewijs doen. Eerst identificeren we de tuples van works die moeten worden ge-update en bewaren die in een tijdelijke relatie t 1. Dan creëeren we een tijdelijke relatie t 2 met de nieuwe tupples. Als laatste verwijderen we uit works de tuples in t 1, en voegen de tuples in t 2 toe. 3.8c. Give all managers in this database a 10-percent salary raise. t 1 Π works.person-name,company-name,salary( σ works.person-name=manager-name(works manages)) t 2 Π person-name,company-name,1.1 salary(t 1 ) works (works t 1 ) t 2
8 9 3.8d. Give all managers in this database a 10-percent salary raise, unless the salary would be greater than $100,000. In such cases, give only a 3-percent raise. t 1 Π works.person-name,company-name,salary( σ works.person-name=manager-name(works manages)) t 2 Π works.person-name,company-name,salary 1.03( σ t1.salary 1.1>100000(t 1 )) t 2 t 2 (Π works.person-name,company-name,salary 1.1( σ t1.salary 1.1 100000(t 1 ))) works (works t 1 ) t 2 3.8e. Delete all tuples in the works relation for employees of Small Bank Corporation. works works σ company-name= Small Bank Corporation (works) 10 11 3.9 Using the bank example, write relational-algebra queries to find the accounts held by more than two customers in the following ways: a. using an aggregate function t 1 account-numberg count customer-name as num-holders(depositor) Π account-number(σ num-holders>2(t 1 )) b. without using any aggregate functions t 1 (ρ d1 (depositor) ρ d2 (depositor) ρ d3 (depositor)) t 2 σ (d1.account-number=d2.account-number=d3.account-number)(t 1 ) Π d1.account-number( σ (d1.customer-name d2.customer-name d1.customer-name d3.customer-name d2.customer-name d3.customer-name)(t 2 )) 3.10 Consider the relational database of Figure 3.39. Give a relationalalgebra expression for each of the following queries: 3.10a. Find the company with the most employees. t 1 company-nameg count-distinct person-name as num-employees(works) t 2 G max num-employees as max-num-employees(t 1 ) Π company-name(t 1 (num-employees=max-num-employees) t 2 ) 3.10b. Find the company with the smallest payroll. t 1 company-nameg sum salary as payroll (works) t 2 G min payroll as min-payroll(t 1 ) Π company-name(t 1 (payroll=min-payroll) t 2 ) 12 3.10c. Find those companies whose employees earn a higher salary, on average, than the average salary at First Bank Corporation. t 1 company-nameg avg salary as avg-salary(works) t 2 σ company-name= First Bank Corporation (t 1 ) Π t1.company-name(t 1 (t1.avg-salary>t 2.avg-salary) t 2 ) 13 4.4 Let the following relation schemas be given: R = (A, B, C) S = (D, E, F ) Let relations r(r) and s(s) be given. Give an expression in SQL that is equivalent to each of the following queries: a Π A (r) select distinct A from r 14 4.4 (vervolg). Give an SQL expression for each of the following queries: b σ B=17 (r) c r s d Π A,F (σ C=D (r s)) select from r where B = 17 select distinct from r, s select distinct A, F from r, s where C = D 15
16 17 4.5 Let R = (A, B, C) and let r 1 and r 2 both be relations on schema R. Give an expression in SQL that is equivalent to each of the following queries: a r 1 r 2 (select from r 1 ) union (select from r 2 ) b r 1 r 2 Dit kan met de intersect operatie (zoals in a), maar het kan ook met een geneste subquery. 4.5 (vervolg). Give an SQL expression for each of the following queries: b r 1 r 2 c r 1 r 2 select from r 1 where (A, B, C) in (select from r 2 ) select from r 1 where (A, B, C) not in (select from r 2 ) 18 19 4.5 (vervolg). Give an SQL expression for each of the following queries: d Π AB (r 1 ) Π BC (r 2 ) select r 1.A, r 2.B, r 2.C from r 1, r 2 where r 1.B = r 2.B 4.1 Consider the insurance database of Figure 4.12, where the primary keys are underlined. Construct the following SQL queries for this relational database. Figure 4.12: person(driver-id,name,address) car(license,year,model) accident(report-number,location,date) owns(driver-id,license) 20 21 4.1a. Find the total number of people who owned cars that were involved in accidents in 1989. select count (distinct name) from accident, participated, person where accident.report-number = participated.report-number and participated.driver-id = person.driver-id and date between date 1989-00-00 and date 1989-12-31 Dit is niet hetzelfde als het aantal ongelukken in 1989, er kunnen mensen zijn die bij meerdere ongelukken betrokken waren. 4.1b. Find the number of accidents in which the cars belonging to John Smith were involved. select count (distinct *) from accident where exists (select * from participated, person where participated.driver-id = person.driver-id and person.name = John Smith and accident.report-number = participated.report-number) 4.1c. Add a new accident to the database; assume any values for required attributes. We nemen aan dat de driver-id Jones is, en dat Jones een (en precies een) auto model Toyota heeft. Eerst moeten we de licence vinden van de auto. Dan moeten de participated en accident relaties worden aangepast, om het ongeluk te registreren en het aan de genoemde auto te relateren. We nemen de volgende attribuut waarden aan: de location is Berkeley, de date is 2003-05-22, het report-number is 4007 en de damage-amount is 3000. 22 4.1c. Add a new accident to the database; assume any values for required attributes. insert into accident values (4007, 2003-05-22, Berkeley ) insert into participated select owns.driver-id, car.license, 4007, 3000 from person, owns, car where person.name = Jones and person.driver-id = owns.driver-id and owns.license = car.license and car.model = Toyota 23
24 25 4.1d. Delete the Mazda belonging to John Smith. Omdat model geen key van car is, moeten we aannemen dat maar 1 van John Smith s auto s een Mazda is, of dat al zijn auto s Mazda s zijn (de query is hetzelfde). delete car where model = Mazda and license in (select license from person, owns where person.name = John Smith and person.driver-id = owns.driver-id) 4.1e. Update the damage amount for the car with license number AABB2000 in the accident with report number AR2197 to $3000. update participated set damage-amount = 3000 where report-number = AR2197 and driver-id in (select driver-id from owns where license = AABB2000 ) 26 27 Database System Concepts Hoofdstuk 4 Over Structured Query Language of SQL Vorige keer 4.1 tot en met 4.4 Vandaag 4.5 tot en met 4.11. select A 1, A 2,..., A n from r 1, r 2,..., r n where P 28 29 30 31
32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47
48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63
64 65 66 67 Overzicht Vandaag Database System Concepts. Hoofdstuk 4: SQL ( 4.5 4.11). Opgaven: 3.8, 3.9, 3.10, 4.1, 4.4 en 4.5. SQL Aantekeningen 3. Meer Queries. 68 69 Volgende Keer bij Relationele Databases End! Opgaven voor volgende keer (5 juni): 4.3 4.8 4.10 Database System Concepts. Integrity & Security (Hoofdstuk 6).