0% found this document useful (0 votes)
7 views

TP2

Uploaded by

Islem Ghenai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

TP2

Uploaded by

Islem Ghenai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

create table Projet (

noproj number(1) not null primary key,

nomproj Varchar(10),

budget number(8) not null);

/* table creee */

insert into Projet values

(1,'gamma',320000);

insert into Projet values

(2,'papyrus',250000);

insert into Projet values

(3,'rodin',280000);

insert into Projet values

(4,'alpha',200000);

commit;

/* validation effectuee */

create table Dept (

nodept number(2) not null primary key,

nomdept Varchar(20),

lieu Varchar(20));

/* table creee */

insert into Dept values

(10,'finances','paris');

insert into Dept values

(20,'recherche','grenoble');
insert into Dept values

(30,'ventes','lyon');

insert into Dept values

(40,'fabrication','lille');

commit;

/* validation effectuee */

create table Emp (

noemp number(4) not null,

nomemp Varchar(20),

poste Varchar(20),

supr number(4),

datemb date,

sal number(4),

comm number(4),

noproj number(1) not null,

nodept number(2) not null,

CONSTRAINT pk_emp PRIMARY KEY(noemp),

CONSTRAINT fk_emp FOREIGN KEY(noproj) REFERENCES Projet(noproj),

CONSTRAINT fk2_emp FOREIGN KEY(nodept) REFERENCES Dept(nodept),

CONSTRAINT fk3_emp FOREIGN KEY(supr) REFERENCES Emp(noemp));

/* table creee */

insert into Emp values

(7839,'leroy','president',Null,'17/11/2009',7250,Null,1,10);

insert into Emp values

(7566,'mercier','directeur',7839,'02/04/2001',5950,Null,2,20);
insert into Emp values

(7698,'noiret','directeur',7839,'01/05/2001',5700,Null,4,30);

insert into Emp values

(7782,'lesage','directeur',7839,'09/06/2001',4900,Null,1,10);

insert into Emp values

(7788,'dubois','ingenieur',7566,'14/04/2005',6000,Null,2,20);

insert into Emp values

(7902,'chatel','ingenieur',7566,'03/12/2001',6000,Null,2,20);

insert into Emp values

(7369,'leclerc','commercial',7902,'17/12/2000',1800,null,2,20);

insert into Emp values

(7499,'biraud','commercial',7698,'20/02/2001',3200,600,3,30);

insert into Emp values

(7521,'berger','commercial',7698,'22/02/2001',2750,1000,4,30);

insert into Emp values

(7654,'martin','commercial',7698,'28/12/2001',2750,2800,3,30);

insert into Emp values

(7844,'benain','commercial',7698,'08/09/2001',3000,Null,3,30);

insert into Emp values

(7876,'clement','secretaire',7788,'18/05/2005',2200,Null,2,20);

insert into Emp values

(7900,'fremont','secretaire',7698,'03/12/2001',1900,Null,3,30);

insert into Emp values

(7934,'villard','secretaire',7782,'23/01/2002',2600,Null,3,30);

commit;

/* rentrer d'abord le president (pas de superieur) */

/* Validation effectuée */
/* QUESTIONS */

/* Q1 */

select distinct poste from Emp where sal >= 2500 order by poste desc;

/* Q2 */

select noemp, nomemp, poste from Emp where nodept = 10;

/*Q3 */

select noemp from Emp where noproj in(select noproj from Projet where nomproj = 'alpha');

/* Q4 */

select noemp from Emp where nodept in(select nodept from Dept where nomdept = 'recherche');

/* Q5 */

select noemp from Emp where nodept in(select nodept from Dept where nomdept = 'fabrication')
intersect select noemp from Emp where noproj in(select noproj from Projet where nomproj =
'alpha');

/* reponse: aucune ligne selectionnee -> ok */

/* Q6 */

select nomemp, poste, sal from Emp where nodept in(select nodept from Dept where nomdept =
'ventes');

/* Q7 */

select nomemp, poste from Emp where poste = (select poste from Emp where nomemp = 'biraud');
/* Q8 */

/* forme procedurale */

select nomemp, noemp

from Emp

where nodept in(select nodept from Dept where lieu = 'paris')

intersect

select nomemp, noemp from Emp

where sal>(select sal from Emp where poste = 'directeur'

and nodept in (select nodept from Dept where nomdept = 'ventes'));

/* ***** CORRECTION ALTERNATIVE ***** */

/* forme procedurale */

select nomemp

from Emp

where nodept in(select nodept from Dept where lieu='paris')

and sal > (select sal from Emp where poste = 'directeur' and nodept in(select nodept from Dept
where nomdept='ventes'));

/* forme relationnelle */

select e1.nomemp

from Emp e1, Emp e2, Dept d1, Dept d2

where e1.nodept = d1.nodept

and d1.lieu = 'paris'

and e2.poste = 'directeur'

and e2.nodept = d2.nodept

and d2.nomdept = 'ventes'


and e1.sal > e2.sal;

/* e1 et e2 representent deux occurences (2 employes distincts) et d1 et d2 2 departement distincts.


On compare les 2 employes suivant qu'ils soient a paris ou dans le departement de ventes, ainsi que
leur salaire */

/* Q9 */

select nodept, count(nomemp)

from Emp

Group by nodept;

/* Q10 */

select poste,round(avg(sal))

from Emp

group by poste

having count(poste) > 2;

/* Q11 */

select poste, min(sal),max(sal),avg(sal),12*avg(sal),sum(comm),count(nomemp)

from Emp

Group by poste;

/* Q12 */

select nomemp,poste,sal

from Emp

where sal > (select avg(sal) from Emp);

/* attention pour la comparaison: on compare soit 2 valeurs soit avec une sous-requete qui retourne
une valeur */
/* Q13 */

select poste

from Emp

group by poste

having avg(sal) > (select avg(sal) from Emp where poste='directeur');

/* PARTIE 2 */

/* Q1 */

insert into Dept values

(50,'achats','toulouse');

commit;

insert into Emp values

(7986,'richard','directeur',7839,'01/09/2006',6500,1250,1,50);

commit;

/* Q2 */

update Emp set sal=(sal+(sal*0.1))

where comm > 750

and nodept in(select nodept from Dept where nomdept = 'ventes');

/* Q3 */

delete Emp

where nodept in(select nodept from Dept where nomdept = 'achats')

or nodept in(select nodept from Dept where nomdept='fabrication');


delete Dept

where nomdept = 'achats'

or nomdept = 'fabrication';

/* FIN

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy