Advertisement
Guest User

caca

a guest
Nov 21st, 2014
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. drop sequence CINE_CLIENT_SEQ;
  2. create sequence CINE_CLIENT_SEQ;
  3.  
  4. drop sequence CINE_EMPRUNT_SEQ;
  5. create sequence cine_emprunt_seq;
  6.  
  7. drop table CINE_EMPRUNT;
  8. drop table CINE_CLIENT;
  9.  
  10. create table CINE_CLIENT(
  11. NCLIENT integer,
  12. NOM varchar(20)
  13. constraint CK_CINE_CLIENT_NOM
  14. check(nom=upper(nom)),
  15. PRENOM varchar(20)
  16. constraint CK_CINE_CLIENT_PRENOM
  17. check(prenom=initcap(prenom)),
  18. VILLE varchar(20)
  19. constraint CK_CINE_CLIENT_VILLE
  20. check(ville=initcap(ville)),
  21. ANCIENNETE integer,
  22. nbre_emprunt integer,
  23. constraint PK_CINE_CLIENT primary key(NCLIENT)
  24. );
  25.  
  26.  
  27. create table CINE_EMPRUNT(
  28. NEMPRUNT INTEGER,
  29. NCLIENT integer,
  30. NFILM integer,
  31. DATEEMPRUNT date,
  32. retour char(3)
  33. not null
  34. constraint ck_cine_emprunt_date
  35. check (retour in('OUI','NON')),
  36. constraint PK_CINE_EMPRUNT primary key(NEMPRUNT)
  37. );
  38.  
  39.  
  40. create or replace trigger CINE_CLIENT_BIUR
  41. before insert or update
  42. on CINE_CLIENT
  43. for each row
  44. begin
  45. if INSERTING then
  46. :new.NCLIENT := CINE_CLIENT_SEQ.NEXTVAL;
  47. :new.NOM := UPPER(:new.NOM);
  48. :new.PRENOM := INITCAP(:new.PRENOM);
  49. :new.VILLE := INITCAP(:new.VILLE);
  50. else
  51. :new.NCLIENT := :old.NCLIENT;
  52. :new.NOM := :old.NOM;
  53. :new.PRENOM := :old.PRENOM;
  54. end if;
  55. if :new.ANCIENNETE is null then
  56. :new.ANCIENNETE := 0;
  57. end if;
  58. if :new.NBRE_EMPRUNT is null then
  59. :new.NBRE_EMPRUNT := 0;
  60. end if;
  61. end;
  62. /
  63.  
  64. create or replace trigger cine_emprunt_biur
  65. before insert or update
  66. on cine_emprunt
  67. for each row
  68. begin
  69. if inserting then
  70. :new.nemprunt := cine_emprunt_seq.nextval;
  71. else
  72. :new.nemprunt := :old.nemprunt;
  73. :new.dateemprunt := :old.dateemprunt;
  74. end if;
  75. if :new.dateemprunt is null then
  76. :new.dateemprunt :=sysdate;
  77. end if;
  78. if :new.retour is null then
  79. :new.retour := 'NON';
  80. end if;
  81. end;
  82. /
  83.  
  84. create or replace trigger cine_emprunt_biur2
  85. before insert or update
  86. on cine_emprunt
  87. for each row
  88. declare
  89. count_client number(1);
  90. count_film number(1);
  91. nempr integer;
  92. begin
  93. select count(*)
  94. into count_client
  95. from cine_client D
  96. where d.nclient = :new.nclient;
  97.  
  98. select count(*)
  99. into count_film
  100. from cine_film f
  101. where f.nfilm= :new.nfilm;
  102.  
  103. select nbre_emprunt
  104. into nempr
  105. from cine_client
  106. where nclient= :new.nclient;
  107.  
  108.  
  109. if count_client = 0 then
  110. raise_application_error(-20001, 'Nclient est inconnu');
  111. end if;
  112.  
  113. if count_film = 0 then
  114. raise_application_error(-20002,'Nfilm est inconnu');
  115. end if;
  116.  
  117. if inserting then
  118. if (nempr<=2) then
  119. update cine_client
  120. set nbre_emprunt = nbre_emprunt+1
  121. where nclient = :new.nclient;
  122. else raise_application_error(-20003,'Nbre max d''emprunt atteint');
  123.  
  124. end if;
  125. end if;
  126.  
  127. if updating then
  128. if :new.retour = 'OUI' then
  129. update cine_client
  130. set nbre_emprunt = nbre_emprunt-1
  131. where nclient = :new.nclient;
  132. end if;
  133. end if;
  134. end;
  135. /
  136.  
  137. insert into CINE_CLIENT values(12, 'duPoNt','jeaN','marseille',30,null);
  138. insert into CINE_CLIENT values(17, 'marie',null,null,null,null);
  139.  
  140. insert into Cine_emprunt values (null, 2, 2, null,null);
  141. insert into cine_emprunt values (null, 2, 3, null,null);
  142. insert into cine_emprunt values (null, 2, 4, null,null);
  143. insert into Cine_emprunt values (null, 2, 5, null,null);
  144.  
  145. select * from CINE_CLIENT;
  146.  
  147. update cine_emprunt
  148. set retour='OUI'
  149. where nemprunt=1;
  150.  
  151. select * from cine_emprunt;
  152. select * from cine_client;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement