-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrigger.sql
322 lines (265 loc) · 8.83 KB
/
Trigger.sql
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*Trigger :a trigger is a stored program invoked automatically in response to an event such as insert, update, or delete that occurs in the associated table. For example, you can define a trigger that is invoked automatically
before a new row is inserted into a table.
2 types of trigger are invoked
MySQL supports only row-level triggers. It doesn’t support statement-level triggers.
*/
--Create Trigger
/*CREATE TRIGGER trigger_name
{BEFORE | AFTER} {INSERT | UPDATE| DELETE }
ON table_name FOR EACH ROW
trigger_body*/
use campus_prepartion;
CREATE TABLE employees_audit (
id INT AUTO_INCREMENT PRIMARY KEY,
employeeNumber INT NOT NULL,
lastname VARCHAR(50) NOT NULL,
changedat DATETIME DEFAULT NULL,
action VARCHAR(50) DEFAULT NULL
);
--------------
CREATE TRIGGER before_employee_update
BEFORE UPDATE ON customers
FOR EACH ROW
INSERT INTO employees_audit
SET action = 'update',
id = OLD.branchid,
lastname = OLD.lastname,
changedat = NOW();
---------
SHOW TRIGGERS;
select * from customers
UPDATE customers
SET
lastName = 'Phan'
WHERE
branchid =7;
=======================================================================================================================
CREATE TABLE billings (
billingNo INT AUTO_INCREMENT,
customerNo INT,
billingDate DATE,
amount DEC(10 , 2 ),
PRIMARY KEY (billingNo)
);
DELIMITER $$
CREATE TRIGGER before_billing_update
BEFORE UPDATE
ON billings FOR EACH ROW
BEGIN
IF new.amount > old.amount * 10 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'New amount cannot be 10 times greater than the current amount.';
END IF;
END$$
DELIMITER ;
-----------
SELECT * FROM billings
SHOW TRIGGERS;
--- Drop Triggers
DROP TRIGGER before_employee_update;
-- MySQL BEFORE INSERT Trigger
CREATE TABLE WorkCenters (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
capacity INT NOT NULL
);
CREATE TABLE WorkCenterStats(
totalCapacity INT NOT NULL
);
-----------------------------------------------------
DELIMITER $$
CREATE TRIGGER before_workcenters_insert
BEFORE INSERT
ON WorkCenters FOR EACH ROW
BEGIN
DECLARE rowcount INT;
SELECT COUNT(*)
INTO rowcount
FROM WorkCenterStats;
IF rowcount > 0 THEN
UPDATE WorkCenterStats
SET totalCapacity = totalCapacity + new.capacity;
ELSE
INSERT INTO WorkCenterStats(totalCapacity)
VALUES(new.capacity);
END IF;
END $$
DELIMITER ;
-- Testing Trigger
INSERT INTO WorkCenters(name, capacity)
VALUES('Mold Machine',100);
------------------------
SELECT * FROM WorkCenterStats;
INSERT INTO WorkCenters(name, capacity)
VALUES('Packing',200);
SELECT * FROM WorkCenterStats; -- trigger action have done total ans 300
------------------- After Trigger
DROP TABLE IF EXISTS members;
CREATE TABLE members (
id INT AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
email VARCHAR(255),
birthDate DATE,
PRIMARY KEY (id)
);
DROP TABLE IF EXISTS reminders;
CREATE TABLE reminders (
id INT AUTO_INCREMENT,
memberId INT,
message VARCHAR(255) NOT NULL,
PRIMARY KEY (id , memberId)
);
---------------------------------------
DELIMITER $$
CREATE TRIGGER after_members_insert
AFTER INSERT
ON members FOR EACH ROW
BEGIN
IF NEW.birthDate IS NULL THEN
INSERT INTO reminders(memberId, message)
VALUES(new.id,CONCAT('Hi ', NEW.name, ', please update your date of birth.'));
END IF;
END$$
DELIMITER ;
-----------------------------------------------------------------------------------------------------------
INSERT INTO members(name, email, birthDate)
VALUES
('John Doe', 'john.doe@example.com', NULL),
('Jane Doe', 'jane.doe@example.com','2000-01-01');
------------------------------------------------------------------------
SELECT * FROM reminders;
-- MySQL BEFORE UPDATE Trigger
DROP TABLE IF EXISTS sales;
CREATE TABLE sales (
id INT AUTO_INCREMENT,
product VARCHAR(100) NOT NULL,
quantity INT NOT NULL DEFAULT 0,
fiscalYear SMALLINT NOT NULL,
fiscalMonth TINYINT NOT NULL,
CHECK(fiscalMonth >= 1 AND fiscalMonth <= 12),
CHECK(fiscalYear BETWEEN 2000 and 2050),
CHECK (quantity >=0),
UNIQUE(product, fiscalYear, fiscalMonth),
PRIMARY KEY(id)
);
INSERT INTO sales(product, quantity, fiscalYear, fiscalMonth)
VALUES
('2003 Harley-Davidson Eagle Drag Bike',120, 2020,1),
('1969 Corvair Monza', 150,2020,1),
('1970 Plymouth Hemi Cuda', 200,2020,1);
=======================================================================================================================
DELIMITER $$
CREATE TRIGGER before_sales_update
BEFORE UPDATE
ON sales FOR EACH ROW
BEGIN
DECLARE errorMessage VARCHAR(255);
SET errorMessage = CONCAT('The new quantity ',
NEW.quantity,
' cannot be 3 times greater than the current quantity ',
OLD.quantity);
IF new.quantity > old.quantity * 3 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = errorMessage;
END IF;
END $$
DELIMITER ;
============================================================================================================================
UPDATE sales
SET quantity = 150
WHERE id = 1;
Select * from sales;
SHOW ERRORS;
-- MySQL BEFORE DELETE triggers
DROP TABLE IF EXISTS Salaries;
CREATE TABLE Salaries (
employeeNumber INT PRIMARY KEY,
validFrom DATE NOT NULL,
amount DEC(12 , 2 ) NOT NULL DEFAULT 0
);
INSERT INTO salaries(employeeNumber,validFrom,amount)
VALUES
(1002,'2000-01-01',50000),
(1056,'2000-01-01',60000),
(1076,'2000-01-01',70000);
==============================================================================================================
DROP TABLE IF EXISTS SalaryArchives;
CREATE TABLE SalaryArchives (
id INT PRIMARY KEY AUTO_INCREMENT,
employeeNumber INT ,
validFrom DATE NOT NULL,
amount DEC(12 , 2 ) NOT NULL DEFAULT 0,
deletedAt TIMESTAMP DEFAULT NOW()
);
================== Trigger
DELIMITER $$
CREATE TRIGGER before_salaries_delete
BEFORE DELETE
ON salaries FOR EACH ROW
BEGIN
INSERT INTO SalaryArchives(employeeNumber,validFrom,amount)
VALUES(OLD.employeeNumber,OLD.validFrom,OLD.amount);
END$$
DELIMITER ;
-------------------------------------
select * from salaries;
select * from salaryarchives;
--------------------------------------------------------------------------------
DELETE FROM salaries
WHERE employeeNumber = 1002;
-----------------------------------------------------------------------------------------------------------------------------
select * from salaryarchives;
----------------------------------------After Delete
CREATE TRIGGER after_salaries_delete
AFTER DELETE
ON Salaries FOR EACH ROW
UPDATE SalaryBudgets
SET total = total - old.salary;
---------------------------------------- CREATE MULTIPLE TRIGGERS -----------------------------------------------------------
DELIMITER $$
CREATE TRIGGER trigger_name
{BEFORE|AFTER}{INSERT|UPDATE|DELETE}
ON table_name FOR EACH ROW
{FOLLOWS|PRECEDES} existing_trigger_name
BEGIN
-- statements
END$$
DELIMITER ;
----------------------MySQL Scheduled Event
/*MySQL Events can be very useful in many cases such as optimizing database tables, cleaning up logs, archiving data, or generating complex reports during off-peak time.
MySQL event scheduler configuration
MySQL uses a special thread called event scheduler thread to execute
all scheduled events. You can view the status of the event scheduler thread by executing the SHOW PROCESSLIST command:*/
SHOW PROCESSLIST;
SET GLOBAL event_scheduler = ON;
===================================================================================================
use campus_prepartion;
CREATE TABLE messages (
id INT PRIMARY KEY AUTO_INCREMENT,
message VARCHAR(255) NOT NULL,
created_at DATETIME NOT NULL
);
CREATE EVENT IF NOT EXISTS test_event_01
ON SCHEDULE AT CURRENT_TIMESTAMP
DO
INSERT INTO messages(message,created_at)
VALUES('Test MySQL Event 1',NOW());
SELECT * FROM messages;
SHOW EVENTS FROM campus_prepartion
---Modifying events trigger
CREATE EVENT test_event_04
ON SCHEDULE EVERY 1 MINUTE
DO
INSERT INTO messages(message,created_at)
VALUES('Test ALTER EVENT statement',NOW());
ALTER EVENT test_event_04
ON SCHEDULE EVERY 2 MINUTE;
ALTER EVENT test_event_04
DO
INSERT INTO messages(message,created_at)
VALUES('Message from event',NOW());
--Disable event
ALTER EVENT test_event_04
DISABLE;
---Drop Event
DROP EVENT [IF EXIST] event_name;