SlideShare une entreprise Scribd logo
1  sur  7
Télécharger pour lire hors ligne
Corrigé du mini projet Pl/SQL



1 – Trigger

Dans la table Ord, le total correspond au cumul des totaux par ligne de
commande Itemtot.

create trigger tot
after Insert or update or delete
ON item
declare sumitem item.itemtot%type;

begin
for enr_rec in (select ordid from ord)
loop
      select sum(itemtot) into sumitem from item
      where item.ordid=enr_rec.ordid;
      update ord
      set ord.total= sumitem
      where ord.ordid=enr_rec.ordid;

end loop;

end;
/


Dans la table Item, le prix appliqué actual price est supérieur ou égal au prix
minimum minprice en cour à la date de la commande

create trigger mini
before insert or update on item
declare
prixactuel item.actualprice%type;
prixmini price.minprice%type;
begin
for enr_rec in(select prodid from item)
loop
      select actualPrice into prixactuel
      from item
      where prodid=enr_rec.prodid;
select minprice into prixmini
      from price
      where prodid=enr_rec.prodid and startdate<sysdate and (enddate is null
or enddate>sysdate);
      if(prixactuel<prixmini)
      then update item
             set actualprice=prixmini
             where prodid=enr_rec.prodid;
      end if;
end loop;
end;
/


Dans la table customer, repid reference un employé commercial



Create or replace trigger comercial before insert or update on customer
declare
numemp emp.job%type;
test emp.job%type:='SALESMAN';
begin

for num in (select repid from customer)
loop
      select job into numemp from emp where empno=num.repid;
      if(sql%notfound)
      then dbms_output.put_line('num d employer inxesistant: '||num.repid);
      else if(numemp!=test)
             then dbms_output.put_line('employer n est pas un
commercial:'||num.repid);
             end if;
      end if;
end loop;
end;
/
2- Script qui affiche le total des ventes par produit et par commercial
vue utilisé : vue1 pour facilité la recherche des produit et des employé

create or replace view vue1 as
select C.repid,E.ename, I.prodid,P.descrip, I.Itemtot
from customer c, ord O, item I,emp E,product P
where C.custid=O.custid and O.ordid=I.ordid and C.repid=E.empno and
P.proid=I.prodid;

Script :
declare
blanc vue1.descrip%type:='--------------------';
Cursor lesEmp is select distinct repid from vue1;
Cursor lesProd is select distinct prodid from vue1;
nomEmp vue1.ename%type;
nomProd vue1.descrip%type;
valeur vue1.itemtot%type;
i number;

begin
dbms_output.put(blanc||' ');
for numEmp in lesEmp
loop
select distinct ename into nomEmp from vue1 where repid=numEmp.repid;
dbms_output.put(nomEmp);
end loop;
dbms_output.new_line();

for numProd in lesProd
loop
select distinct descrip into nomProd from vue1 where prodid=numProd.prodid ;
dbms_output.put(nomProd);
i:=20-length(nomProd);
if(i>0)
then for j in 1..i
loop dbms_output.put(' ');
end loop;
end if;
dbms_output.put(' ');
for numEmp in lesEmp
loop
dbms_output.put(to_char(totalevente(numProd.prodid,numEmp.repid)));
i:=10-length(to_char(totalevente(numProd.prodid,numEmp.repid)));
if(i>0)
then for j in 1..i
loop dbms_output.put(' ');
end loop;
end if;

end loop;
dbms_output.new_line();
end loop;



end;




/
3. Créer une table de ce format et l’afficher (requête dynamique)

drop table resultat
/
Declare
      Cursor curRepName is
             Select Distinct RepName
             From VENTE
             Order By RepName;
      Cursor curDescrip is
             Select Distinct Descrip
             From VENTE
             Order By Descrip;
      myRepName VENTE.RepName%TYPE;
      myDescrip VENTE.Descrip%TYPE;
      myAmount Number(8,2);
      qryCreateTable      LONG;
      qryCreateRows       LONG;

Begin

        /* Debut de la requete pour creer la table */
        qryCreateTable:='CREATE TABLE RESULTAT (Descrip Varchar2(30)';

     /* Creation de la requete */
     Open curRepName;
     Loop
            Fetch curRepName into myRepName;
            Exit When curRepName%NOTFOUND;
            qryCreateTable:=qryCreateTable || ',' || myRepName || '
number(8,2)';
     End Loop;
     Close curRepName;

        /* Fin de la requete */
        qryCreateTable:=qryCreateTable || ')';

    /* On execute la requete */
    execute immediate qryCreateTable;
dbms_output.put_line(qryCreateTable);
    /* Creation des tuples */
Open curDescrip;
     Loop
            Fetch curDescrip into myDescrip;
            Exit When curDescrip%NOTFOUND;
            qryCreateRows:='insert into resultat (descrip) values ('||'''' ||
myDescrip ||''''||')';
            execute immediate qryCreateRows;
      --dbms_output.put_line(qryCreateRows);
     End Loop;
     Close curDescrip;

       /* On complete maintenant les valeurs */
       Open curRepName;
       Loop
             Fetch curRepName into myRepName;
             Exit When curRepName%NOTFOUND;

            Open curDescrip;
            Loop
                 Fetch curDescrip into myDescrip;
                 Exit When curDescrip%NOTFOUND;
                 Select sum(itemtot)
                 Into myAmount
                 From VENTE
                 Where repname=myRepName
                 And descrip=myDescrip;

                   qryCreateRows:='update resultat set ' || myRepName || '='''
|| myAmount || ''' where descrip=' || '''' || myDescrip || '''';
                   --dbms_output.put_line(qryCreateRows);
                   execute immediate qryCreateRows;
           End Loop;
           Close curDescrip;

       End Loop;
       Close curRepName;
End;
/

select * from resultat;
Traces d’exécution :

SQL> set serveroutput on;
SQL> set echo off;
SQL> set verify on;
SQL> @6.sql
DOC>L'affichage se fait sous forme de tableau de contingence avec
DOC>le nom du produit en lignes et le nom du commercial en colonnes

Procédure PL/SQL terminée avec succès.



PRODDESCRIP                 MARTIN           TURNER
------------------------------ ----------   ----------
ACE TENNIS RACKET I              896         1400
ACE TENNIS RACKET II               180       1800
…

Contenu connexe

Tendances

Sujet bac info 2012 g1, g2 et g3 avec correction
Sujet bac info 2012 g1, g2 et g3 avec correctionSujet bac info 2012 g1, g2 et g3 avec correction
Sujet bac info 2012 g1, g2 et g3 avec correctionborhen boukthir
 
噗浪最近有點胖
噗浪最近有點胖噗浪最近有點胖
噗浪最近有點胖Irvin Chen
 
Тененёв Анатолий, Boost.Asio в алгоритмической торговле
Тененёв Анатолий, Boost.Asio в алгоритмической торговлеТененёв Анатолий, Boost.Asio в алгоритмической торговле
Тененёв Анатолий, Boost.Asio в алгоритмической торговлеPlatonov Sergey
 
โปรแกรมย่อยและฟังก์ชันมาตรฐาน
โปรแกรมย่อยและฟังก์ชันมาตรฐานโปรแกรมย่อยและฟังก์ชันมาตรฐาน
โปรแกรมย่อยและฟังก์ชันมาตรฐานknangsmiley
 
C++14 reflections
C++14 reflections C++14 reflections
C++14 reflections corehard_by
 
openFrameworks、プログラムの制御構造の基本 - 多摩美メディアアートII
openFrameworks、プログラムの制御構造の基本 - 多摩美メディアアートIIopenFrameworks、プログラムの制御構造の基本 - 多摩美メディアアートII
openFrameworks、プログラムの制御構造の基本 - 多摩美メディアアートIIAtsushi Tadokoro
 
openFrameworks 外部ファイルを利用する - 画像、動画 - 多摩美メディアアートII
openFrameworks 外部ファイルを利用する - 画像、動画 - 多摩美メディアアートIIopenFrameworks 外部ファイルを利用する - 画像、動画 - 多摩美メディアアートII
openFrameworks 外部ファイルを利用する - 画像、動画 - 多摩美メディアアートIIAtsushi Tadokoro
 
openFrameworks 動きを生みだす様々なアルゴリズム - 多摩美メディアアートII
openFrameworks 動きを生みだす様々なアルゴリズム - 多摩美メディアアートIIopenFrameworks 動きを生みだす様々なアルゴリズム - 多摩美メディアアートII
openFrameworks 動きを生みだす様々なアルゴリズム - 多摩美メディアアートIIAtsushi Tadokoro
 
exercise of basic computer programming.docx
exercise of basic computer programming.docxexercise of basic computer programming.docx
exercise of basic computer programming.docxmiftah88
 
openFrameworks基礎 - 新規プロジェクトの作成、図形を描く 芸大グラフィクスプログラミングB
openFrameworks基礎 - 新規プロジェクトの作成、図形を描く 芸大グラフィクスプログラミングBopenFrameworks基礎 - 新規プロジェクトの作成、図形を描く 芸大グラフィクスプログラミングB
openFrameworks基礎 - 新規プロジェクトの作成、図形を描く 芸大グラフィクスプログラミングBAtsushi Tadokoro
 
openFrameworks入門 - 多摩美メディアアートII
openFrameworks入門 - 多摩美メディアアートIIopenFrameworks入門 - 多摩美メディアアートII
openFrameworks入門 - 多摩美メディアアートIIAtsushi Tadokoro
 
openFrameworks addonを利用する ofxControlPanel ofxOpenCv - 多摩美メディアアートII
openFrameworks addonを利用する ofxControlPanel ofxOpenCv - 多摩美メディアアートIIopenFrameworks addonを利用する ofxControlPanel ofxOpenCv - 多摩美メディアアートII
openFrameworks addonを利用する ofxControlPanel ofxOpenCv - 多摩美メディアアートIIAtsushi Tadokoro
 

Tendances (20)

Sujet bac info 2012 g1, g2 et g3 avec correction
Sujet bac info 2012 g1, g2 et g3 avec correctionSujet bac info 2012 g1, g2 et g3 avec correction
Sujet bac info 2012 g1, g2 et g3 avec correction
 
噗浪最近有點胖
噗浪最近有點胖噗浪最近有點胖
噗浪最近有點胖
 
week-24x
week-24xweek-24x
week-24x
 
Тененёв Анатолий, Boost.Asio в алгоритмической торговле
Тененёв Анатолий, Boost.Asio в алгоритмической торговлеТененёв Анатолий, Boost.Asio в алгоритмической торговле
Тененёв Анатолий, Boost.Asio в алгоритмической торговле
 
Monads
MonadsMonads
Monads
 
Gjuha paskal
Gjuha paskal Gjuha paskal
Gjuha paskal
 
Programming in C
Programming in CProgramming in C
Programming in C
 
โปรแกรมย่อยและฟังก์ชันมาตรฐาน
โปรแกรมย่อยและฟังก์ชันมาตรฐานโปรแกรมย่อยและฟังก์ชันมาตรฐาน
โปรแกรมย่อยและฟังก์ชันมาตรฐาน
 
C++14 reflections
C++14 reflections C++14 reflections
C++14 reflections
 
openFrameworks、プログラムの制御構造の基本 - 多摩美メディアアートII
openFrameworks、プログラムの制御構造の基本 - 多摩美メディアアートIIopenFrameworks、プログラムの制御構造の基本 - 多摩美メディアアートII
openFrameworks、プログラムの制御構造の基本 - 多摩美メディアアートII
 
C language
C languageC language
C language
 
openFrameworks 外部ファイルを利用する - 画像、動画 - 多摩美メディアアートII
openFrameworks 外部ファイルを利用する - 画像、動画 - 多摩美メディアアートIIopenFrameworks 外部ファイルを利用する - 画像、動画 - 多摩美メディアアートII
openFrameworks 外部ファイルを利用する - 画像、動画 - 多摩美メディアアートII
 
Docuemnto 6
Docuemnto 6Docuemnto 6
Docuemnto 6
 
openFrameworks 動きを生みだす様々なアルゴリズム - 多摩美メディアアートII
openFrameworks 動きを生みだす様々なアルゴリズム - 多摩美メディアアートIIopenFrameworks 動きを生みだす様々なアルゴリズム - 多摩美メディアアートII
openFrameworks 動きを生みだす様々なアルゴリズム - 多摩美メディアアートII
 
exercise of basic computer programming.docx
exercise of basic computer programming.docxexercise of basic computer programming.docx
exercise of basic computer programming.docx
 
Funções para validação
Funções para validaçãoFunções para validação
Funções para validação
 
openFrameworks基礎 - 新規プロジェクトの作成、図形を描く 芸大グラフィクスプログラミングB
openFrameworks基礎 - 新規プロジェクトの作成、図形を描く 芸大グラフィクスプログラミングBopenFrameworks基礎 - 新規プロジェクトの作成、図形を描く 芸大グラフィクスプログラミングB
openFrameworks基礎 - 新規プロジェクトの作成、図形を描く 芸大グラフィクスプログラミングB
 
Bifurcaciones (Ejemplo)
Bifurcaciones (Ejemplo)Bifurcaciones (Ejemplo)
Bifurcaciones (Ejemplo)
 
openFrameworks入門 - 多摩美メディアアートII
openFrameworks入門 - 多摩美メディアアートIIopenFrameworks入門 - 多摩美メディアアートII
openFrameworks入門 - 多摩美メディアアートII
 
openFrameworks addonを利用する ofxControlPanel ofxOpenCv - 多摩美メディアアートII
openFrameworks addonを利用する ofxControlPanel ofxOpenCv - 多摩美メディアアートIIopenFrameworks addonを利用する ofxControlPanel ofxOpenCv - 多摩美メディアアートII
openFrameworks addonを利用する ofxControlPanel ofxOpenCv - 多摩美メディアアートII
 

En vedette

formulaire d'inscription sur les listes électorales
formulaire d'inscription sur les listes électoralesformulaire d'inscription sur les listes électorales
formulaire d'inscription sur les listes électoralesGwladys
 
Gestion des dépendances dans un projet PHP - RMLL 2012
Gestion des dépendances dans un projet PHP - RMLL 2012Gestion des dépendances dans un projet PHP - RMLL 2012
Gestion des dépendances dans un projet PHP - RMLL 2012Jean-Marc Fontaine
 
Recommendation Letter- PDRM
Recommendation Letter- PDRMRecommendation Letter- PDRM
Recommendation Letter- PDRMSally Abas
 
Formation MySQL Workbench
Formation MySQL WorkbenchFormation MySQL Workbench
Formation MySQL WorkbenchIsenDev
 
Soutenance Zend Framework vs Symfony
Soutenance Zend Framework vs SymfonySoutenance Zend Framework vs Symfony
Soutenance Zend Framework vs SymfonyVincent Composieux
 
L’entretien campus france
L’entretien campus franceL’entretien campus france
L’entretien campus franceAntoine Lopez
 
La sécurité des applications avec ESAPI
La sécurité des applications avec ESAPILa sécurité des applications avec ESAPI
La sécurité des applications avec ESAPITakfarinas KENOUCHE
 
Présentation de symfony - Human talks aux docks le 8 juillet 2014
Présentation de symfony - Human talks aux docks le 8 juillet 2014Présentation de symfony - Human talks aux docks le 8 juillet 2014
Présentation de symfony - Human talks aux docks le 8 juillet 2014Tony Galmiche
 

En vedette (10)

formulaire d'inscription sur les listes électorales
formulaire d'inscription sur les listes électoralesformulaire d'inscription sur les listes électorales
formulaire d'inscription sur les listes électorales
 
Gestion des dépendances dans un projet PHP - RMLL 2012
Gestion des dépendances dans un projet PHP - RMLL 2012Gestion des dépendances dans un projet PHP - RMLL 2012
Gestion des dépendances dans un projet PHP - RMLL 2012
 
Recommendation Letter- PDRM
Recommendation Letter- PDRMRecommendation Letter- PDRM
Recommendation Letter- PDRM
 
Formation MySQL Workbench
Formation MySQL WorkbenchFormation MySQL Workbench
Formation MySQL Workbench
 
My SQL
My SQLMy SQL
My SQL
 
Comprendre la securite web
Comprendre la securite webComprendre la securite web
Comprendre la securite web
 
Soutenance Zend Framework vs Symfony
Soutenance Zend Framework vs SymfonySoutenance Zend Framework vs Symfony
Soutenance Zend Framework vs Symfony
 
L’entretien campus france
L’entretien campus franceL’entretien campus france
L’entretien campus france
 
La sécurité des applications avec ESAPI
La sécurité des applications avec ESAPILa sécurité des applications avec ESAPI
La sécurité des applications avec ESAPI
 
Présentation de symfony - Human talks aux docks le 8 juillet 2014
Présentation de symfony - Human talks aux docks le 8 juillet 2014Présentation de symfony - Human talks aux docks le 8 juillet 2014
Présentation de symfony - Human talks aux docks le 8 juillet 2014
 

Corrig Projet P L S Q L

  • 1. Corrigé du mini projet Pl/SQL 1 – Trigger Dans la table Ord, le total correspond au cumul des totaux par ligne de commande Itemtot. create trigger tot after Insert or update or delete ON item declare sumitem item.itemtot%type; begin for enr_rec in (select ordid from ord) loop select sum(itemtot) into sumitem from item where item.ordid=enr_rec.ordid; update ord set ord.total= sumitem where ord.ordid=enr_rec.ordid; end loop; end; / Dans la table Item, le prix appliqué actual price est supérieur ou égal au prix minimum minprice en cour à la date de la commande create trigger mini before insert or update on item declare prixactuel item.actualprice%type; prixmini price.minprice%type; begin for enr_rec in(select prodid from item) loop select actualPrice into prixactuel from item where prodid=enr_rec.prodid;
  • 2. select minprice into prixmini from price where prodid=enr_rec.prodid and startdate<sysdate and (enddate is null or enddate>sysdate); if(prixactuel<prixmini) then update item set actualprice=prixmini where prodid=enr_rec.prodid; end if; end loop; end; / Dans la table customer, repid reference un employé commercial Create or replace trigger comercial before insert or update on customer declare numemp emp.job%type; test emp.job%type:='SALESMAN'; begin for num in (select repid from customer) loop select job into numemp from emp where empno=num.repid; if(sql%notfound) then dbms_output.put_line('num d employer inxesistant: '||num.repid); else if(numemp!=test) then dbms_output.put_line('employer n est pas un commercial:'||num.repid); end if; end if; end loop; end; /
  • 3. 2- Script qui affiche le total des ventes par produit et par commercial vue utilisé : vue1 pour facilité la recherche des produit et des employé create or replace view vue1 as select C.repid,E.ename, I.prodid,P.descrip, I.Itemtot from customer c, ord O, item I,emp E,product P where C.custid=O.custid and O.ordid=I.ordid and C.repid=E.empno and P.proid=I.prodid; Script : declare blanc vue1.descrip%type:='--------------------'; Cursor lesEmp is select distinct repid from vue1; Cursor lesProd is select distinct prodid from vue1; nomEmp vue1.ename%type; nomProd vue1.descrip%type; valeur vue1.itemtot%type; i number; begin dbms_output.put(blanc||' '); for numEmp in lesEmp loop select distinct ename into nomEmp from vue1 where repid=numEmp.repid; dbms_output.put(nomEmp); end loop; dbms_output.new_line(); for numProd in lesProd loop select distinct descrip into nomProd from vue1 where prodid=numProd.prodid ; dbms_output.put(nomProd); i:=20-length(nomProd); if(i>0) then for j in 1..i loop dbms_output.put(' '); end loop; end if; dbms_output.put(' '); for numEmp in lesEmp loop
  • 4. dbms_output.put(to_char(totalevente(numProd.prodid,numEmp.repid))); i:=10-length(to_char(totalevente(numProd.prodid,numEmp.repid))); if(i>0) then for j in 1..i loop dbms_output.put(' '); end loop; end if; end loop; dbms_output.new_line(); end loop; end; /
  • 5. 3. Créer une table de ce format et l’afficher (requête dynamique) drop table resultat / Declare Cursor curRepName is Select Distinct RepName From VENTE Order By RepName; Cursor curDescrip is Select Distinct Descrip From VENTE Order By Descrip; myRepName VENTE.RepName%TYPE; myDescrip VENTE.Descrip%TYPE; myAmount Number(8,2); qryCreateTable LONG; qryCreateRows LONG; Begin /* Debut de la requete pour creer la table */ qryCreateTable:='CREATE TABLE RESULTAT (Descrip Varchar2(30)'; /* Creation de la requete */ Open curRepName; Loop Fetch curRepName into myRepName; Exit When curRepName%NOTFOUND; qryCreateTable:=qryCreateTable || ',' || myRepName || ' number(8,2)'; End Loop; Close curRepName; /* Fin de la requete */ qryCreateTable:=qryCreateTable || ')'; /* On execute la requete */ execute immediate qryCreateTable; dbms_output.put_line(qryCreateTable); /* Creation des tuples */
  • 6. Open curDescrip; Loop Fetch curDescrip into myDescrip; Exit When curDescrip%NOTFOUND; qryCreateRows:='insert into resultat (descrip) values ('||'''' || myDescrip ||''''||')'; execute immediate qryCreateRows; --dbms_output.put_line(qryCreateRows); End Loop; Close curDescrip; /* On complete maintenant les valeurs */ Open curRepName; Loop Fetch curRepName into myRepName; Exit When curRepName%NOTFOUND; Open curDescrip; Loop Fetch curDescrip into myDescrip; Exit When curDescrip%NOTFOUND; Select sum(itemtot) Into myAmount From VENTE Where repname=myRepName And descrip=myDescrip; qryCreateRows:='update resultat set ' || myRepName || '=''' || myAmount || ''' where descrip=' || '''' || myDescrip || ''''; --dbms_output.put_line(qryCreateRows); execute immediate qryCreateRows; End Loop; Close curDescrip; End Loop; Close curRepName; End; / select * from resultat;
  • 7. Traces d’exécution : SQL> set serveroutput on; SQL> set echo off; SQL> set verify on; SQL> @6.sql DOC>L'affichage se fait sous forme de tableau de contingence avec DOC>le nom du produit en lignes et le nom du commercial en colonnes Procédure PL/SQL terminée avec succès. PRODDESCRIP MARTIN TURNER ------------------------------ ---------- ---------- ACE TENNIS RACKET I 896 1400 ACE TENNIS RACKET II 180 1800 …