Création d'un trigger de mise à jour
Bonjour
Je souhaite créer un trigger sous ORACLE qui met a jour le stock(quantité) dans la table produit après une insertion d'une ligne dans la table commande. le code sql est :
Table produit:
Code:
1 2 3 4 5 6 7 8 9 10 11 12
|
CREATE TABLE `produit_` (
`idproduit` INT(11) NOT NULL AUTO_INCREMENT,
`nom_produit` VARCHAR(50) NOT NULL DEFAULT '0',
`quantite` INT(4) UNSIGNED ZEROFILL NOT NULL DEFAULT '0000',
`prix_unitaire` INT(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`idproduit`)
)
COLLATE='utf8mb4_0900_ai_ci'
ENGINE=InnoDB
AUTO_INCREMENT=11
; |
la table commande:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
CREATE TABLE `commande` (
`idcommande` INT(11) NOT NULL AUTO_INCREMENT,
`idperso` INT(11) NOT NULL,
`quantiteComm` INT(4) UNSIGNED ZEROFILL NOT NULL,
`idprod` INT(11) NOT NULL DEFAULT '0',
`datecomm` VARCHAR(50) NOT NULL DEFAULT '0',
PRIMARY KEY (`idcommande`, `idperso`, `idprod`),
INDEX `FK_commande_personnes` (`idperso`),
INDEX `FK_commande_produit_` (`idprod`),
CONSTRAINT `FK_commande_personnes` FOREIGN KEY (`idperso`) REFERENCES `personnes` (`Idpersonne`),
CONSTRAINT `FK_commande_produit_` FOREIGN KEY (`idprod`) REFERENCES `produit_` (`idproduit`)
)
COLLATE='utf8mb4_0900_ai_ci'
ENGINE=InnoDB
AUTO_INCREMENT=7
; |
Le trigger que j'ai créée est :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
create trigger stockage
after insert on commande
for each row
Begin
select commande.quantiteComm , produit_.quantite
from commande, produit_
where (commande.idcommande= LAST_INSERT_ID() and produit_.idproduit= commande.idprod) ;
if (produit_.quantite>commande.quantiteComm) then
update on produit_ p
set p.quantite = p.quantite -commande.quantiteComm
where commande.idcommande= LAST_INSERT_ID() and p.idproduit = commande.idprod;
end if;
END;
/ |
Je souhaite de l'aide de quelq'un ? Un grand merci d'avance pour les aide futur.