BD2024_2025/TP2.sql
2024-12-06 15:10:44 +01:00

204 lines
4.4 KiB
SQL

//Exo 1 :
CREATE OR REPLACE FUNCTION fn_points_fidelite(client_id INT)
RETURN INT
IS va_total_point INT;
BEGIN
SELECT (SUM(prix_total)/10) INTO va_total_point
FROM ligne_commande lc JOIN commande3 c ON c.id = lc.commande_id
WHERE
c.client_id = client_id;
RETURN ROUND(va_total_point);
EXCEPTION
WHEN NO_DATA_FOUND
THEN DBMS_OUTPUT.PUT_LINE('Aucune commande');
END;
DECLARE CURSOR c_points_fidelite IS
SELECT nom, id
FROM client
WHERE client.client_id = :client_id;
v_nom client.nom%TYPE;
v_id client.id%TYPE;
BEGIN
OPEN c_points_fidelite;
LOOP
FETCH c_points_fidelite INTO v_nom, v_id;
DBMS_OUTPUT.PUTLINE((v_nom||"a"||fn_point_fidelite(v_id)||"points de fidélité"),
EXIT WHEN c_points_fidelite % NOT FOUND;
END LOOP;
CLOSE c_points_fidelite;
END;
/
//Exo 2 :
CREATE OR REPLACE TRIGGER trg_DeleteCommande3
BEFORE DELETE
ON Commande3
FOR EACH ROW
BEGIN
DELETE FROM ligne_commande
WHERE commande_id = :OLD.id;
END;
//Exo 3 :
CREATE OR REPLACE TRIGGER trg_DeleteClient
BEFORE DELETE ON client
FOR EACH ROW
BEGIN
DELETE FROM Commande3
WHERE client_id = :OLD.id;
DELETE FROM ligne_commande
WHERE commande_id IN (SELECT id FROM Commande3 WHERE client_id = :OLD.id);
END;
//Exo 4 :
CREATE TABLE Client_His (
id INT PRIMARY KEY,
prenom VARCHAR(50),
nom VARCHAR(50),
email VARCHAR(100),
ville VARCHAR(50),
date_suppression DATE DEFAULT SYSDATE
);
CREATE OR REPLACE TRIGGER trg_DeleteClient
BEFORE DELETE ON client
FOR EACH ROW
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 Commande3 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;
//Exo 6 :
DROP TABLE ligne_commande;
DROP TABLE Commande3;
DROP TABLE Client;
DROP TABLE Client_His;
CREATE TABLE Client (
id INT PRIMARY KEY,
prenom VARCHAR(50),
nom VARCHAR(50),
email VARCHAR(100),
ville VARCHAR(50)
);
CREATE TABLE Client_His (
id INT PRIMARY KEY,
prenom VARCHAR(50),
nom VARCHAR(50),
email VARCHAR(100),
ville VARCHAR(50),
date_suppression DATE DEFAULT SYSDATE
);
CREATE TABLE Commande3 (
id INT PRIMARY KEY,
client_id INT,
date_achat DATE,
reference VARCHAR(50),
FOREIGN KEY (client_id) REFERENCES Client(id) ON DELETE CASCADE
);
CREATE TABLE ligne_commande (
id INT PRIMARY KEY,
commande_id INT,
produit_id INT,
quantite INT,
prix_total DECIMAL(10,2),
FOREIGN KEY (commande_id) REFERENCES Commande3(id) ON DELETE CASCADE
);
//Exo 7 :
CREATE TABLE CommandeAuditLog (
Utilisateur VARCHAR(50),
ActionSQL VARCHAR(3),
DateMAJ DATE DEFAULT SYSDATE,
ActCol VARCHAR(255)
);
CREATE OR REPLACE TRIGGER trg_AuditInsertCommande
AFTER INSERT ON Commande
FOR EACH ROW
BEGIN
INSERT INTO CommandeAuditLog (Utilisateur, ActionSQL, DateMAJ, ActCol)
VALUES (USER, 'INS', SYSDATE, 'N/A');
END;
CREATE OR REPLACE TRIGGER trg_AuditDeleteCommande
AFTER DELETE ON Commande
FOR EACH ROW
BEGIN
INSERT INTO CommandeAuditLog (Utilisateur, ActionSQL, DateMAJ, ActCol)
VALUES (USER, 'DEL', SYSDATE, 'N/A');
END;
CREATE OR REPLACE TRIGGER trg_AfterUpdateCommande3
AFTER UPDATE ON Commande3
FOR EACH ROW
BEGIN
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;