Vous êtes sur la page 1sur 9

/*******************************************************

Votre code ne compile pas en entier et/ou il contient plusieurs erreurs


0.5/1

*********************************************************/

/*

tudiant: Jorge Luis Dominguez Nontol


tudiant: Houda Dahir

*/

-- QUESTION 1

-- 1.a)
/*
drop table LAB_ARTICLE cascade constraints;
drop table LAB_FOURNISSEUR cascade constraints;*/
drop table LAB_ACHETER cascade constraints;
drop table LAB_CLIENT cascade constraints;
*/

-- 1.b)

create table LAB_ARTICLE (


NO_ARTICLE number(6) not null,
LIBELLE_ART varchar2(30) not null,
STOCK_ART number(6) default 0 not null,
MNT_PRIX_INVENT_ART number(6,2) default 0.0 not null,
constraint PK_ARTICLE primary key (NO_ARTICLE));

create table LAB_FOURNISSEUR (


NO_FOURNISSEUR number(6) not null,
NOM_FOU varchar2 (30) null,
ADR_FOU varchar2 (30) null,
VILLE_FOU varchar2 (30) null,
constraint PK_FOURNISSSEUR primary key (NO_FOURNISSEUR));

create table LAB_CLIENT (


NO_CLIENT number(6) not null,
NOM_CLI varchar2(30) not null,
ADR_CLI varchar2 (30) not null,
VILLE_CLI varchar2 (30) not null,
constraint PK_CLIENT primary key (NO_CLIENT));

create table LAB_ACHETER (


NO_FOURNISSEUR number (6) not null,
NO_ARTICLE number (6) not null,
MNT_PRIX_ACH number (6,2) default 0.0 not null,
DELAI_ACH number (2) default 5 not null,
constraint PK_ACHETER PRIMARY KEY (NO_FOURNISSEUR, NO_ARTICLE),
constraint FK_AC_NO_FOUR foreign key (NO_FOURNISSEUR) references
LAB_FOURNISSEUR (NO_FOURNISSEUR),
constraint FK_AC_NO_ART foreign key (NO_ARTICLE) references
LAB_ARTICLE (NO_ARTICLE),
constraint CT_PRIX_ACHAT_SUP_EGAL_0 check (MNT_PRIX_ACH >= 0),
constraint CT_DELAI_ACHAT_SUP_EGAL_0 check (DELAI_ACH >= 0));

-- 1.c)

insert into LAB_ARTICLE


(NO_ARTICLE,LIBELLE_ART,STOCK_ART,MNT_PRIX_INVENT_ART) values
('1','Parapluie','83','4,99');
insert into LAB_ARTICLE
(NO_ARTICLE,LIBELLE_ART,STOCK_ART,MNT_PRIX_INVENT_ART) values
('4','Casquette','0','19,99');
insert into LAB_ARTICLE
(NO_ARTICLE,LIBELLE_ART,STOCK_ART,MNT_PRIX_INVENT_ART) values
('2','Coupe-vent','112','9,99');
insert into LAB_ARTICLE
(NO_ARTICLE,LIBELLE_ART,STOCK_ART,MNT_PRIX_INVENT_ART) values
('3','Lunette soleil','30','49,99');

insert into LAB_FOURNISSEUR (NO_FOURNISSEUR,NOM_FOU,ADR_FOU,VILLE_FOU)


values ('125','Tremblay et associs','3443 de la tabatire','Qubec');
insert into LAB_FOURNISSEUR (NO_FOURNISSEUR,NOM_FOU,ADR_FOU,VILLE_FOU)
values ('130','Gamache et filles','321 de l''enclume','St-Augustin');
insert into LAB_FOURNISSEUR (NO_FOURNISSEUR,NOM_FOU,ADR_FOU,VILLE_FOU)
values ('135','DistribuPro','926 boul. industriel','Trois-Rivires');
insert into LAB_FOURNISSEUR (NO_FOURNISSEUR,NOM_FOU,ADR_FOU,VILLE_FOU)
values ('140','Anti-pluie plus','5555 2e avenue','Qubec');
insert into LAB_FOURNISSEUR (NO_FOURNISSEUR,NOM_FOU,ADR_FOU,VILLE_FOU)
values ('145','Protection en gros','9878 rue des lections','Montral');

insert into LAB_ACHETER


(NO_FOURNISSEUR,NO_ARTICLE,MNT_PRIX_ACH,DELAI_ACH) values
('125','1','3,99','2');
insert into LAB_ACHETER
(NO_FOURNISSEUR,NO_ARTICLE,MNT_PRIX_ACH,DELAI_ACH) values
('125','2','8,99','3');
insert into LAB_ACHETER
(NO_FOURNISSEUR,NO_ARTICLE,MNT_PRIX_ACH,DELAI_ACH) values
('125','3','29,99','10');
insert into LAB_ACHETER
(NO_FOURNISSEUR,NO_ARTICLE,MNT_PRIX_ACH,DELAI_ACH) values
('125','4','8,99','4');
insert into LAB_ACHETER
(NO_FOURNISSEUR,NO_ARTICLE,MNT_PRIX_ACH,DELAI_ACH) values
('130','1','3,79','1');
insert into LAB_ACHETER
(NO_FOURNISSEUR,NO_ARTICLE,MNT_PRIX_ACH,DELAI_ACH) values
('130','2','8,99','2');
insert into LAB_ACHETER
(NO_FOURNISSEUR,NO_ARTICLE,MNT_PRIX_ACH,DELAI_ACH) values
('135','3','20','1');
insert into LAB_ACHETER
(NO_FOURNISSEUR,NO_ARTICLE,MNT_PRIX_ACH,DELAI_ACH) values
('140','3','25','9');
insert into LAB_ACHETER
(NO_FOURNISSEUR,NO_ARTICLE,MNT_PRIX_ACH,DELAI_ACH) values
('140','4','12,25','8');

-- 1.d)

-- i)

alter table LAB_CLIENT


add NO_CLIENT_PARRAIN number (10) null;

-- ii)

alter table LAB_CLIENT


add constraint FK_NO_CLIENT_PARRAIN foreign key (NO_CLIENT_PARRAIN)
references
LAB_CLIENT;

-- 1.e)

insert into LAB_CLIENT


(NO_CLIENT,NOM_CLI,ADR_CLI,VILLE_CLI,NO_CLIENT_PARRAIN)
values (10,'Louis Tremblay','1414 du fer cheval','Qubec',null);

insert into LAB_CLIENT


(NO_CLIENT,NOM_CLI,ADR_CLI,VILLE_CLI,NO_CLIENT_PARRAIN)
values (11,'Lucie Gamache','855 3e avenue ','Qubec',10);

insert into LAB_CLIENT


(NO_CLIENT,NOM_CLI,ADR_CLI,VILLE_CLI,NO_CLIENT_PARRAIN)
values (12,'Julie Simard','1918 Napolon','St-Augustin',11);

insert into LAB_CLIENT


(NO_CLIENT,NOM_CLI,ADR_CLI,VILLE_CLI,NO_CLIENT_PARRAIN)
values (13,'Jean Par','3430 de la rivire','Chicoutimi',12);

insert into LAB_CLIENT


(NO_CLIENT,NOM_CLI,ADR_CLI,VILLE_CLI,NO_CLIENT_PARRAIN)
values (14,'Thomas voyer','3324 avenue du parc','Montral',11);

-- 2)
-- 2.a)

create or replace procedure LAB_SP_PRIX_PARAPLUIE is V_PRIX_PARAPLUIE


number(6,2);
begin
select MNT_PRIX_INVENT_ART into V_PRIX_PARAPLUIE
from LAB_ARTICLE
where LIBELLE_ART = 'Parapluie';
for V_POR in reverse 80..100
loop

if (MOD(V_POR, 5) = 0) then
dbms_output.put_line('Le prix du parapluie ' || V_POR
||'%'||' '|| 'est de ' ||' '|| round(V_PRIX_PARAPLUIE*(V_POR/100),
2)||'$');

end if;
end loop;
end;

-- 2.a.i)
set serveroutput on;

-- 2.a.ii)
execute LAB_SP_PRIX_PARAPLUIE;

-- 2.b)

-- 2.b.i et 2.b.ii)
create or replace procedure LAB_SP_PRIX_PARAPLUIE_TMR is V_PRIX_PARAPLUIE
number(6,2);
begin
select MNT_PRIX_INVENT_ART into V_PRIX_PARAPLUIE
from LAB_ARTICLE
where LIBELLE_ART = 'Parapluie';
for V_POR in reverse 80..100
loop

if (MOD(V_POR, 5) = 0) then
dbms_output.put_line('Le prix du parapluie ' || V_POR
||'%'||' '|| 'est de ' ||' '|| round(V_PRIX_PARAPLUIE*(V_POR/100),
2)||'$');

end if;
end loop;

exception -- 2.b.iii)

when TOO_MANY_ROWS then --(1)


dbms_output.put_line('Il y a aucun d''un article avec la libele
Parapluie'); --(2)
end;

-- 2.b.iv)
insert into LAB_ARTICLE (NO_ARTICLE, LIBELLE_ART, STOCK_ART,
MNT_PRIX_INVENT_ART)
values (6, 'Parapluie', 10, 5.29);

-- 2.b.v)
execute LAB_SP_PRIX_PARAPLUIE;

-- 2.b.vi)
execute LAB_SP_PRIX_PARAPLUIE_TMR;

-- 2.c

-- 2.c.i et 2.c.ii)
create or replace procedure LAB_SP_PRIX_PARAPLUIE_TMR_NDF is
V_PRIX_PARAPLUIE number(6,2);
begin
select MNT_PRIX_INVENT_ART into V_PRIX_PARAPLUIE
from LAB_ARTICLE
where LIBELLE_ART = 'Parapluie';
for V_POR in reverse 80..100
loop
if (MOD(V_POR, 5) = 0) then
dbms_output.put_line('Le prix du parapluie ' || V_POR
||'%'||' '|| 'est de ' ||' '|| round(V_PRIX_PARAPLUIE*(V_POR/100),
2)||'$');

end if;
end loop;

exception

when TOO_MANY_ROWS then


dbms_output.put_line('Il y a plus d''un article avec la libele
Parapluie');

-- 2.c.iii)
when NO_DATA_FOUND then --(1)
dbms_output.put_line('Il n''y a aucun article avec le libelle
Parapluie'); --(2)

end;

-- 2.c.iv)
-- (1)

delete from LAB_ACHETER


where NO_ARTICLE = 1;

-- (2)
delete from LAB_ARTICLE
where LIBELLE_ART = 'Parapluie';

-- 2.c.v)
execute LAB_SP_PRIX_PARAPLUIE_TMR;

-- 2.c.vi)
execute LAB_SP_PRIX_PARAPLUIE_TMR_NDF;

/
*========================================================================
======
QUESTION 3.
=========================================================================
=====*/
-- 3.a)
create or replace procedure LAB_SP_INFO_CLIENT (P_I_NO_CLIENT in number)
is

V_ADR_CLI varchar2(30);
V_VILLE_CLI varchar2(30);

begin
select ADR_CLI, VILLE_CLI into V_ADR_CLI, V_VILLE_CLI
from LAB_CLIENT
where NO_CLIENT = P_I_NO_CLIENT;
dbms_output.put_line(V_ADR_CLI);
dbms_output.put_line(V_VILLE_CLI);
exception
when NO_DATA_FOUND then
dbms_output.put_line('Le client avec le numro '||
P_I_NO_CLIENT||' n''existe pas.');
end;

-- 3.a.i)
execute LAB_SP_INFO_CLIENT (11);

-- 3.a.ii)
execute LAB_SP_INFO_CLIENT (14);

/
*========================================================================
======
QUESTION 4.
=========================================================================
=====*/

-- 4.a)
create or replace procedure LAB_SP_FOURNISSEUR is
V_NO_FOURNISSEUR number(6);
V_NOM_FOU varchar2(30);
V_VILLE_FOU varchar2(30);

cursor CURSEUR is
select NO_FOURNISSEUR, NOM_FOU, VILLE_FOU into V_NO_FOURNISSEUR,
V_NOM_FOU, V_VILLE_FOU
from LAB_FOURNISSEUR
order by NOM_FOU asc;

begin
open CURSEUR;
loop
fetch CURSEUR into V_NO_FOURNISSEUR, V_NOM_FOU, V_VILLE_FOU;
exit when CURSEUR%NOTFOUND;
dbms_output.put_line('No : '||V_NO_FOURNISSEUR||
', Fournisseur : '||V_NOM_FOU||
', Ville : '||V_VILLE_FOU);
end loop;
close CURSEUR;
end;

-- 4.b)++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++
execute LAB_SP_FOURNISSEUR;

-- 4.c)++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++
create or replace procedure LAB_SP_FOURNISSEUR_LOOP is
cursor CURSEUR is
select NO_FOURNISSEUR, NOM_FOU, VILLE_FOU
from LAB_FOURNISSEUR
order by NOM_FOU asc;

begin
for V_DONNEES in CURSEUR
loop
dbms_output.put_line('No : '||V_DONNEES.NO_FOURNISSEUR||
', Fournisseur : '||
V_DONNEES.NOM_FOU||
', Ville : '||V_DONNEES.VILLE_FOU);
end loop;
end;

-- 4.d)++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++
execute LAB_SP_FOURNISSEUR_LOOP;

/
*========================================================================
======
QUESTION 5.
=========================================================================
=====*/

-- 5.a)++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++
create or replace function LAB_FCT_PRIX_MOYEN_ACHAT (P_I_NO_ARTICLE
number)
return number is
V_MNT_PRIX_MOYEN_ACH number (6,2);

begin
select nvl(avg(MNT_PRIX_ACH), 0) into V_MNT_PRIX_MOYEN_ACH
from LAB_ACHETER
where NO_ARTICLE = P_I_NO_ARTICLE;
return V_MNT_PRIX_MOYEN_ACH;
end;

-- 5.b)++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++
select LAB_FCT_PRIX_MOYEN_ACHAT (3) from dual;

-- 5.c)++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++
select LAB_FCT_PRIX_MOYEN_ACHAT (4) from dual;

-- 5.d)++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++
select LAB_FCT_PRIX_MOYEN_ACHAT (9) from dual;

/
*========================================================================
======
QUESTION 6.
=========================================================================
=====*/

-- 6.a)++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++
create or replace trigger LAB_TRG_AUI_ACHETER after
update or insert on LAB_ACHETER
for each row
declare
V_NB_ARTICLE_EN_PERTE number;

begin
select count(NO_ARTICLE) into V_NB_ARTICLE_EN_PERTE
from LAB_ARTICLE
where NO_ARTICLE = :new.NO_ARTICLE and :new.MNT_PRIX_ACH >
MNT_PRIX_INVENT_ART;
if V_NB_ARTICLE_EN_PERTE > 0 then
raise_application_error(-20000, 'Le prix d''achat au
fournisseur ne peut pas
tre suprieur au prix d''inventaire, sinon il y
aura perte');
end if;
end;

-- 6.b)++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++
insert into LAB_ACHETER (NO_FOURNISSEUR, NO_ARTICLE, MNT_PRIX_ACH,
DELAI_ACH)
values (130, 3, 47.49, 15);

-- 6.c)++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++
insert into LAB_ACHETER (NO_FOURNISSEUR, NO_ARTICLE, MNT_PRIX_ACH,
DELAI_ACH)
values (125, 3, 57.49, 12);

-- 6.d)++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++
insert into LAB_ACHETER (NO_FOURNISSEUR, NO_ARTICLE, MNT_PRIX_ACH,
DELAI_ACH)
values (130, 4, 52.49, 18);

/
*========================================================================
======
QUESTION 7.
=========================================================================
=====*/

-- 7.a)++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++
select lpad('',LEVEL*2,'')||NOM_CLI as ARBRE,
NO_CLIENT,
NO_CLIENT_PARRAIN,
sys_connect_by_path(NOM_CLI, '/') as CHEMIN,
LEVEL as NIVEAU
from LAB_CLIENT
connect by prior NO_CLIENT = NO_CLIENT_PARRAIN
start with NO_CLIENT_PARRAIN is null;

-- 7.b)++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++
select substr(NOM_CLI,1,30) as ARBRE,
NO_CLIENT,
NO_CLIENT_PARRAIN,
sys_connect_by_path(NOM_CLI, '/') as CHEMIN,
LEVEL as NIVEAU
from LAB_CLIENT
connect by prior NO_CLIENT = NO_CLIENT_PARRAIN
start with NO_CLIENT_PARRAIN is null;

-- 7.c)++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++
select substr(NOM_CLI,1,30) as ARBRE,
NO_CLIENT,
NO_CLIENT_PARRAIN,
substr(NOM_CLI,1,60) as CHEMIN,
LEVEL as NIVEAU
from LAB_CLIENT
connect by prior NO_CLIENT = NO_CLIENT_PARRAIN
start with NO_CLIENT_PARRAIN is null;