Vous êtes sur la page 1sur 5

Master1 Info IFPRU 2007-2008 1

Autour de SQL avancé

1. Préambule
Le but du TD est d’étudier des requêtes SQL avancé

2. Requêtes
On donne, ci-dessous, la description du schéma global
Vol(novol, vildep, vilar ,dep h ,dep mn ,ar h ,ar mn ) ; Schéma relationnel décrivant les vols connus par leur numéro, la
ville de départ, celle d’arrivée et les horaires de départ et arrivée (en h et mn)
Pilote(nopil ,nom,adresse ,sal ,embauche) ; Schéma relationnel décrivant les pilotes identifiés par leur numéro et ayant
un nom, une adresse, un salaire et une date d’embauche.
Appareil(codetype, nbplace ,famille) ; Schéma relationnel décrivant les appareils ayant un codetype comme identifiant,
un nom de type ici appelé famille et une capacité (nb de places)
Avion(nuavion ,codetype,annserv,nom,nbhvol,compagnie) ; Schéma relationnel décrivant les avions ayant un numéro, un
codetyp, l’année de mise en service, le nombre d’heures de vol et la compagnie à laquelle ils sont rattachés.
Affectation(vol,date vol ,pilote, avion,nbpass ) ; Schéma relationnel décrivant l’affectation effectuée : pour un vol à une
date donnée et pour un pilote donné on a affecté un avion et un nbre de passagers.
Une instanciation peut être
insert into appareil values(’74E’,150,’BOEING 747-400’);
insert into appareil values(’AB3’,180,’AIRBUS A300’);
insert into appareil values(’741’,100,’BOEING 747-100’);
insert into appareil values(’734’,450,’BOEING 737-400’);
insert into appareil values(’SSC’,80,’CONCORDE’);

insert into avion values(’8832’,’734’,1998,’Ville de Paris’,16000,’AF’);


insert into avion values(’8567’,’734’,1998,’Ville de Rome’,800,’ALITALIA’);
insert into avion values(’8467’,’734’,1995,’Ville de Marseille’,600,’AF’);
insert into avion values(’7693’,’741’,1990,’Le Sud’,34000,’ALITALIA’);
insert into avion values(’8556’,’AB3’,1997,NULL,12000,’’RYANAIR’);
insert into avion values(’8432’,’AB3’,1994,’Malte’,10600,’AF’);
insert into avion values(’8118’,’74E’,1993,NULL,11800,’RYANAIR’);

insert into vol values(’AF8810’,’PARIS’,’DJERBA’,09,00,11,45);


insert into vol values(’AF8809’,’DJERBA’,’PARIS’,12,45,15,40);
insert into vol values(’IW201’,’LYON’,’FORT DE FRANCE’,09,45,15,25);
insert into vol values(’IW655’,’LA HAVANE’,’PARIS’,19,55,12,35);
insert into vol values(’IW433’,’PARIS’,’ST MARTIN’,17,00,08,20);
insert into vol values(’IW924’,’SIDNEY’,’COLOMBO’,17,25,22,30);
insert into vol values(’IT319’,’BORDEAUX’,’NICE’,10,35,11,45);
insert into vol values(’AF3218’,’MARSEILLE’,’FRANCFORT’,16,45,19,10);
insert into vol values(’AF3530’,’LYON’,’LONDRES’,08,00,08,40);
insert into vol values(’AF3538’,’LYON’,’LONDRES’,18,35,19,15);
insert into vol values(’AF3570’,’MARSEILLE’,’LONDRES’,09,35,10,20);
insert into vol values(’AF5570’,’NANTES’,’LONDRES’,10,35,11,50);

insert into pilote values(’1333’,’REDON’,’NANTES’,24000,’01/02/1997’);


insert into pilote values(’6589’,’DUVAL’,’PARIS’,18600,’12-mars-1999’);
insert into pilote values(’7100’,’MARTIN’,’LYON’,15600.00,’01-avril-1999’);
insert into pilote values(’3452’,’ANDRE’,’NICE’,22600.00,’12/12/1998’);
insert into pilote values(’3421’,’BERGER’,’REIMS’,18700.00,’08/11/1998’);
insert into pilote values(’6548’,’BARRE’,’LYON’,22680.00,’01/12/1999’);
insert into pilote values(’1243’,’COLLET’,’PARIS’,19000.00,’01/01/1997’);
insert into pilote values(’5643’,’DELORME’,’PARIS’,21850.00,’01/02/1999’);
Master1 Info IFPRU 2007-2008 2

insert into pilote values(’6723’,’MARTIN’,’ORSAY’,23150.00,’15-MAI-1999’);


insert into pilote values(’8843’,’GAUTHIER’,’TOURS’,17600.00,’20/01/2000’);
insert into pilote values(’3465’,’PIC’,’MONTPELLIER’,18650.00,’27/12/2002’);

insert into affectation values(’IW201’,’01/03/99’,’6723’,’8567’,310);


insert into affectation values(’IW201’,’02/03/99’,’6723’,’8832’,265);
insert into affectation values(’AF3218’,’12/06/99’,’6723’,’7693’,84);
insert into affectation values(’AF3530’,’12/11/99’,’6723’,’8432’,178);
insert into affectation values(’AF3530’,’13/11/99’,’6723’,’8432’,156);
insert into affectation values(’AF3538’,’21/12/99’,’6723’,’8118’,110);
insert into affectation values(’IW201’,’03/03/99’,’1333’,’8567’,356);
insert into affectation values(’IW201’,’12/03/99’,’6589’,’8467’,211);
insert into affectation values(’AF8810’,’02/03/99’,’7100’,’8556’,160);
insert into affectation values(’IT319’,’02/03/99’,’3452’,’8432’,105);
insert into affectation values(’IW433’,’22/03/99’,’3421’,’8556’,178);
insert into affectation values(’IW655’,’23/03/99’,’6548’,’8118’,118);
insert into affectation values(’IW655’,’20/12/99’,’1243’,’8467’,402);
insert into affectation values(’IW655’,’18/01/99’,’5643’,’8467’,398);
insert into affectation values(’IW924’,’30/04/99’,’8843’,’8832’,412);
insert into affectation values(’IW201’,’01/05/99’,’6548’,’8432’,156);
insert into affectation values(’AF8810’,’02/05/99’,’6589’,’7693’,88);
insert into affectation values(’AF3218’,’01/09/99’,’8843’,’7693’,98);
insert into affectation values(’AF3570’,’12/09/99’,’1243’,’7693’,56);

2.1 Opérateurs ensemblistes


– Quels sont les codes de types d’avions que les deux compagnies ’AF’ et ’ALITALIA’ exploitent en commun ?
select codetype from avion where compagnie=’AF’
intersect
select codetype from avion where compagnie=’ALITALIA’;
– Quels sont tous les codes de types d’avions que les deux compagnies ’AF’ et ’ALITALIA’ exploitent ?
select codetype from avion where compagnie=’AF’
union
select codetype from avion where compagnie=’ALITALIA’;
– Quels sont les codes de types d’avions exploités par la compagnies ’AF’ mais pas par la compagnie ’ALITALIA’ ?
select type from avion where compagnie=’AF’
minus
select type from avion where compagnie=’ALITALIA’;

2.2 Produit cartésien et jointures


– Produit cartésien
Quels sont les couples possibles (nomavion,nompilote) considérant les avions de la compagnie ’AF’ ;
select A.nom,B.nom
from Avion A, Pilote B
where compagnie=’AF’;
– Jointure relationnelle
Format (SQL89)
SELECT col1,col2 ..
FROM nomRel1, nomRel2, ...
WHERE condition de jointure
Nom des pilotes et numéro du vol pour ceux ayant volé entre le 1/03/1998 et le 31/6/1999 ?
select distinct P.nom , A.vol
from Pilote P, Affectation A
where P.nopil=A.pilote and date_vol between ’1/03/1999’ and ’31/07/1999’;
Jointure externe (+)
Permet d’extraire des n-uplets ne répondant pas aux critères de jointure.
Les n-uplets de la table ”dominante” sont retournés même s’ils ne répondent pas aux critères de jointure.
Nom des pilotes et numéro du vol effectués pour tous les pilotes (même les pilotes n’ayant pas volé).
Master1 Info IFPRU 2007-2008 3

select distinct nom,vol


from Pilote, Affectation WHERE nopil=pilote (+); Table dominante Pilote
ou
select distinct nom,vol
from Pilote, Affectation WHERE pilote (+)=nopil;
Format SQL2
Select col1,col2 ...
FROM nomRel1 [{INNER | {LEFT | RIGHT | FULL}, [OUTER]}]
JOIN nomRel2 {ON condition | USING (col1 [, ...]) }
|
{CROSSJOIN |NATURAL [{INNER | {LEFT | RIGHT | FULL}, [OUTER]}]
JOIN nomRel2
[WHERE condition];
--------------
INNER JOIN (jointure interne)
OUTER JOIN (jointure externe)
select distinct nom
from Pilote
JOIN Affectation ON nopil=pilote
where date\_vol between ’1/03/1999’ and ’31/07/1999’;
INNER est par défaut
Nom des pilotes et numéro du vol effectués pour tous les pilotes.
select distinct nom, vol
from Pilote CROSS JOIN Affectation;
-- equivalent à un produit cartésien
select distinct nom, vol
from Pilote, Affectation;

select distinct nom, vol


from Pilote NATURAL JOIN Affectation;
– equivalent à un produit cartésien
select distinct nom, vol
from Pilote
JOIN Affectation ON nopil=pilote;
identique à
select distinct nom, vol
from Pilote
INNER JOIN Affectation ON nopil=pilote;

Nom des pilotes et numéro du vol effectués pour tous les pilotes (même les pilotes n’ayant pas volé).
select distinct nom,vol
from Pilote
LEFT OUTER JOIN Affectation ON nopil=pilote;
ou
select distinct nom,vol
from Affectation
RIGHT OUTER JOIN Pilote ON nopil=pilote;
En sql-89 – beaucoup plus complexe
select distinct nom,vol
from Pilote, Affectation
where nopil=pilote
union
select nom,NULL
from Pilote
where not exists (select * from Affectation where Pilote.nopil= Affectation.pilote);

Remarques :
- On peut ainsi joindre plusieurs tables avec la syntaxe SQL2
- La jointure FULL OUTER JOIN permet d’extraire des n-uplets ne répondant pas aux critères de jointure et les deux
Master1 Info IFPRU 2007-2008 4

tables jouent un rôle symétrique. Nom des pilotes et numéro du vol effectués pour tous les pilotes (même les pilotes
n’ayant pas volé et les vols n’ayant pas encore de pilote). -)
select distinct nom,vol
from Affectation
FULL OUTER JOIN Pilote ON nopil=pilote;
ici pas de changement mais en fait on pourrait récupérer les pilotes sans affectations et les affectations sans pilote
– Libellé vol, avion et type des vols ayant été affrétés.
select A.vol,A.avion,C.type
From affectation A, vol B, avion C, appareil D
where A.vol=B.novol and A.avion=C.nuavion and C.type=D.codetype;

ou

select vol
from affectation
join vol on vol=novol
join avion on avion=nuavion
join appareil on type=codetype;
– Traiter la requête du TP relative à traduire la description de DESC avec la prise en compte des contraintes
select constraint\_name, constraint\_type
from user\_constraints
where table\_name =’PILOTE’;

Select column_name, constraint_name


from user_cons_columns
where table_name=’PILOTE’;

Create view AT\_CONS


as Select column\_name, A.constraint\_name, constraint\_type
from user\_constraints A, user\_cons\_columns B
where A.table\_name=B.table\_name and A.table\_name=’PILOTE’;

NOPIL PILOTE\_PK P

Create view AT\_TYPE


as Select column\_name,data\_type
from cols
where table\_name=’PILOTE’;

select A.column_name,data_type,constraint_name, constraint_type


from AT\_TYPE A ,AT\_CONS B
where A.column_name=B.column_name ;

NOPIL CHAR PILOTE\_PK P

select A.column_name,data_type,constraint_name, constraint_type


from AT\_TYPE A ,AT\_CONS B
where A.column\_name=B.column\_name (+) ;

NOPIL CHAR PILOTE\_PK P

ADRESSE VARCHAR2

SAL NUMBER
Master1 Info IFPRU 2007-2008 5

EMBAUCHE DATE

NOM VARCHAR2

2.3 Sous interrogations in, all et any, not exists


– Nom des vols assurés par des avions ayant plus de 20000 heures de vol ?
Select vol
from Affectation
where Affectation.avion in (select nuavion from Avion where nbhvol >20000);

– Nom des pilotes n’ayant pas volé ?


Select distinct nom
from pilote
where nopil not in (select pilote from affectation);
Select nom
from pilote
where not exists
(select pilote from affectation where pilote=nopil);
– Avions dont le nombre d’heures de vol est inférieur à celui de n’importe lequel des 734 ?
Select nom, type
from avion
where nbhvol > any (select nbhvol from avion where type=’734’);
Select nom, type
from avion
where nbhvol > any (select nbhvol from avion where type=’734’);
– Avions dont le nombre d’heures de vol est inférieur à toutes celles des 734 ?
Select nom, type
from avion
where nbhvol $>$ALL (select nbhvol from avion where type=’734’);

Vous aimerez peut-être aussi