I/trigger table :
1/trigger maintient la capacité d'un cinema après chaque modificationde la table
salle
CREATE TABLE cinema (
nom VARCHAR2(50) PRIMARY KEY, -- Nom unique du cinéma
capacite NUMBER -- Capacité totale du cinéma
);
CREATE TABLE salle (
id_salle NUMBER PRIMARY KEY, -- Identifiant unique pour chaque salle
capacite NUMBER, -- Capacité de la salle
nomCinema VARCHAR2(50), -- Nom du cinéma auquel la salle appartient
FOREIGN KEY (nomCinema) REFERENCES cinema(nom) -- Clé étrangère vers la table
cinema
);
INSERT INTO cinema (nom, capacite) VALUES ('Cinéma Lumière', 300);
INSERT INTO cinema (nom, capacite) VALUES ('Cinéma Étoile', 150);
INSERT INTO salle (id_salle, capacite, nomCinema) VALUES (1, 100, 'Cinéma
Lumière');
INSERT INTO salle (id_salle, capacite, nomCinema) VALUES (2, 200, 'Cinéma
Lumière');
INSERT INTO salle (id_salle, capacite, nomCinema) VALUES (3, 150, 'Cinéma Étoile');
CREATE OR REPLACE TRIGGER cumulcapacite
AFTER UPDATE OF capacite ON salle
FOR EACH ROW
BEGIN
-- Vérification si la capacité a changé
IF :new.capacite != :old.capacite THEN
UPDATE cinema
SET capacite = capacite - :old.capacite + :new.capacite
WHERE nom = :new.nomCinema;
END IF;
END cumulcapacite;
/
create trigger CumulCapaciteGlobal
after insert or delete or update of capacite
on salle
begin
update cinema C
set capacite = (select sum(S.capacite) from salle S where C.nom = S.nomCinema);
end CumulCapaciteGlobal;
2/exemple 2
SQL> create table temp_affichage(
2 id_affichage number(2),
3 affichage varchar2(50));
Table created.
SQL> create sequence compteur start with 1 increment by 1;
Sequence created.
SQL> create or replace package TriggerMoment IS
2 v_compteur number :=0;
3 end triggerMoment;
4 /
Package created.
SQL> create or replace Trigger BEFOREinstruction
2 before update on produit
3 begin
4 insert into temp_affichage values(compteur.nextval,'Before niveau
instruction : compteur='||TriggerMoment.v_compteur);
5 TriggerMoment.v_compteur:=TriggerMoment.v_compteur+1;
6 end BEFOREinstruction;
7 /
Trigger created.
create or replace Trigger AFTERinstruction1
after update on produit
begin
insert into temp_affichage values(compteur.nextval,'After niveau instruction 1 :
compteur='||TriggerMoment.v_compteur);
TriggerMoment.v_compteur:=TriggerMoment.v_compteur+1;
end AFTERinstruction1;
/
create or replace Trigger AFTERinstruction2
after update on produit
begin
insert into temp_affichage values(compteur.nextval,'After niveau instruction 2 :
compteur='||TriggerMoment.v_compteur);
TriggerMoment.v_compteur:=TriggerMoment.v_compteur+1;
end AFTERinstruction2;
/
SQL> update produit set nom_produit = 'Produit B' where id_produit = 2;
1 row updated.
SQL> select * from temp_affichage;
ID_AFFICHAGE AFFICHAGE
------------ --------------------------------------------------
1 Before niveau instruction : compteur=0
2 After niveau instruction 2 : compteur=1
3 After niveau instruction 1 : compteur=2
expl gpt :
SQL> CREATE OR REPLACE TRIGGER insert_trigger
2 AFTER INSERT ON produit
3 FOR EACH ROW --sans FOR EACH ROW, je peux pas accéder aux variables :old
et :new dans un trigger.
4 BEGIN
5 DBMS_OUTPUT.PUT_LINE('Produit inséré: ' || :new.nom_produit || ' avec
prix: ' || :new.prix_unitaire);
6 END;
7 /
Trigger created.
SQL> insert into produit values (4,'Produit D',17.5);
Produit inséré: Produit D avec prix: 17.5
3/insertion avec id mis en trigger cours :
declare
x int;
begin
select prod_id.nextval into x from dual;
dbms_output.put_line(x);
end;
/
SQL> create sequence prod_id start with 5 increment by 1;
Sequence created.
CREATE OR REPLACE TRIGGER ProduitId
BEFORE INSERT ON produit
FOR EACH ROW
BEGIN
IF :new.ID_PRODUIT IS NULL THEN
:new.ID_PRODUIT := prod_id.nextval;
END IF;
END ProduitId;
insert into produit (nom_produit,prix_unitaire)values('Produit E',20.5); 5 Produit
E 20.5
II/Trigger Vieouuu :
SQL> CREATE OR REPLACE VIEW productF_show AS
2 SELECT id_produit, prix_unitaire
3 FROM produit
4 WHERE nom_produit = 'Produit F';
View created.
SQL> select * from productF_show;
ID_PRODUIT PRIX_UNITAIRE
---------- -------------
6 10.3
create or replace trigger ProductFInsert
instead of insert on ProductF_show
for each row
begin
insert into Produit values (:new.id_produit,'Produit F',:new.prix_unitaire);
end;
/
SQL> INSERT INTO productF_show (id_produit, prix_unitaire)
2 VALUES (7, 15.0);
1 row created.
SQL> select * from productF_show;
ID_PRODUIT PRIX_UNITAIRE
---------- -------------
6 10.3
7 15
SQL> select * from produit where nom_produit = 'Produit F';
6 Produit F 10.3
7 Produit F 15
--lezem dima trigger instead bsh najam nzid des donnés pour une vue (pour sa table
aussi bazz)