TP2+
This commit is contained in:
parent
cec8402c39
commit
bc20e779a3
171
TP2.sql
Normal file
171
TP2.sql
Normal file
@ -0,0 +1,171 @@
|
||||
//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 Commande WHERE client_id = :OLD.id;
|
||||
|
||||
DELETE FROM ligne_commande WHERE commande_id IN (SELECT id FROM Commande WHERE client_id = :OLD.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 Commande(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
|
||||
//Exo 7 :
|
||||
|
||||
CREATE TABLE CommandeAuditLog (
|
||||
Utilisateur VARCHAR(50),
|
||||
ActionSQL VARCHAR(3), -- 'INS', 'DEL', ou 'UPD'
|
||||
DateMAJ DATE DEFAULT SYSDATE,
|
||||
ActCol VARCHAR(255)
|
||||
);
|
||||
|
||||
CREATE 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 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 TRIGGER trg_AuditUpdateCommande
|
||||
AFTER UPDATE ON Commande
|
||||
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);
|
||||
END;
|
Loading…
Reference in New Issue
Block a user