altriesercizi
TRANSCRIPT
-
8/10/2019 AltriEsercizi
1/28
Esercizi
(ci sono alcuni problemi con i font)
-
8/10/2019 AltriEsercizi
2/28
1
Algebra Relazionale
suppliers(sid, sname, address)parts(pid, pname, color)catalog(sid, pid, cost)
trovare i nomi dei fornitori che fornisconoparti rosse
psname(
psid(
ppid(
scolor=red(parts))
!
catalog)!
suppliers)
-
8/10/2019 AltriEsercizi
3/28
2
Algebra Relazionale
trovare gli identificativi dei fornitori che fornisconoparti rosse o verdi
psid
(ppid
(scolor = rosso
!
color = verde
(parts))!catalog)
trovare gli identificativi dei fornitori che fornisconoparti rosse oppure che stanno in piazza Garibaldi
(psid(ppid(scolor = rosso(parts))!catalog))"(psid(saddress= p.zza Garibaldi(suppliers)))
-
8/10/2019 AltriEsercizi
4/28
3
Relational Algebra
trovare gli identificativi dei fornitori che fornisconoogni parte
psid(catalog) - psid((psid(catalog)#parts) catalog)
trovare gli identificativi dei pezzi forniti da almenodue fornitori diversi
pR1.pid(sR1.pid=R2.pid$R1.sid"R2.sid
(r (R1,catalog)#r (R2,catalog)))
-
8/10/2019 AltriEsercizi
5/28
-
8/10/2019 AltriEsercizi
6/28
-
8/10/2019 AltriEsercizi
7/28
6
trovare gli identificativi dei piloti certificati peraerei Boeing
peid(saname=Boeing(certified!aircraft))
{c.eid | c(certified), a(aircraft) | a.aid=c.aid$a.aname=Boeing}
select certified.eidfrom certified, aircraftwhere certified.aid = aircraft.aid and
aircraft.aname =Boeing
-
8/10/2019 AltriEsercizi
8/28
7
trovare i nomi dei piloti certificati per aereiBoeing
pename(saname=Boeing(certified!aircraft!employees))
{e.ename | c(certified), a(aircraft),
e(employees) | a.aid=c.aid $
e.eid = c.eid $a.aname=Boeing}
select certified.enamefrom certified, aircraft, employees
where certified.eid = aircraft.eid andemployees.eid = certified.eid andaircraft.aname =Boeing
-
8/10/2019 AltriEsercizi
9/28
8
trovare gli identificativi di tutti gli aerei chepossono fare voli da NY a LA
paid(scruisingrange>distance(aircraft#s from = NY $to = LA(flights)))
{a.aid | a(aircraft), f(flights) | f.from = NY $f.to=LA $a.cruisingrange>f.distance}
select aircraft.aidfrom aircraft, flightswhere flights.to = NY and
flights.from = LA and
aircraft.cruisingrange >flights.distance
-
8/10/2019 AltriEsercizi
10/28
9
trovare gli identificativi dei voli che possonoessere effettuati da piloti con stipendiomaggiore di 100K $
pflno(sdistance 100k
(fligths!aircraft!certified !employees))
select f.flnofrom aircraft a, certified c, employees e,flights f
where a.aid = c.aid and e.eid = c.eid and
distance100k
-
8/10/2019 AltriEsercizi
11/28
10
trovare i nomi dei piloti che possono pilotare aereicon autonomia maggiore di 3000 km ma non sonocertificati per aerei Boeing
pename(employees!(peid(scruisingrange>3000(aircraft!certified))-peid(saname=Boeing(aircraft!certified))))
{e.ename | e(employees), c(certified), a(aircraft),c2(certified), a2(aircraft) | (a.aid=c.aid$e.eid=c.eid$
a.cruisingrange>3000)$
(a2.aname=Boeing$
c2.aid=a2.aid$c2.eid=e.eid)}
select e.enamefrom certified c, employees e, aircraft awhere a.aid=c.aid and e.eid=c.eid and
a.cruisingrange>3000 ande.eid not in (select c2.eid
from certified c2, aircraft a2 where c2.aid=a2.aid and
a2.aname=boeing)
-
8/10/2019 AltriEsercizi
12/28
11
trovare gli impiegati con il salario pi alto
r (e1, employees), peid(employees)-pe1.eid(employees !employees.salary>e1.salarye1)
{e.eid | e(employees),e1(employees) |(e1.salary>e.salary)}
select e.eidfrom employees ewhere e.salary =(select max (e1.salary)
from employees e1)
-
8/10/2019 AltriEsercizi
13/28
12
SQL
Si consideri il seguente schema relazionale:Emp(eid:integer, ename:string, age:integer,salary:real)
Works(eid:integer, did:integer,pct_time:integer)Dept(did:integer, dname:string, budget:real,managerid:integer)
-
8/10/2019 AltriEsercizi
14/28
13
SQL
Dare un esempio di chiave esterna checoinvolge la relazione Dept
Lattributo did in Works si riferisce allattributo did in Dept
-
8/10/2019 AltriEsercizi
15/28
14
SQL
Si scrivano le istruzioni SQL per definire lerelazioni Emp, Works e Dept
Create table Emp (eid integer, ename char(10), age integer, salary real, primary key (eid))
-
8/10/2019 AltriEsercizi
16/28
15
SQL
create table Works (eid integer,did integer,
pct_time integer,primary key (eid, did),foreign key (did) references Dept,foreign key (eid) references Emp,on delete cascade)
-
8/10/2019 AltriEsercizi
17/28
16
SQL
Create table Dept (did integer, budget real,
managerid integer, primary key (did), foreign key (managerid) references Emp.eid, on delete set null)
-
8/10/2019 AltriEsercizi
18/28
17
SQL
Definire Dept in modo che ogni dipartimentoabbia un manager
Create table Dept (did integer, budget real, managerid integer not null, primary key (did), foreign key (managerid) references Emp,
on delete set null)
-
8/10/2019 AltriEsercizi
19/28
18
SQL
In riferimento agli schemi di relazioneappena definiti, si esprima in SQL laseguente interrogazione:
Trovare i nomi e let di tutti gli impiegatiche lavorano nel dipartimento 2
Select E.ename, E.ageFrom Emp E, works W1Where E.eid=W1.eid and W1.did=2
-
8/10/2019 AltriEsercizi
20/28
19
SQL
In riferimento agli schemi di relazioneappena definiti, si esprima in SQL laseguente interrogazione:
Trovare i nomi e let di tutti gli impiegatiche lavorano sia nel dipartimento 1 che nel
dipartimento 2
Select E.ename, E.ageFrom Emp E, works W1, Works W2Where E.eid=W1.eid and W1.did= 1 and
E.eid=W2.eid and W2.did= 2
-
8/10/2019 AltriEsercizi
21/28
20
SQL
Si consideri il seguente schema di database:
Studente(snum, nome, et, anno)
Classe(nome, aula, ora, pid)Iscritto(snum, nome)Professore(pid, nome, did)
-
8/10/2019 AltriEsercizi
22/28
21
SQL
trovare nome ed et degli studenti iscritti alcorso di paleontologia
Select S.nome, S.etFrom studente S, iscritto IWhere S.snum=I.snum
and I.nome=paleontologia
-
8/10/2019 AltriEsercizi
23/28
22
SQL
In alternativa:
Select studente.nome, studente.etFrom studente SWhere S.snum in (select I.snum
from I where I.nome=paleontologia)
-
8/10/2019 AltriEsercizi
24/28
23
SQL
Trovare i nomi di tutti gli studenti del primoanno iscritti ad un corso tenuto dal prof.Rossi
Select distinct S.nomeFrom studente S, corso C, iscritto I, professore PWhere S.snum=I.snum and I.nome=C.nome and
C.pid=P.pid and P.nome=Rossi and S.anno=1
-
8/10/2019 AltriEsercizi
25/28
24
SQL
Oppure
Select distinct S.nomeFrom studente S
Where S.anno=1 and S.snum in (select I.snum from iscritto I where I.nome in (select C.nome
from corso C where C.pid in (select P.pid
from professore P where P.nome = Rossi)))
-
8/10/2019 AltriEsercizi
26/28
25
SQL
Trovare let media degli studenti, suddivisiper anno
Select anno, avg(et)From studente SGroup by anno
-
8/10/2019 AltriEsercizi
27/28
26
SQL
Trovare gli studenti che non sono iscritti adalcun corso
Select S.nome
From studente SWhere S.snum not in (select I.snum
from iscritto I)
-
8/10/2019 AltriEsercizi
28/28
27
SQL
Trovare let degli studenti piu anziani chesono iscritti al secondo anno oppure cheseguono un corso tenuto dal prof. Rossi
Select max(S.age)From studente SWhere S.anno=2 or S.snum in (select I.snum
from iscritto I, corso C, Professore P where I.nome=C.nome and C.pid=P.pid and
P.nome=Rossi)