MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
triggers_08.inc
1 #======================================================================
2 #
3 # Trigger Tests
4 # (test case numbering refer to requirement document TP v1.1)
5 #======================================================================
6 # WL#4084: enable disabled parts, 2007-11-15 hhunger
7 
8 USE test;
9 --source suite/funcs_1/include/tb3.inc
10 
11 
12 
13 # General setup for Trigger tests
14 let $message= Testcase: 3.5:;
15 --source include/show_msg.inc
16 
17 --disable_abort_on_error
18 
19  create User test_general@localhost;
20  set password for test_general@localhost = password('PWD');
21  revoke ALL PRIVILEGES, GRANT OPTION FROM test_general@localhost;
22 
23  create User test_super@localhost;
24  set password for test_super@localhost = password('PWD');
25  grant ALL on *.* to test_super@localhost with grant OPTION;
26  connect (con2_general,localhost,test_general,PWD,test,$MASTER_MYPORT,$MASTER_MYSOCK);
27  connect (con2_super,localhost,test_super,PWD,test,$MASTER_MYPORT,$MASTER_MYSOCK);
28  connection default;
29 
30 #################################
31 ####### Section 3.5.8 ###########
32 # Checks on Triggered Actions #
33 #################################
34 
35 #Section 3.5.8.1
36 # Testcase: Ensure that the triggered action of every trigger always executes
37 # correctly and the results in all expected changes made to the database
38 let $message= Testcase 3.5.8.1: (implied in previous tests);
39 --source include/show_msg.inc
40 
41 #Section 3.5.8.2
42 # Testcase: Ensure that the triggered actions of every trigger never results
43 # in an unexpected change made to the database.
44 let $message= Testcase 3.5.8.2: (implied in previous tests);
45 --source include/show_msg.inc
46 
47 
48 #Section 3.5.8.3 / 3.5.8.4
49 #Test case: Ensure that the triggered action can any valid SQL statement / set
50 # of valid SQL statements, provided the statements are written within
51 # a BEGIN/END compound statement construct
52 # OBN - At this point the tests focuses on the the INSERT/UPDATE/DELETE SQL statements
53 # as there are the most likely to be used in triggers
54 let $message= Testcase 3.5.8.3/4:;
55 --source include/show_msg.inc
56 
57 # creating test tables to perform the trigger SQL on
58  connection con2_super;
59  create database db_test;
60  grant SELECT, INSERT, UPDATE, DELETE on db_test.* to test_general;
61  grant LOCK TABLES on db_test.* to test_general;
62  Use db_test;
63  --replace_result $engine_type <engine_to_be_used>
64  eval create table t1_i (
65  i120 char ascii not null DEFAULT b'101',
66  i136 smallint zerofill not null DEFAULT 999,
67  i144 int zerofill not null DEFAULT 99999,
68  i163 decimal (63,30)) engine=$engine_type;
69  --replace_result $engine_type <engine_to_be_used>
70  eval create table t1_u (
71  u120 char ascii not null DEFAULT b'101',
72  u136 smallint zerofill not null DEFAULT 999,
73  u144 int zerofill not null DEFAULT 99999,
74  u163 decimal (63,30)) engine=$engine_type;
75  --replace_result $engine_type <engine_to_be_used>
76  eval create table t1_d (
77  d120 char ascii not null DEFAULT b'101',
78  d136 smallint zerofill not null DEFAULT 999,
79  d144 int zerofill not null DEFAULT 99999,
80  d163 decimal (63,30)) engine=$engine_type;
81  Insert into t1_u values ('a',111,99999,999.99);
82  Insert into t1_u values ('b',222,99999,999.99);
83  Insert into t1_u values ('c',333,99999,999.99);
84  Insert into t1_u values ('d',222,99999,999.99);
85  Insert into t1_u values ('e',222,99999,999.99);
86  Insert into t1_u values ('f',333,99999,999.99);
87  Insert into t1_d values ('a',111,99999,999.99);
88  Insert into t1_d values ('b',222,99999,999.99);
89  Insert into t1_d values ('c',333,99999,999.99);
90  Insert into t1_d values ('d',444,99999,999.99);
91  Insert into t1_d values ('e',222,99999,999.99);
92  Insert into t1_d values ('f',222,99999,999.99);
93 
94 let $message= 3.5.8.4 - multiple SQL;
95 --source include/show_msg.inc
96 # Trigger definition - multiple SQL
97  use test;
98  delimiter //;
99  Create trigger trg1 AFTER INSERT on tb3 for each row
100  BEGIN
101  insert into db_test.t1_i
102  values (new.f120, new.f136, new.f144, new.f163);
103  update db_test.t1_u
104  set u144=new.f144, u163=new.f163
105  where u136=new.f136;
106  delete from db_test.t1_d where d136= new.f136;
107  select sum(db_test.t1_u.u163) into @test_var from db_test.t1_u
108  where u136= new.f136;
109  END//
110  delimiter ;//
111 
112 # Test trigger execution - multiple SQL
113  connection con2_general;
114  Use test;
115  set @test_var=0;
116  Insert into tb3 (f120, f122, f136, f144, f163)
117  values ('1', 'Test 3.5.8.4', 222, 23456, 1.05);
118  Select f120, f122, f136, f144, f163 from tb3 where f122= 'Test 3.5.8.4';
119  --sorted_result
120  select * from db_test.t1_i;
121  --sorted_result
122  select * from db_test.t1_u;
123  --sorted_result
124  select * from db_test.t1_d;
125  select @test_var;
126 
127 
128 let $message= 3.5.8.4 - single SQL - insert;
129 --source include/show_msg.inc
130 # Trigger definition - single SQL Insert
131  connection con2_super;
132  delimiter //;
133  Create trigger trg2 BEFORE UPDATE on tb3 for each row
134  BEGIN
135  insert into db_test.t1_i
136  values (new.f120, new.f136, new.f144, new.f163);
137  END//
138  delimiter ;//
139 
140 # Trigger exeution - single SQL Insert
141  connection con2_general;
142  Select f120, f122, f136, f144, f163 from tb3 where f122 like 'Test 3.5.8.4%';
143  select * from db_test.t1_i order by i120;
144  update tb3 set f120='I', f122='Test 3.5.8.4-Single Insert'
145  where f122='Test 3.5.8.4';
146  Select f120, f122, f136, f144, f163 from tb3 where f122 like 'Test 3.5.8.4%';
147  select * from db_test.t1_i order by i120;
148 
149 
150 let $message= 3.5.8.4 - single SQL - update;
151 --source include/show_msg.inc
152 # Trigger definition - single SQL update
153  connection con2_super;
154  drop trigger trg2;
155  Create trigger trg3 BEFORE UPDATE on tb3 for each row
156  update db_test.t1_u
157  set u120=new.f120
158  where u136=new.f136;
159 
160 # Trigger exeution - single SQL - update;
161  connection con2_general;
162  update tb3 set f120='U', f122='Test 3.5.8.4-Single Update'
163  where f122='Test 3.5.8.4-Single Insert';
164  Select f120, f122, f136, f144, f163 from tb3 where f122 like 'Test 3.5.8.4%';
165  select * from db_test.t1_u order by u120;
166 
167 
168 let $message= 3.5.8.3/4 - single SQL - delete;
169 --source include/show_msg.inc
170 # Trigger definition - single SQL delete
171  connection con2_super;
172  drop trigger trg3;
173  Create trigger trg4 AFTER UPDATE on tb3 for each row
174  delete from db_test.t1_d where d136= new.f136;
175 
176 # Trigger exeution - single SQL delete
177  connection con2_general;
178 #lock tables tb3 write, db_test.t1_i write, db_test.t1_u write, db_test.t1_d write;
179  update tb3 set f120='D', f136=444,
180  f122='Test 3.5.8.4-Single Delete'
181  where f122='Test 3.5.8.4-Single Update';
182 #unlock tables;
183  Select f120, f122, f136, f144, f163 from tb3 where f122 like 'Test 3.5.8.4%';
184  select * from db_test.t1_d order by d120;
185 
186 
187 let $message= 3.5.8.3/4 - single SQL - select;
188 --source include/show_msg.inc
189 # Trigger definition - single SQL select
190  connection con2_super;
191  drop trigger trg4;
192  Create trigger trg5 AFTER UPDATE on tb3 for each row
193  select sum(db_test.t1_u.u163) into @test_var from db_test.t1_u
194  where u136= new.f136;
195 
196 # Trigger exeution - single SQL select
197  connection con2_general;
198  set @test_var=0;
199  update tb3 set f120='S', f136=111,
200  f122='Test 3.5.8.4-Single Select'
201  where f122='Test 3.5.8.4-Single Delete';
202  Select f120, f122, f136, f144, f163 from tb3 where f122 like 'Test 3.5.8.4%';
203  select @test_var;
204 
205 #Cleanup
206  connection default;
207  --disable_warnings
208  drop trigger trg1;
209  drop trigger trg5;
210  drop database if exists db_test;
211  delete from tb3 where f122 like 'Test 3.5.8.4%';
212  revoke ALL PRIVILEGES, GRANT OPTION FROM 'test_general'@'localhost';
213  --enable_warnings
214 
215 
216 #Section 3.5.8.5 (IF)
217 # Test case: Ensure that the stored procedure-specific flow control statement like IF
218 # works correctly when it is a part of the triggered action portion of a
219 # trigger definition.
220 let $message= Testcase 3.5.8.5 (IF):;
221 --source include/show_msg.inc
222 
223  delimiter //;
224  create trigger trg2 before insert on tb3 for each row
225  BEGIN
226  IF new.f120='1' then
227  set @test_var='one', new.f120='2';
228  ELSEIF new.f120='2' then
229  set @test_var='two', new.f120='3';
230  ELSEIF new.f120='3' then
231  set @test_var='three', new.f120='4';
232  END IF;
233 
234  IF (new.f120='4') and (new.f136=10) then
235  set @test_var2='2nd if', new.f120='d';
236  ELSE
237  set @test_var2='2nd else', new.f120='D';
238  END IF;
239  END//
240  delimiter ;//
241 
242  set @test_var='Empty', @test_var2=0;
243  Insert into tb3 (f120, f122, f136) values ('1', 'Test 3.5.8.5-if', 101);
244  select f120, f122, f136, @test_var, @test_var2
245  from tb3 where f122 = 'Test 3.5.8.5-if' order by f136;
246  Insert into tb3 (f120, f122, f136) values ('2', 'Test 3.5.8.5-if', 102);
247  select f120, f122, f136, @test_var, @test_var2
248  from tb3 where f122 = 'Test 3.5.8.5-if' order by f136;
249  Insert into tb3 (f120, f122, f136) values ('3', 'Test 3.5.8.5-if', 10);
250  select f120, f122, f136, @test_var, @test_var2
251  from tb3 where f122 = 'Test 3.5.8.5-if' order by f136;
252  Insert into tb3 (f120, f122, f136) values ('3', 'Test 3.5.8.5-if', 103);
253  select f120, f122, f136, @test_var, @test_var2
254  from tb3 where f122 = 'Test 3.5.8.5-if' order by f136;
255 
256  delimiter //;
257  --error ER_PARSE_ERROR
258  create trigger trg3 before update on tb3 for each row
259  BEGIN
260  ELSEIF new.f120='2' then
261  END IF;
262  END//
263  --error 0, ER_TRG_DOES_NOT_EXIST
264  drop trigger trg3//
265 
266  --error ER_PARSE_ERROR
267  create trigger trg4 before update on tb3 for each row
268  BEGIN
269  IF (new.f120='4') and (new.f136=10) then
270  set @test_var2='2nd if', new.f120='d';
271  ELSE
272  set @test_var2='2nd else', new.f120='D';
273  END//
274  delimiter ;//
275  --error 0, ER_TRG_DOES_NOT_EXIST
276  drop trigger trg4;
277 
278 #Cleanup
279  --disable_warnings
280  drop trigger trg2;
281  delete from tb3 where f121='Test 3.5.8.5-if';
282  --enable_warnings
283 
284 
285 #Section 3.5.8.5 (CASE)
286 # Test case: Ensure that the stored procedure-specific flow control statement
287 # like CASE works correctly when it is a part of the triggered action
288 # portion of a trigger definition.
289 let $message= Testcase 3.5.8.5-case:;
290 --source include/show_msg.inc
291 
292  delimiter //;
293  create trigger trg3 before insert on tb3 for each row
294  BEGIN
295  SET new.f120=char(ascii(new.f120)-32);
296  CASE
297  when new.f136<100 then set new.f136=new.f136+120;
298  when new.f136<10 then set new.f144=777;
299  when new.f136>100 then set new.f120=new.f136-1;
300  END case;
301  CASE
302  when new.f136=200 then set @test_var=CONCAT(new.f120, '=');
303  ELSE set @test_var=concat(new.f120, '*');
304  END case;
305  CASE new.f144
306  when 1 then set @test_var=concat(@test_var, 'one');
307  when 2 then set @test_var=concat(@test_var, 'two');
308  when 3 then set @test_var=concat(@test_var, 'three');
309  when 4 then set @test_var=concat(@test_var, 'four');
310  when 5 then set @test_var=concat(@test_var, 'five');
311  when 6 then set @test_var=concat(@test_var, 'six');
312  when 7 then set @test_var=concat(@test_var, 'seven');
313  when 8 then set @test_var=concat(@test_var, 'eight');
314  when 9 then set @test_var=concat(@test_var, 'nine');
315  when 10 then set @test_var=concat(@test_var, 'ten');
316  when 11 then set @test_var=concat(@test_var, 'eleven');
317  when 12 then set @test_var=concat(@test_var, 'twelve');
318  when 13 then set @test_var=concat(@test_var, 'thirteen');
319  when 14 then set @test_var=concat(@test_var, 'fourteen');
320  when 15 then set @test_var=concat(@test_var, 'fifteen');
321  ELSE set @test_var=CONCAT(new.f120, '*', new.f144);
322  END case;
323  END//
324  delimiter ;//
325 
326  set @test_var='Empty';
327  Insert into tb3 (f120, f122, f136, f144)
328  values ('a', 'Test 3.5.8.5-case', 5, 7);
329  select f120, f122, f136, f144, @test_var
330  from tb3 where f122 = 'Test 3.5.8.5-case' order by f120,f136;
331  Insert into tb3 (f120, f122, f136, f144)
332  values ('b', 'Test 3.5.8.5-case', 71,16);
333  select f120, f122, f136, f144, @test_var
334  from tb3 where f122 = 'Test 3.5.8.5-case' order by f120,f136;
335  Insert into tb3 (f120, f122, f136, f144)
336  values ('c', 'Test 3.5.8.5-case', 80,1);
337  select f120, f122, f136, f144, @test_var
338  from tb3 where f122 = 'Test 3.5.8.5-case' order by f120,f136;
339  Insert into tb3 (f120, f122, f136)
340  values ('d', 'Test 3.5.8.5-case', 152);
341  select f120, f122, f136, f144, @test_var
342  from tb3 where f122 = 'Test 3.5.8.5-case' order by f120,f136;
343  Insert into tb3 (f120, f122, f136, f144)
344  values ('e', 'Test 3.5.8.5-case', 200, 8);
345  select f120, f122, f136, f144, @test_var
346  from tb3 where f122 = 'Test 3.5.8.5-case' order by f120,f136;
347  --error 0, ER_SP_CASE_NOT_FOUND
348  Insert into tb3 (f120, f122, f136, f144)
349  values ('f', 'Test 3.5.8.5-case', 100, 8);
350  select f120, f122, f136, f144, @test_var
351  from tb3 where f122 = 'Test 3.5.8.5-case' order by f120,f136;
352 
353  delimiter //;
354  --error ER_PARSE_ERROR
355  create trigger trg3a before update on tb3 for each row
356  BEGIN
357  CASE
358  when new.f136<100 then set new.f120='p';
359  END//
360  delimiter ;//
361 
362  --error 0, ER_TRG_DOES_NOT_EXIST
363  drop trigger trg3a;
364 
365 #Cleanup
366  --disable_warnings
367  drop trigger trg3;
368  delete from tb3 where f121='Test 3.5.8.5-case';
369  --enable_warnings
370 
371 #Section 3.5.8.5 (LOOP)
372 # Test case: Ensure that the stored procedure-specific flow control
373 # statement like LOOP / LEAVE work correctly when they are
374 # part of the triggered action portion of a trigger definition.
375 let $message= Testcase 3.5.8.5-loop/leave:;
376 --source include/show_msg.inc
377 
378  delimiter //;
379  Create trigger trg4 after insert on tb3 for each row
380  BEGIN
381  set @counter=0, @flag='Initial';
382  Label1: loop
383  if new.f136<new.f144 then
384  set @counter='Nothing to loop';
385  leave Label1;
386  else
387  set @counter=@counter+1;
388  if new.f136=new.f144+@counter then
389  set @counter=concat(@counter, ' loops');
390  leave Label1;
391  end if;
392  end if;
393  iterate label1;
394  set @flag='Final';
395  END loop Label1;
396  END//
397  delimiter ;//
398  Insert into tb3 (f122, f136, f144)
399  values ('Test 3.5.8.5-loop', 2, 8);
400  select @counter, @flag;
401  Insert into tb3 (f122, f136, f144)
402  values ('Test 3.5.8.5-loop', 11, 8);
403  select @counter, @flag;
404 
405 
406  delimiter //;
407 
408  --error ER_PARSE_ERROR
409  Create trigger trg4_2 after update on tb3 for each row
410  BEGIN
411  Label1: loop
412  set @counter=@counter+1;
413  END;
414  END//
415  delimiter ;//
416  --error 0, ER_TRG_DOES_NOT_EXIST
417  drop trigger trg4_2;
418 
419 #Cleanup
420  --disable_warnings
421  drop trigger trg4;
422  delete from tb3 where f122='Test 3.5.8.5-loop';
423  --enable_warnings
424 
425 #Section 3.5.8.5 (REPEAT ITERATE)
426 #Testcase: Ensure that the stored procedure-specific flow control statements
427 # like REPEAT work correctly when they are part of the triggered action
428 # portion of a trigger definition.
429 let $message= Testcase 3.5.8.5-repeat:;
430 --source include/show_msg.inc
431 
432  delimiter //;
433  Create trigger trg6 after insert on tb3 for each row
434  BEGIN
435  rp_label: REPEAT
436  SET @counter1 = @counter1 + 1;
437  IF (@counter1 MOD 2 = 0) THEN ITERATE rp_label;
438  END IF;
439  SET @counter2 = @counter2 + 1;
440  UNTIL @counter1> new.f136 END REPEAT rp_label;
441  END//
442  delimiter ;//
443 
444  set @counter1= 0, @counter2= 0;
445  Insert into tb3 (f122, f136)
446  values ('Test 3.5.8.5-repeat', 13);
447  select @counter1, @counter2;
448 
449 
450  delimiter //;
451  --error ER_PARSE_ERROR
452  Create trigger trg6_2 after update on tb3 for each row
453  BEGIN
454  REPEAT
455  SET @counter2 = @counter2 + 1;
456  END//
457  delimiter ;//
458 
459 #Cleanup
460  --disable_warnings
461  drop trigger trg6;
462  delete from tb3 where f122='Test 3.5.8.5-repeat';
463  --enable_warnings
464 
465 
466 #Section 3.5.8.5 (WHILE)
467 # Test case: Ensure that the stored procedure-specific flow control
468 # statements WHILE, work correctly when they are part of
469 # the triggered action portion of a trigger definition.
470 let $message= Testcase 3.5.8.5-while:;
471 --source include/show_msg.inc
472 
473  delimiter //;
474  Create trigger trg7 after insert on tb3 for each row
475  wl_label: WHILE @counter1 < new.f136 DO
476  SET @counter1 = @counter1 + 1;
477  IF (@counter1 MOD 2 = 0) THEN ITERATE wl_label;
478  END IF;
479  SET @counter2 = @counter2 + 1;
480  END WHILE wl_label//
481  delimiter ;//
482 
483  set @counter1= 0, @counter2= 0;
484  Insert into tb3 (f122, f136)
485  values ('Test 3.5.8.5-while', 7);
486  select @counter1, @counter2;
487  delimiter //;
488  --error ER_PARSE_ERROR
489  Create trigger trg7_2 after update on tb3 for each row
490  BEGIN
491  WHILE @counter1 < new.f136
492  SET @counter1 = @counter1 + 1;
493  END//
494  delimiter ;//
495 
496 #Cleanup
497  --disable_warnings
498  delete from tb3 where f122='Test 3.5.8.5-while';
499  drop trigger trg7;
500  --enable_warnings
501 
502 #Section 3.5.8.6
503 # Test case: Ensure that a trigger definition that includes a CALL to a stored
504 # procedure fails, at CREATE TRIGGER time, with an appropriate error
505 # message. Not more valid requirement.
506 let $message= Testcase 3.5.8.6: (requirement void);
507 --source include/show_msg.inc
508  delimiter //;
509  CREATE PROCEDURE sp_01 () BEGIN set @v1=1; END//
510 
511  CREATE TRIGGER trg8_1 BEFORE UPDATE ON tb3 FOR EACH ROW
512  BEGIN
513  CALL sp_01 ();
514  END//
515  delimiter ;//
516  Insert into tb3 (f120, f122, f136) values ('6', 'Test 3.5.8.6-insert', 101);
517  update tb3 set f120='S', f136=111,
518  f122='Test 3.5.8.6-tr8_1'
519  where f122='Test 3.5.8.6-insert';
520  select f120, f122
521  from tb3 where f122 like 'Test 3.5.8.6%' order by f120;
522  DROP TRIGGER trg8_1;
523  DROP PROCEDURE sp_01;
524 
525 
526 #Section 3.5.8.7
527 # Test case: Ensure that a trigger definition that includes a
528 # transaction-delimiting statement (e.g. COMMIT,
529 # ROLLBACK, START TRANSACTION) fails, at CREATE TRIGGER
530 # time, with an appropriate error message.
531 let $message= Testcase 3.5.8.7;
532 --source include/show_msg.inc
533 
534  delimiter //;
535  --error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG
536  Create trigger trg9_1 before update on tb3 for each row
537  BEGIN
538  Start transaction;
539  Set new.f120='U';
540  Commit;
541  END//
542 
543  --error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG
544  Create trigger trg9_2 before delete on tb3 for each row
545  BEGIN
546  Start transaction;
547  Set @var2=old.f120;
548  Rollback;
549  END//
550  delimiter ;//
551 
552 
553 # Cleanup section 3.5
554  connection default;
555  drop user test_general@localhost;
556  drop user test_general;
557  drop user test_super@localhost;
558 
559 DROP TABLE test.tb3;