0% found this document useful (0 votes)
22 views4 pages

Correction tp2

Uploaded by

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

Correction tp2

Uploaded by

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

Correction TP2 ORACLE : LDD & LID & Parcours hi�rarchique

---------------------------------------------------------

ALTER SESSION SET NLS_DATE_LANGUAGE = 'AMERICAN';

-- 2) create table empwork


as select * from emp;

-- 3) delete from emp;

alter table emp


modify (ename varchar2(9));

insert into emp


select empno, substr(ename,1,9), job, mgr, hiredate, sal,
comm, deptno
from empwork;

-- 4)

update emp
set hiredate = sysdate
where upper(ENAME) = 'MILLER';

-- 5)

-- 5.1)

select avg(nvl(comm, 0.00)) from emp;

-- 5.2)

select ename
from emp
where to_char(hiredate, 'DD/MM/YY') =
to_char(sysdate, 'DD/MM/YY');

-- 5.3) select distinct job


from emp
where deptno in (select deptno
from dept
where upper(loc) = 'CHICAGO');

-- 5.4) select empno, sal, hiredate


from emp
where deptno in (select deptno
from emp
group by deptno
having count(*) > 3);

-- 5.5)

SELECT job
FROM emp
GROUP BY job
HAVING AVG(sal) = (
SELECT MIN(AVG(sal))
FROM emp
GROUP BY job
);

-- 5.6)

select empno
from emp
where comm <= all (select distinct comm
from emp
where comm is not null
and comm>0)
and comm > 0;

-- 5.7) select empno, ename,


to_char(hiredate, 'MM/YY') "mois"
from emp
where hiredate = (select min(hiredate)
from emp);

-- 5.8)

select empno, ename


from emp
where empno <> 7844
and trunc(hiredate, 'MM') =
(select trunc(hiredate, 'MM')
from emp
where empno = 7844);

Parcours hi�rarchique (jointure r�cursive) :


---------------------------------------------

SELECT LPAD(' ', 2 * LEVEL) || ename AS "HIERARCHIE"


FROM emp
CONNECT BY PRIOR empno = mgr
START WITH UPPER(TRIM(ename)) = 'KING';

HIERARCHIE
--------------------
KING
JONES
SCOTT
ADAMS
FORD
SMITH
BLAKE
ALLEN
WARD
MARTIN
TURNER
JAMES
CLARK
MILLER

6)

LPAD(' ', 2 * LEVEL) :

2 * LEVEL cr�e un d�calage qui augmente avec la profondeur de la hi�rarchie.


Exemple :
LEVEL 1 : LPAD(' ', 2 * 1) = 2 espaces
LEVEL 2 : LPAD(' ', 2 * 2) = 4 espaces
LEVEL 3 : LPAD(' ', 2 * 3) = 6 espaces
Cela donne un affichage en arbre visuel o� chaque niveau est indent�.

7)

--- Les sup�rieurs hi�rarchiques de 'FORD' :

SELECT ename AS "FORD_SUP"


FROM emp
CONNECT BY empno = PRIOR mgr
START WITH UPPER(TRIM(ename)) = 'FORD';

FORD_SUP
-------------------
FORD
JONES
KING

--- Les sup�rieurs hi�rarchiques direct de 'FORD' :

select ename "FORD_SUP_DIRECT"


from emp
where level = 2
connect by EMPNO = PRIOR MGR
START WITH UPPER(TRIM(ename)) = 'FORD';

FORD_SUP
-------------------
JONES

--- Tous les subordonn�s de 'KING' :

SELECT ename "SUB_KING"


FROM emp
WHERE UPPER(TRIM(ename)) <> 'KING'
CONNECT BY PRIOR EMPNO = MGR
START WITH UPPER(TRIM(ename)) = 'KING'
ORDER BY ename;

SUB_KING
----------
ADAMS
ALLEN
BLAKE
CLARK
FORD
JAMES
JONES
MARTIN
MILLER
SCOTT
SMITH
TURNER
WARD

You might also like