diff --git a/MySQL/LFitze/1-Erweiterung DDL + DML-LFI.txt b/MySQL/LFitze/1-Erweiterung DDL + DML-LFI.txt new file mode 100644 index 0000000..7b3b81e --- /dev/null +++ b/MySQL/LFitze/1-Erweiterung DDL + DML-LFI.txt @@ -0,0 +1,17 @@ +//Erweiterung Abrechnung +ALTER TABLE abrechnung + ADD date TIMESTAMP DEFAULT NOW(); + +ALTER TABLE abrechnung + ADD isFakturiert bool DEFAULT false; + + +//neue Tabelle Rechnung_kunde +CREATE TABLE rechnung_kunde +( + rgId INT AUTO_INCREMENT PRIMARY KEY, + fkAbrechnung INT UNSIGNED , + FOREIGN KEY (fkAbrechnung) REFERENCES abrechnung(abrechnung_id) ON DELETE CASCADE +); + + \ No newline at end of file diff --git a/MySQL/LFitze/2-SoreProcedur-PodBill-und-Triggerl-LFI.txt b/MySQL/LFitze/2-SoreProcedur-PodBill-und-Triggerl-LFI.txt new file mode 100644 index 0000000..dfaec07 --- /dev/null +++ b/MySQL/LFitze/2-SoreProcedur-PodBill-und-Triggerl-LFI.txt @@ -0,0 +1,40 @@ +//Store Procedure PodBill + +DELIMITER // +CREATE PROCEDURE PodBill +( + _abrechnungsId INT +) + +BEGIN + + IF EXISTS(SELECT isFakturiert FROM abrechnung WHERE abrechnung_id = _abrechnungsId AND isFakturiert = 1) THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'Fehler: Bestellung bereits verrechnet'; + END IF; + + INSERT INTO rechnung_kunde(fkAbrechnung) + VALUE(_abrechnungsId); + +END // +DELIMITER ; + + +//trigger wo 0 zu 1 macht! + +DELIMITER // +CREATE TRIGGER tr_createRgKunde_after + AFTER INSERT + ON rechnung_kunde + FOR EACH ROW + +BEGIN + UPDATE abrechnung + SET isFakturiert = 1 + WHERE abrechnung_id = new.fkAbrechnung + ; + +END // +DELIMITER ; + + \ No newline at end of file diff --git a/MySQL/LFitze/3-StoreProcedur-CheckHasBill-und-Event-LFI.txt b/MySQL/LFitze/3-StoreProcedur-CheckHasBill-und-Event-LFI.txt new file mode 100644 index 0000000..7a2640e --- /dev/null +++ b/MySQL/LFitze/3-StoreProcedur-CheckHasBill-und-Event-LFI.txt @@ -0,0 +1,42 @@ +DELIMITER // +CREATE EVENT IF NOT EXISTS create_rg_kunde + ON SCHEDULE EVERY DAY + + DO + CALL CheckHasBillProcedure(); + ; +DELIMITER ; + + +DELIMITER // +CREATE PROCEDURE CheckHasBillProcedure +( + +) + +BEGIN + DECLARE currentID INT; + DECLARE hasNext BOOL DEFAULT TRUE; + + DECLARE myRows CURSOR FOR + SELECT abrechnung_id From abrechnung + WHERE isFakturiert = 0 + GROUP BY kundenkonto_fk, date + HAVING SUM(betrag) > 100 OR (datediff(now(), abrechnung.date) > 90); + + DECLARE CONTINUE HANDLER FOR NOT FOUND + SET hasNext = false; + + OPEN myRows; + + processingLoop : LOOP + FETCH myRows INTO currentId; + IF hasNExt = FALSE THEN + LEAVE processingLoop; + END IF; + CALL PodBill(currentId); + + END LOOP; + +END // +DELIMITER ; \ No newline at end of file