-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab6_triggers.sql
More file actions
53 lines (45 loc) · 1.51 KB
/
lab6_triggers.sql
File metadata and controls
53 lines (45 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
-- TRIGGERS
DELIMITER //
CREATE OR REPLACE TRIGGER triggerWithHonours
BEFORE INSERT ON Grades
FOR EACH ROW
BEGIN
IF (new.withHonours = 1 AND new.value <9.0 ) THEN
SIGNAL SQLSTATE '45000' SET message_text =
'you cannot insert a grade with honours whose value is less then 9';
end if;
end //
DELIMITER ;
DELIMITER //
CREATE OR REPLACE TRIGGER triggerGradeStudentGroup
BEFORE INSERT ON Grades
FOR EACH ROW
BEGIN
DECLARE isInGroup INT;
SET isInGroup = (SELECT COUNT(*)
FROM GroupsStudents WHERE studentId = new.studentId
AND groupId = NEW.groupId);
IF (isInGroup < 1 ) THEN
SIGNAL SQLSTATE '45000' SET message_text =
'A student cannt have grades for groups in which they are not registered';
end if;
end //
DELIMITER ;
/*
DELIMITER //
CREATE OR REPLACE TRIGGER triggerGradesChangeDifference
BEFORE UPDATE ON Grades
FOR EACH ROW
BEGIN
DECLARE difference DECIMAL(4,2) ;
DECLARE student ROW TYPE OF Students ;
SET difference = new.value - OLD.value;
IF (difference > 4 ) THEN
SELECT * INTO student FROM Students WHERE studentId = new.studentId;
SET @error_message = CONCAT('Y cant add', difference,
'points to a grade for the student', student.firstName, ' ', student.surname);
SIGNAL SQLSTATE '45000' SET message_text = @error_message;
end if;
end //
DELIMITER ;
*/