CREATE TABLE "PRODUIT" ( "RÉF" Number NOT NULL ENABLE, CONSTRAINT PRODUIT_PK PRIMARY KEY ("RÉF")) ;
CREATE TABLE "ENTRER" ( "DATE-E" DATE NOT NULL ENABLE , "RÉF" Number NOT NULL ENABLE, "QTT" Number NOT NULL ENABLE, "P/UNIT" Number NOT NULL ENABLE, CONSTRAINT ENTRER_PK PRIMARY KEY ("DATE-E"), CONSTRAINT REF_FK FOREIGN KEY ("RÉF") REFERENCES PRODUIT("RÉF") ) ;
C’est une contrainte d’intégrité que tu veux sur le montant ? Si c’est le cas, alors tu peux utiliser le « check » contrainte de vérification ainsi la requête devient :
CREATE TABLE "ENTER" ( "DATE-E" DATE NOT NULL ENABLE, "RÉF" NUMBER NOT NULL ENABLE, "Qtt" NUMBER NOT NULL ENABLE, "P/UNIT" NUMBER NOT NULL ENABLE, "MONTANT" NUMBER NOT NULL , CONSTRAINT "ENTER_PK" PRIMARY KEY ("DATE-E") ENABLE, CONSTRAINT "RÉF_FK" FOREIGN KEY ("RÉF") REFERENCES "PRODUIT" ("RÉF"), CONSTRAINT check_montant check("MONTANT" = "P/UNIT" * "Qtt") ) ;
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionCREATE OR REPLACE TRIGGER tg_contraintes_stock before UPDATE or INSERT ON STOCK FOR EACH ROW DECLARE tg_exeption EXCEPTION; v_ok NUMBER:=-1; begin select count(*) into v_ok from entrer e, sortie s, stock st where e.ref = s.ref and s.ref = st.ref and e.ref = :new.ref and :new.MONTANT = e.MONTANT + s.MONTANT and :new.Qtt = ENTER.Qtt - sortir.Qtt; if v_ok<1 then RAISE tg_exeption ; end if; EXCEPTION WHEN tg_exeption THEN dbms_output.put_line ('attention !! erreur de saisie..... text ....du text'); ROLLBACK; end;
CREATE OR REPLACE TRIGGER tg_contraintes_stock before UPDATE or INSERT ON STOCK FOR EACH ROW DECLARE tg_exeption EXCEPTION; v_mt entrer.montant%TYPE :=0; v_qtt entrer.qtt%TYPE :=0; begin select (e.Qtt - s.Qtt), (e.MONTANT + s.MONTANT) into v_qtt, v_mt from entrer e, sortie s, stock st where e.ref = s.ref and s.ref = st.ref and e.ref = :new.ref; if (not( v_qtt= :new.qtt) or not(v_mt=:new.montant)) then RAISE tg_exeption ; end if; EXCEPTION WHEN tg_exeption THEN dbms_output.put_line ('attention !! erreur de saisie..... text ....du text'); ROLLBACK; end;
Bonjour,
Ce trigger simplifié fonctionne ( test ok).
Cependant, votre schéma n’est pas vraiment correct. Par ce que, La même référence produit pourrait être insérée plusieurs fois dans la même table ce qu’il n’est pas bon : c’est mieux de mettre à jour les caractéristiques du produit que de l’insérer à chaque fois (problème de redondance). C est pourquoi, il faut ajouter une contrainte (clé primaire ou KEY) sur ce champ (sur les trois table stock – sortie - entrer) ;
CREATE OR REPLACE TRIGGER tg_contraintes_stock before UPDATE or INSERT ON STOCK FOR EACH ROW DECLARE tg_exeption EXCEPTION; v_mt NUMBER:=0; v_qtt NUMBER :=0; begin select (e."Qtt" - s."Qtt"), (e."MONTANT" -s."MONTANT") into v_qtt, v_mt from entrer e, sortie s where e."REF" = s."REF" and e."REF" = :new."REF"; DBMS_OUTPUT.PUT_LINE( ' diff qte' ||v_qtt||'diff montant '||v_qtt) ; if (not(v_qtt= :new."Qtt") or not(v_mt=:new."MONTANT") ) then raise_application_error(-20004,‘erreur text ....exemple ’); end if; end; /