From 356d24ff7743e0fa41a29c4c49b762c0be171fea Mon Sep 17 00:00:00 2001 From: raban Date: Fri, 6 Dec 2024 15:10:44 +0100 Subject: [PATCH] TP2++ --- TP2.sql | 80 ++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 56 insertions(+), 24 deletions(-) diff --git a/TP2.sql b/TP2.sql index d8d364a..4cc67b7 100644 --- a/TP2.sql +++ b/TP2.sql @@ -72,9 +72,35 @@ BEGIN INSERT INTO Client_His (id, prenom, nom, email, ville, date_suppression) VALUES (:OLD.id, :OLD.prenom, :OLD.nom, :OLD.email, :OLD.ville, SYSDATE); - DELETE FROM Commande WHERE client_id = :OLD.id; + DELETE FROM Commande3 WHERE client_id = :OLD.id; - DELETE FROM ligne_commande WHERE commande_id IN (SELECT id FROM Commande WHERE client_id = :OLD.id); + DELETE FROM ligne_commande WHERE commande_id IN (SELECT id FROM Commande3 WHERE client_id = :OLD.id); +END; + +//Exo 5 : +//Exo 2 + +//1/ +BEGIN TRANSACTION; + +DELETE FROM ligne_commande WHERE commande_id = :commande_id; + +DELETE FROM Commande3 WHERE id = :commande_id; + +COMMIT; + +//2/ +ALTER TABLE ligne_commande +ADD CONSTRAINT fk_ligne_commande_commande +FOREIGN KEY (commande_id) +REFERENCES Commande3(id) +ON DELETE CASCADE; + +//3/ +CREATE PROCEDURE SupprimerCommande (p_commande_id INT) AS +BEGIN + DELETE FROM ligne_commande WHERE commande_id = p_commande_id; + DELETE FROM Commande3 WHERE id = p_commande_id; END; @@ -119,7 +145,7 @@ CREATE TABLE ligne_commande ( produit_id INT, quantite INT, prix_total DECIMAL(10,2), - FOREIGN KEY (commande_id) REFERENCES Commande(id) ON DELETE CASCADE + FOREIGN KEY (commande_id) REFERENCES Commande3(id) ON DELETE CASCADE ); @@ -127,12 +153,12 @@ CREATE TABLE ligne_commande ( CREATE TABLE CommandeAuditLog ( Utilisateur VARCHAR(50), - ActionSQL VARCHAR(3), -- 'INS', 'DEL', ou 'UPD' + ActionSQL VARCHAR(3), DateMAJ DATE DEFAULT SYSDATE, ActCol VARCHAR(255) ); -CREATE TRIGGER trg_AuditInsertCommande +CREATE OR REPLACE TRIGGER trg_AuditInsertCommande AFTER INSERT ON Commande FOR EACH ROW BEGIN @@ -140,7 +166,7 @@ BEGIN VALUES (USER, 'INS', SYSDATE, 'N/A'); END; -CREATE TRIGGER trg_AuditDeleteCommande +CREATE OR REPLACE TRIGGER trg_AuditDeleteCommande AFTER DELETE ON Commande FOR EACH ROW BEGIN @@ -148,24 +174,30 @@ BEGIN VALUES (USER, 'DEL', SYSDATE, 'N/A'); END; -CREATE TRIGGER trg_AuditUpdateCommande -AFTER UPDATE ON Commande +CREATE OR REPLACE TRIGGER trg_AfterUpdateCommande3 +AFTER UPDATE ON Commande3 FOR EACH ROW BEGIN - DECLARE modified_columns VARCHAR(255); - - SET modified_columns = ''; - - IF :OLD.date_achat != :NEW.date_achat THEN - SET modified_columns = CONCAT(modified_columns, 'date_achat, '); - END IF; - - IF :OLD.reference != :NEW.reference THEN - SET modified_columns = CONCAT(modified_columns, 'reference, '); - END IF; - - SET modified_columns = RTRIM(modified_columns, ', '); - - INSERT INTO CommandeAuditLog (Utilisateur, ActionSQL, DateMAJ, ActCol) - VALUES (USER, 'UPD', SYSDATE, modified_columns); + + DECLARE + modified_columns VARCHAR2(255); + BEGIN + + modified_columns := ''; + + IF :OLD.date_achat != :NEW.date_achat THEN + modified_columns := modified_columns || 'date_achat, '; + END IF; + + IF :OLD.reference != :NEW.reference THEN + modified_columns := modified_columns || 'reference, '; + END IF; + + modified_columns := RTRIM(modified_columns, ', '); + + INSERT INTO CommandeAuditLog (Utilisateur, ActionSQL, DateMAJ, ActCol) + VALUES (USER, 'UPD', SYSDATE, modified_columns); + END; END; + +