1# This test uses chmod, can't be run with root permissions
2-- source include/not_as_root.inc
3
4
5#
6# Basic triggers test
7#
8
9# Create additional connections used through test
10connect (addconroot1, localhost, root,,);
11connect (addconroot2, localhost, root,,);
12# Connection without current database set
13connect (addconwithoutdb, localhost, root,,*NO-ONE*);
14connection default;
15
16create table t1 (i int);
17
18# let us test some very simple trigger
19create trigger trg before insert on t1 for each row set @a:=1;
20set @a:=0;
21select @a;
22insert into t1 values (1);
23select @a;
24drop trigger trg;
25
26# let us test simple trigger reading some values
27create trigger trg before insert on t1 for each row set @a:=new.i;
28insert into t1 values (123);
29select @a;
30drop trigger trg;
31
32drop table t1;
33
34# Let us test before insert trigger
35# Such triggers can be used for setting complex default values
36create table t1 (i int not null, j int);
37delimiter |;
38create trigger trg before insert on t1 for each row
39begin
40  if isnull(new.j) then
41    set new.j:= new.i * 10;
42  end if;
43end|
44insert into t1 (i) values (1)|
45insert into t1 (i,j) values (2, 3)|
46select * from t1|
47drop trigger trg|
48drop table t1|
49delimiter ;|
50
51# After insert trigger
52# Useful for aggregating data
53create table t1 (i int not null primary key);
54create trigger trg after insert on t1 for each row
55  set @a:= if(@a,concat(@a, ":", new.i), new.i);
56set @a:="";
57insert into t1 values (2),(3),(4),(5);
58select @a;
59drop trigger trg;
60drop table t1;
61
62# Before update trigger
63# (In future we will achieve this via proper error handling in triggers)
64create table t1 (aid int not null primary key, balance int not null default 0);
65insert into t1 values (1, 1000), (2,3000);
66delimiter |;
67create trigger trg before update on t1 for each row
68begin
69  declare loc_err varchar(255);
70  if abs(new.balance - old.balance) > 1000 then
71    set new.balance:= old.balance;
72    set loc_err := concat("Too big change for aid = ", new.aid);
73    set @update_failed:= if(@update_failed, concat(@a, ":", loc_err), loc_err);
74  end if;
75end|
76set @update_failed:=""|
77update t1 set balance=1500|
78select @update_failed;
79select * from t1|
80drop trigger trg|
81drop table t1|
82delimiter ;|
83
84# After update trigger
85create table t1 (i int);
86insert into t1 values (1),(2),(3),(4);
87create trigger trg after update on t1 for each row
88  set @total_change:=@total_change + new.i - old.i;
89set @total_change:=0;
90update t1 set i=3;
91select @total_change;
92drop trigger trg;
93drop table t1;
94
95# Before delete trigger
96# This can be used for aggregation too :)
97create table t1 (i int);
98insert into t1 values (1),(2),(3),(4);
99create trigger trg before delete on t1 for each row
100  set @del_sum:= @del_sum + old.i;
101set @del_sum:= 0;
102delete from t1 where i <= 3;
103select @del_sum;
104drop trigger trg;
105drop table t1;
106
107# After delete trigger.
108# Just run out of imagination.
109create table t1 (i int);
110insert into t1 values (1),(2),(3),(4);
111create trigger trg after delete on t1 for each row set @del:= 1;
112set @del:= 0;
113delete from t1 where i <> 0;
114select @del;
115drop trigger trg;
116drop table t1;
117
118# Several triggers on one table
119create table t1 (i int, j int);
120
121delimiter |;
122create trigger trg1 before insert on t1 for each row
123begin
124  if new.j > 10 then
125    set new.j := 10;
126  end if;
127end|
128create trigger trg2 before update on t1 for each row
129begin
130  if old.i % 2 = 0 then
131    set new.j := -1;
132  end if;
133end|
134create trigger trg3 after update on t1 for each row
135begin
136  if new.j = -1 then
137    set @fired:= "Yes";
138  end if;
139end|
140delimiter ;|
141set @fired:="";
142insert into t1 values (1,2),(2,3),(3,14);
143select @fired;
144select * from t1;
145update t1 set j= 20;
146select @fired;
147select * from t1;
148
149drop trigger trg1;
150drop trigger trg2;
151drop trigger trg3;
152drop table t1;
153
154
155# Let us test how triggers work for special forms of INSERT such as
156# REPLACE and INSERT ... ON DUPLICATE KEY UPDATE
157create table t1 (id int not null primary key, data int);
158create trigger t1_bi before insert on t1 for each row
159  set @log:= concat(@log, "(BEFORE_INSERT: new=(id=", new.id, ", data=", new.data,"))");
160create trigger t1_ai after insert on t1 for each row
161  set @log:= concat(@log, "(AFTER_INSERT: new=(id=", new.id, ", data=", new.data,"))");
162create trigger t1_bu before update on t1 for each row
163  set @log:= concat(@log, "(BEFORE_UPDATE: old=(id=", old.id, ", data=", old.data,
164                                        ") new=(id=", new.id, ", data=", new.data,"))");
165create trigger t1_au after update on t1 for each row
166  set @log:= concat(@log, "(AFTER_UPDATE: old=(id=", old.id, ", data=", old.data,
167                                       ") new=(id=", new.id, ", data=", new.data,"))");
168create trigger t1_bd before delete on t1 for each row
169  set @log:= concat(@log, "(BEFORE_DELETE: old=(id=", old.id, ", data=", old.data,"))");
170create trigger t1_ad after delete on t1 for each row
171  set @log:= concat(@log, "(AFTER_DELETE: old=(id=", old.id, ", data=", old.data,"))");
172# Simple INSERT - both triggers should be called
173set @log:= "";
174insert into t1 values (1, 1);
175select @log;
176# INSERT IGNORE for already existing key - only before trigger should fire
177set @log:= "";
178insert ignore t1 values (1, 2);
179select @log;
180# INSERT ... ON DUPLICATE KEY UPDATE ...
181set @log:= "";
182insert into t1 (id, data) values (1, 3), (2, 2) on duplicate key update data= data + 1;
183select @log;
184# REPLACE (also test for bug#13479 "REPLACE activates UPDATE trigger,
185#          not the DELETE and INSERT triggers")
186# We define REPLACE as INSERT which DELETEs old rows which conflict with
187# row being inserted. So for the first record in statement below we will
188# call before insert trigger, then delete will be executed (and both delete
189# triggers should fire). Finally after insert trigger will be called.
190# For the second record we will just call both on insert triggers.
191set @log:= "";
192replace t1 values (1, 4), (3, 3);
193select @log;
194# Now we will drop ON DELETE triggers to test REPLACE which is internally
195# executed via update
196drop trigger t1_bd;
197drop trigger t1_ad;
198set @log:= "";
199replace t1 values (1, 5);
200select @log;
201
202# This also drops associated triggers
203drop table t1;
204
205
206#
207# Let us test triggers which access other tables.
208#
209# Trivial trigger which inserts data into another table
210create table t1 (id int primary key, data varchar(10), fk int);
211create table t2 (event varchar(100));
212create table t3 (id int primary key);
213create trigger t1_ai after insert on t1 for each row
214  insert into t2 values (concat("INSERT INTO t1 id=", new.id, " data='", new.data, "'"));
215insert into t1 (id, data) values (1, "one"), (2, "two");
216select * from t1;
217select * from t2;
218drop trigger t1_ai;
219# Trigger which uses couple of tables (and partially emulates FK constraint)
220delimiter |;
221create trigger t1_bi before insert on t1 for each row
222begin
223  if exists (select id from t3 where id=new.fk) then
224    insert into t2 values (concat("INSERT INTO t1 id=", new.id, " data='", new.data, "' fk=", new.fk));
225  else
226    insert into t2 values (concat("INSERT INTO t1 FAILED id=", new.id, " data='", new.data, "' fk=", new.fk));
227    set new.id= NULL;
228  end if;
229end|
230delimiter ;|
231insert into t3 values (1);
232--error ER_BAD_NULL_ERROR
233insert into t1 values (4, "four", 1), (5, "five", 2);
234select * from t1;
235select * from t2;
236drop table t1, t2, t3;
237# Trigger which invokes function
238create table t1 (id int primary key, data varchar(10));
239create table t2 (seq int);
240insert into t2 values (10);
241create function f1 () returns int return (select max(seq) from t2);
242delimiter |;
243create trigger t1_bi before insert on t1 for each row
244begin
245  if new.id > f1() then
246    set new.id:= f1();
247  end if;
248end|
249delimiter ;|
250insert into t1 values (1, "first");
251insert into t1 values (f1(), "max");
252select * from t1;
253drop table t1, t2;
254drop function f1;
255# Trigger which forces invocation of another trigger
256# (emulation of FK on delete cascade policy)
257create table t1 (id int primary key, fk_t2 int);
258create table t2 (id int primary key, fk_t3 int);
259create table t3 (id int primary key);
260insert into t1 values (1,1), (2,1), (3,2);
261insert into t2 values (1,1), (2,2);
262insert into t3 values (1), (2);
263create trigger t3_ad after delete on t3 for each row
264  delete from t2 where fk_t3=old.id;
265create trigger t2_ad after delete on t2 for each row
266  delete from t1 where fk_t2=old.id;
267delete from t3 where id = 1;
268select * from t1 left join (t2 left join t3 on t2.fk_t3 = t3.id) on t1.fk_t2 = t2.id;
269drop table t1, t2, t3;
270# Trigger which assigns value selected from table to field of row
271# being inserted/updated.
272create table t1 (id int primary key, copy int);
273create table t2 (id int primary key, data int);
274insert into t2 values (1,1), (2,2);
275create trigger t1_bi before insert on t1 for each row
276  set new.copy= (select data from t2 where id = new.id);
277create trigger t1_bu before update on t1 for each row
278  set new.copy= (select data from t2 where id = new.id);
279insert into t1 values (1,3), (2,4), (3,3);
280update t1 set copy= 1 where id = 2;
281select * from t1;
282drop table t1, t2;
283
284#
285# Test of wrong column specifiers in triggers
286#
287create table t1 (i int);
288create table t3 (i int);
289
290--error ER_TRG_NO_SUCH_ROW_IN_TRG
291create trigger trg before insert on t1 for each row set @a:= old.i;
292--error ER_TRG_NO_SUCH_ROW_IN_TRG
293create trigger trg before delete on t1 for each row set @a:= new.i;
294--error ER_TRG_CANT_CHANGE_ROW
295create trigger trg before update on t1 for each row set old.i:=1;
296--error ER_TRG_NO_SUCH_ROW_IN_TRG
297create trigger trg before delete on t1 for each row set new.i:=1;
298--error ER_TRG_CANT_CHANGE_ROW
299create trigger trg after update on t1 for each row set new.i:=1;
300--error ER_BAD_FIELD_ERROR
301create trigger trg before update on t1 for each row set new.j:=1;
302--error ER_BAD_FIELD_ERROR
303create trigger trg before update on t1 for each row set @a:=old.j;
304
305
306#
307# Let us test various trigger creation errors
308# Also quickly test table namespace (bug#5892/6182)
309#
310--error ER_NO_SUCH_TABLE
311create trigger trg before insert on t2 for each row set @a:=1;
312
313create trigger trg before insert on t1 for each row set @a:=1;
314--error ER_TRG_ALREADY_EXISTS
315create trigger trg after insert on t1 for each row set @a:=1;
316--error ER_TRG_ALREADY_EXISTS
317create trigger trg before insert on t3 for each row set @a:=1;
318create trigger trg2 before insert on t3 for each row set @a:=1;
319drop trigger trg2;
320drop trigger trg;
321
322--error ER_TRG_DOES_NOT_EXIST
323drop trigger trg;
324
325create view v1 as select * from t1;
326--error ER_WRONG_OBJECT
327create trigger trg before insert on v1 for each row set @a:=1;
328drop view v1;
329
330drop table t1;
331drop table t3;
332
333create temporary table t1 (i int);
334--error ER_TRG_ON_VIEW_OR_TEMP_TABLE
335create trigger trg before insert on t1 for each row set @a:=1;
336drop table t1;
337
338
339
340#
341# Tests for various trigger-related bugs
342#
343
344# Test for bug #5887 "Triggers with string literals cause errors".
345# New .FRM parser was not handling escaped strings properly.
346create table t1 (x1col char);
347create trigger tx1 before insert on t1 for each row set new.x1col = 'x';
348insert into t1 values ('y');
349drop trigger tx1;
350drop table t1;
351
352#
353# Test for bug #5890 "Triggers fail for DELETE without WHERE".
354# If we are going to delete all rows in table but DELETE triggers exist
355# we should perform row-by-row deletion instead of using optimized
356# delete_all_rows() method.
357#
358create table t1 (i int);
359insert into t1 values (1), (2);
360create trigger trg1 before delete on t1 for each row set @del_before:= @del_before + old.i;
361create trigger trg2 after delete on t1 for each row set @del_after:= @del_after + old.i;
362set @del_before:=0, @del_after:= 0;
363delete from t1;
364select @del_before, @del_after;
365drop trigger trg1;
366drop trigger trg2;
367drop table t1;
368
369# Test for bug #5859 "DROP TABLE does not drop triggers". Trigger should not
370# magically reappear when we recreate dropped table.
371create table t1 (a int);
372create trigger trg1 before insert on t1 for each row set new.a= 10;
373drop table t1;
374create table t1 (a int);
375insert into t1 values ();
376select * from t1;
377drop table t1;
378
379# Test for bug #6559 "DROP DATABASE forgets to drop triggers".
380create database mysqltest;
381use mysqltest;
382create table t1 (i int);
383create trigger trg1 before insert on t1 for each row set @a:= 1;
384# This should succeed
385drop database mysqltest;
386use test;
387
388# Test for bug #8791
389# "Triggers: Allowed to create triggers on a subject table in a different DB".
390create database mysqltest;
391create table mysqltest.t1 (i int);
392--error ER_TRG_IN_WRONG_SCHEMA
393create trigger trg1 before insert on mysqltest.t1 for each row set @a:= 1;
394use mysqltest;
395--error ER_NO_SUCH_TABLE
396create trigger test.trg1 before insert on t1 for each row set @a:= 1;
397drop database mysqltest;
398use test;
399
400
401# Test for bug #5860 "Multi-table UPDATE does not activate update triggers"
402# We will also test how delete triggers wor for multi-table DELETE.
403create table t1 (i int, j int default 10, k int not null, key (k));
404create table t2 (i int);
405insert into t1 (i, k) values (1, 1);
406insert into t2 values (1);
407create trigger trg1 before update on t1 for each row set @a:= @a + new.j - old.j;
408create trigger trg2 after update on t1 for each row set @b:= "Fired";
409set @a:= 0, @b:= "";
410# Check that trigger works in case of update on the fly
411update t1, t2 set j = j + 10 where t1.i = t2.i;
412select @a, @b;
413insert into t1 values (2, 13, 2);
414insert into t2 values (2);
415set @a:= 0, @b:= "";
416# And now let us check that triggers work in case of multi-update which
417# is done through temporary tables...
418update t1, t2 set j = j + 15 where t1.i = t2.i and t1.k >= 2;
419select @a, @b;
420# Let us test delete triggers for multi-delete now.
421# We create triggers for both tables because we want test how they
422# work in both on-the-fly and via-temp-tables cases.
423create trigger trg3 before delete on t1 for each row set @c:= @c + old.j;
424create trigger trg4 before delete on t2 for each row set @d:= @d + old.i;
425create trigger trg5 after delete on t1 for each row set @e:= "After delete t1 fired";
426create trigger trg6 after delete on t2 for each row set @f:= "After delete t2 fired";
427set @c:= 0, @d:= 0, @e:= "", @f:= "";
428delete t1, t2 from t1, t2 where t1.i = t2.i;
429select @c, @d, @e, @f;
430# This also will drop triggers
431drop table t1, t2;
432
433# Test for bug #6812 "Triggers are not activated for INSERT ... SELECT".
434# (We also check the fact that trigger modifies some field does not affect
435#  value of next record inserted).
436delimiter |;
437create table t1 (i int, j int default 10)|
438create table t2 (i int)|
439insert into t2 values (1), (2)|
440create trigger trg1 before insert on t1 for each row
441begin
442  if new.i = 1 then
443    set new.j := 1;
444  end if;
445end|
446create trigger trg2 after insert on t1 for each row set @a:= 1|
447set @a:= 0|
448insert into t1 (i) select * from t2|
449select * from t1|
450select @a|
451# This also will drop triggers
452drop table t1, t2|
453delimiter ;|
454
455# Test for bug #8755 "Trigger is not activated by LOAD DATA"
456create table t1 (i int, j int, k int);
457create trigger trg1 before insert on t1 for each row set new.k = new.i;
458create trigger trg2 after insert on t1 for each row set @b:= "Fired";
459set @b:="";
460# Test triggers with file with separators
461load data infile '../../std_data/rpl_loaddata.dat' into table t1 (@a, i);
462select *, @b from t1;
463set @b:="";
464# Test triggers with fixed size row file
465load data infile '../../std_data/loaddata5.dat' into table t1 fields terminated by '' enclosed by '' (i, j);
466select *, @b from t1;
467# This also will drop triggers
468drop table t1;
469
470create table t1 (i int, bt int, k int, key(k));
471create table t2 (i int);
472insert into t1 values (1, 1, 1), (2, 2, 2);
473insert into t2 values (1), (2), (3);
474# Create and then break "before" triggers
475create trigger bi before insert on t1 for each row set @a:= new.bt;
476create trigger bu before update on t1 for each row set @a:= new.bt;
477create trigger bd before delete on t1 for each row set @a:= old.bt;
478alter table t1 drop column bt;
479# The following statements changing t1 should fail and should not
480# cause any effect on table, since "before" trigger is executed
481# before operation on the table row.
482--error ER_BAD_FIELD_ERROR
483insert into t1 values (3, 3);
484select * from t1;
485--error ER_BAD_FIELD_ERROR
486update t1 set i = 2;
487select * from t1;
488--error ER_BAD_FIELD_ERROR
489delete from t1;
490select * from t1;
491--error ER_BAD_FIELD_ERROR
492load data infile '../../std_data/loaddata5.dat' into table t1 fields terminated by '' enclosed by '' (i, k);
493select * from t1;
494--error ER_BAD_FIELD_ERROR
495insert into t1 select 3, 3;
496select * from t1;
497# Both types of multi-update (on the fly and via temp table)
498--error ER_BAD_FIELD_ERROR
499update t1, t2 set k = k + 10 where t1.i = t2.i;
500select * from t1;
501--error ER_BAD_FIELD_ERROR
502update t1, t2 set k = k + 10 where t1.i = t2.i and k < 2;
503select * from t1;
504# Both types of multi-delete
505--error ER_BAD_FIELD_ERROR
506delete t1, t2 from t1 straight_join t2 where t1.i = t2.i;
507select * from t1;
508--error ER_BAD_FIELD_ERROR
509delete t2, t1 from t2 straight_join t1 where t1.i = t2.i;
510select * from t1;
511# Let us test REPLACE/INSERT ... ON DUPLICATE KEY UPDATE.
512# To test properly code-paths different from those that are used
513# in ordinary INSERT we need to drop "before insert" trigger.
514alter table t1 add primary key (i);
515drop trigger bi;
516--error ER_BAD_FIELD_ERROR
517insert into t1 values (2, 4) on duplicate key update k= k + 10;
518select * from t1;
519--error ER_BAD_FIELD_ERROR
520replace into t1 values (2, 4);
521select * from t1;
522# Also drops all triggers
523drop table t1, t2;
524
525# Test for bug #5893 "Triggers with dropped functions cause crashes"
526# Appropriate error should be reported instead of crash.
527# Also test for bug #11889 "Server crashes when dropping trigger
528# using stored routine".
529create table t1 (col1 int, col2 int);
530insert into t1 values (1, 2);
531create function bug5893 () returns int return 5;
532create trigger t1_bu before update on t1 for each row set new.col1= bug5893();
533drop function bug5893;
534--error ER_SP_DOES_NOT_EXIST
535update t1 set col2 = 4;
536# This should not crash server too.
537drop trigger t1_bu;
538drop table t1;
539
540#
541# storing and restoring parsing modes for triggers (BUG#5891)
542#
543set sql_mode='ansi';
544create table t1 ("t1 column" int);
545create trigger t1_bi before insert on t1 for each row set new."t1 column" = 5;
546set sql_mode="";
547insert into t1 values (0);
548# create trigger with different sql_mode
549create trigger t1_af after insert on t1 for each row set @a=10;
550insert into t1 values (0);
551select * from t1;
552select @a;
553--replace_column 6 #
554show triggers;
555drop table t1;
556# check that rigger preserve sql_mode during execution
557set sql_mode="traditional";
558create table t1 (a date);
559-- error 1292
560insert into t1 values ('2004-01-00');
561set sql_mode="";
562create trigger t1_bi before insert on t1 for each row set new.a = '2004-01-00';
563set sql_mode="traditional";
564insert into t1 values ('2004-01-01');
565select * from t1;
566set sql_mode=default;
567show create table t1;
568--replace_column 6 #
569show triggers;
570drop table t1;
571
572# Test for bug #12280 "Triggers: crash if flush tables"
573# FLUSH TABLES and FLUSH PRIVILEGES should be disallowed inside
574# of functions and triggers.
575create table t1 (id int);
576--error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
577create trigger t1_ai after insert on t1 for each row reset master;
578--error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
579create trigger t1_ai after insert on t1 for each row reset slave;
580--error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
581create trigger t1_ai after insert on t1 for each row flush hosts;
582--error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
583create trigger t1_ai after insert on t1 for each row flush tables with read lock;
584--error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
585create trigger t1_ai after insert on t1 for each row flush logs;
586--error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
587create trigger t1_ai after insert on t1 for each row flush status;
588--error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
589create trigger t1_ai after insert on t1 for each row flush user_resources;
590--error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
591create trigger t1_ai after insert on t1 for each row flush tables;
592--error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
593create trigger t1_ai after insert on t1 for each row flush privileges;
594
595create trigger t1_ai after insert on t1 for each row call p1();
596create procedure p1() flush tables;
597--error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
598insert into t1 values (0);
599
600drop procedure p1;
601create procedure p1() reset master;
602--error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
603insert into t1 values (0);
604
605drop procedure p1;
606create procedure p1() reset slave;
607--error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
608insert into t1 values (0);
609
610drop procedure p1;
611create procedure p1() flush hosts;
612--error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
613insert into t1 values (0);
614
615drop procedure p1;
616create procedure p1() flush privileges;
617--error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
618insert into t1 values (0);
619
620drop procedure p1;
621create procedure p1() flush tables with read lock;
622--error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
623insert into t1 values (0);
624
625drop procedure p1;
626create procedure p1() flush tables;
627--error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
628insert into t1 values (0);
629
630drop procedure p1;
631create procedure p1() flush logs;
632--error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
633insert into t1 values (0);
634
635drop procedure p1;
636create procedure p1() flush status;
637--error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
638insert into t1 values (0);
639
640drop procedure p1;
641create procedure p1() flush user_resources;
642--error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
643insert into t1 values (0);
644
645drop procedure p1;
646drop table t1;
647
648# Test for bug #11973 "SELECT .. INTO var_name; in trigger cause
649#                      crash on update"
650
651create table t1 (id int, data int, username varchar(16));
652insert into t1 (id, data) values (1, 0);
653delimiter |;
654create trigger t1_whoupdated before update on t1 for each row
655begin
656  declare user varchar(32);
657  declare i int;
658  select user() into user;
659  set NEW.username = user;
660  select count(*) from ((select 1) union (select 2)) as d1 into i;
661end|
662delimiter ;|
663update t1 set data = 1;
664
665connection addconroot1;
666update t1 set data = 2;
667
668connection default;
669drop table t1;
670
671#
672# #11587 Trigger causes lost connection error
673#
674
675create table t1 (c1 int, c2 datetime);
676delimiter |;
677--error ER_SP_NO_RETSET
678create trigger tr1 before insert on t1 for each row
679begin
680  set new.c2= '2004-04-01';
681  select 'hello';
682end|
683delimiter ;|
684
685insert into t1 (c1) values (1),(2),(3);
686select * from t1;
687
688delimiter |;
689create procedure bug11587(x char(16))
690begin
691  select "hello";
692  select "hello again";
693end|
694
695create trigger tr1 before insert on t1 for each row
696begin
697  call bug11587(new.c2);
698  set new.c2= '2004-04-02';
699end|
700delimiter ;|
701
702--error ER_SP_NO_RETSET
703insert into t1 (c1) values (4),(5),(6);
704select * from t1;
705
706drop procedure bug11587;
707drop table t1;
708
709# Test for bug #11896 "Partial locking in case of recursive trigger
710# definitions". Recursion in triggers should not be allowed.
711# We also should not allow to change tables which are used in
712# statements invoking this trigger.
713create table t1 (f1 integer);
714create table t2 (f2 integer);
715create trigger t1_ai after insert on t1
716  for each row insert into t2 values (new.f1+1);
717create trigger t2_ai after insert on t2
718  for each row insert into t1 values (new.f2+1);
719# Allow SP resursion to be show that it has not influence here
720set @SAVE_SP_RECURSION_LEVELS=@@max_sp_recursion_depth;
721set @@max_sp_recursion_depth=100;
722--error ER_CANT_UPDATE_USED_TABLE_IN_SF_OR_TRG
723insert into t1 values (1);
724set @@max_sp_recursion_depth=@SAVE_SP_RECURSION_LEVELS;
725select * from t1;
726select * from t2;
727drop trigger t1_ai;
728drop trigger t2_ai;
729create trigger t1_bu before update on t1
730  for each row insert into t1 values (2);
731insert into t1 values (1);
732--error ER_CANT_UPDATE_USED_TABLE_IN_SF_OR_TRG
733update t1 set f1= 10;
734select * from t1;
735drop trigger t1_bu;
736create trigger t1_bu before update on t1
737  for each row delete from t1 where f1=new.f1;
738--error ER_CANT_UPDATE_USED_TABLE_IN_SF_OR_TRG
739update t1 set f1= 10;
740select * from t1;
741drop trigger t1_bu;
742# This should work tough
743create trigger t1_bi before insert on t1
744  for each row set new.f1=(select sum(f1) from t1);
745insert into t1 values (3);
746select * from t1;
747drop trigger t1_bi;
748drop tables t1, t2;
749
750# Tests for bug #12704 "Server crashes during trigger execution".
751# If we run DML statements and CREATE TRIGGER statements concurrently
752# it may happen that trigger will be created while DML statement is
753# waiting for table lock. In this case we have to reopen tables and
754# recalculate prelocking set.
755# Unfortunately these tests rely on the order in which tables are locked
756# by statement so they are non determenistic and are disabled.
757--disable_testcase BUG#0000
758create table t1 (id int);
759create table t2 (id int);
760create table t3 (id int);
761create function f1() returns int return (select max(id)+2 from t2);
762create view v1 as select f1() as f;
763
764# Let us check that we notice trigger at all
765connection addconroot1;
766lock tables t2 write;
767connection default;
768send insert into t1 values ((select max(id) from t2)), (2);
769--sleep 1
770connection addconroot2;
771create trigger t1_trg before insert on t1 for each row set NEW.id:= 1;
772connection addconroot1;
773unlock tables;
774connection default;
775reap;
776select * from t1;
777
778# Check that we properly calculate new prelocking set
779insert into t2 values (3);
780connection addconroot1;
781lock tables t2 write;
782connection default;
783send insert into t1 values ((select max(id) from t2)), (4);
784--sleep 1
785connection addconroot2;
786drop trigger t1_trg;
787create trigger t1_trg before insert on t1 for each row
788  insert into t3 values (new.id);
789connection addconroot1;
790unlock tables;
791connection default;
792reap;
793select * from t1;
794select * from t3;
795
796# We should be able to do this even if fancy views are involved
797connection addconroot1;
798lock tables t2 write;
799connection default;
800send insert into t1 values ((select max(f) from v1)), (6);
801--sleep 1
802connection addconroot2;
803drop trigger t1_trg;
804create trigger t1_trg before insert on t1 for each row
805  insert into t3 values (new.id + 100);
806connection addconroot1;
807unlock tables;
808connection default;
809reap;
810select * from t1;
811select * from t3;
812
813# This also should work for multi-update
814# Let us drop trigger to demonstrate that prelocking set is really
815# rebuilt
816drop trigger t1_trg;
817connection addconroot1;
818lock tables t2 write;
819connection default;
820send update t1, t2 set t1.id=10 where t1.id=t2.id;
821--sleep 1
822connection addconroot2;
823create trigger t1_trg before update on t1 for each row
824  insert into t3 values (new.id);
825connection addconroot1;
826unlock tables;
827connection default;
828reap;
829select * from t1;
830select * from t3;
831
832# And even for multi-update converted from ordinary update thanks to view
833drop view v1;
834drop trigger t1_trg;
835create view v1 as select t1.id as id1 from t1, t2 where t1.id= t2.id;
836insert into t2 values (10);
837connection addconroot1;
838lock tables t2 write;
839connection default;
840send update v1 set id1= 11;
841--sleep 1
842connection addconroot2;
843create trigger t1_trg before update on t1 for each row
844  insert into t3 values (new.id + 100);
845connection addconroot1;
846unlock tables;
847connection default;
848reap;
849select * from t1;
850select * from t3;
851
852drop function f1;
853drop view v1;
854drop table t1, t2, t3;
855--enable_testcase
856
857#
858# Test for bug #13399 "Crash when executing PS/SP which should activate
859# trigger which is now dropped". See also test for similar bug for stored
860# routines in sp-error.test (#12329).
861create table t1 (id int);
862create table t2 (id int);
863create trigger t1_bi before insert on t1 for each row insert into t2 values (new.id);
864prepare stmt1 from "insert into t1 values (10)";
865create procedure p1() insert into t1 values (10);
866call p1();
867# Actually it is enough to do FLUSH TABLES instead of DROP TRIGGER
868drop trigger t1_bi;
869# Server should not crash on these two statements
870execute stmt1;
871call p1();
872deallocate prepare stmt1;
873drop procedure p1;
874
875# Let us test more complex situation when we alter trigger in such way that
876# it uses different set of tables (or simply add new trigger).
877create table t3 (id int);
878create trigger t1_bi after insert on t1 for each row insert into t2 values (new.id);
879prepare stmt1 from "insert into t1 values (10)";
880create procedure p1() insert into t1 values (10);
881call p1();
882# Altering trigger forcing it use different set of tables
883drop trigger t1_bi;
884create trigger t1_bi after insert on t1 for each row insert into t3 values (new.id);
885execute stmt1;
886call p1();
887deallocate prepare stmt1;
888drop procedure p1;
889drop table t1, t2, t3;
890
891#
892# BUG#13549 "Server crash with nested stored procedures".
893# Server should not crash when during execution of stored procedure
894# we have to parse trigger/function definition and this new trigger/
895# function has more local variables declared than invoking stored
896# procedure and last of these variables is used in argument of NOT
897# operator.
898#
899create table t1 (a int);
900DELIMITER //;
901CREATE PROCEDURE `p1`()
902begin
903  insert into t1 values (1);
904end//
905create trigger trg before insert on t1 for each row
906begin
907  declare done int default 0;
908  set done= not done;
909end//
910DELIMITER ;//
911CALL p1();
912drop procedure p1;
913drop table t1;
914
915#
916# Test for bug #14863 "Triggers: crash if create and there is no current
917# database". We should not crash and give proper error when database for
918# trigger or its table is not specified and there is no current database.
919#
920connection addconwithoutdb;
921--error ER_NO_DB_ERROR
922create trigger t1_bi before insert on test.t1 for each row set @a:=0;
923--error ER_NO_SUCH_TABLE
924create trigger test.t1_bi before insert on t1 for each row set @a:=0;
925--error ER_NO_DB_ERROR
926drop trigger t1_bi;
927connection default;
928
929#
930# Tests for bug #13525 "Rename table does not keep info of triggers"
931# and bug #17866 "Problem with renaming table with triggers with fully
932# qualified subject table".
933#
934create table t1 (id int);
935create trigger t1_bi before insert on t1 for each row set @a:=new.id;
936create trigger t1_ai after insert on test.t1 for each row set @b:=new.id;
937insert into t1 values (101);
938select @a, @b;
939select trigger_schema, trigger_name, event_object_schema,
940       event_object_table, action_statement from information_schema.triggers
941       where event_object_schema = 'test';
942rename table t1 to t2;
943# Trigger should work after rename
944insert into t2 values (102);
945select @a, @b;
946select trigger_schema, trigger_name, event_object_schema,
947       event_object_table, action_statement from information_schema.triggers
948       where event_object_schema = 'test';
949# Let us check that the same works for simple ALTER TABLE ... RENAME
950alter table t2 rename to t3;
951insert into t3 values (103);
952select @a, @b;
953select trigger_schema, trigger_name, event_object_schema,
954       event_object_table, action_statement from information_schema.triggers
955       where event_object_schema = 'test';
956# And for more complex ALTER TABLE
957alter table t3 rename to t4, add column val int default 0;
958insert into t4 values (104, 1);
959select @a, @b;
960select trigger_schema, trigger_name, event_object_schema,
961       event_object_table, action_statement from information_schema.triggers
962       where event_object_schema = 'test';
963# .TRN file should be updated with new table name
964drop trigger t1_bi;
965drop trigger t1_ai;
966drop table t4;
967# Rename between different databases if triggers exist should fail
968create database mysqltest;
969use mysqltest;
970create table t1 (id int);
971create trigger t1_bi before insert on t1 for each row set @a:=new.id;
972insert into t1 values (101);
973select @a;
974select trigger_schema, trigger_name, event_object_schema,
975       event_object_table, action_statement from information_schema.triggers
976       where event_object_schema = 'test' or event_object_schema = 'mysqltest';
977--error ER_TRG_IN_WRONG_SCHEMA
978rename table t1 to test.t2;
979insert into t1 values (102);
980select @a;
981select trigger_schema, trigger_name, event_object_schema,
982       event_object_table, action_statement from information_schema.triggers
983       where event_object_schema = 'test' or event_object_schema = 'mysqltest';
984# There should be no fantom .TRN files
985--error ER_TRG_DOES_NOT_EXIST
986drop trigger test.t1_bi;
987# Let us also check handling of this restriction in ALTER TABLE ... RENAME
988--error ER_TRG_IN_WRONG_SCHEMA
989alter table t1 rename to test.t1;
990insert into t1 values (103);
991select @a;
992select trigger_schema, trigger_name, event_object_schema,
993       event_object_table, action_statement from information_schema.triggers
994       where event_object_schema = 'test' or event_object_schema = 'mysqltest';
995# Again there should be no fantom .TRN files
996--error ER_TRG_DOES_NOT_EXIST
997drop trigger test.t1_bi;
998--error ER_TRG_IN_WRONG_SCHEMA
999alter table t1 rename to test.t1, add column val int default 0;
1000insert into t1 values (104);
1001select @a;
1002select trigger_schema, trigger_name, event_object_schema,
1003       event_object_table, action_statement from information_schema.triggers
1004       where event_object_schema = 'test' or event_object_schema = 'mysqltest';
1005# Table definition should not change
1006show create table t1;
1007# And once again check for fantom .TRN files
1008--error ER_TRG_DOES_NOT_EXIST
1009drop trigger test.t1_bi;
1010drop trigger t1_bi;
1011drop table t1;
1012drop database mysqltest;
1013use test;
1014
1015# Test for bug #16829 "Firing trigger with RETURN crashes the server"
1016# RETURN is not supposed to be used anywhere except functions, so error
1017# should be returned when one attempts to create trigger with RETURN.
1018create table t1 (i int);
1019--error ER_SP_BADRETURN
1020create trigger t1_bi before insert on t1 for each row return 0;
1021insert into t1 values (1);
1022drop table t1;
1023
1024# Test for bug #17764 "Trigger crashes table"
1025#
1026# Table was reported as crashed when it was subject table of trigger invoked
1027# by insert statement which was executed with enabled bulk insert mode (which
1028# is actually set of optimizations enabled by handler::start_bulk_insert())
1029# and this trigger also explicitly referenced it.
1030# The same problem arose when table to which bulk insert was done was also
1031# referenced in function called by insert statement.
1032create table t1 (a varchar(64), b int);
1033create table t2 like t1;
1034create trigger t1_ai after insert on t1 for each row
1035  set @a:= (select max(a) from t1);
1036insert into t1 (a) values
1037  ("Twas"),("brillig"),("and"),("the"),("slithy"),("toves"),
1038  ("Did"),("gyre"),("and"),("gimble"),("in"),("the"),("wabe");
1039create trigger t2_ai after insert on t2 for each row
1040  set @a:= (select max(a) from t2);
1041insert into t2 select * from t1;
1042load data infile '../../std_data/words.dat' into table t1 (a);
1043drop trigger t1_ai;
1044drop trigger t2_ai;
1045# Test that the problem for functions is fixed as well
1046create function f1() returns int return (select max(b) from t1);
1047insert into t1 values
1048  ("All",f1()),("mimsy",f1()),("were",f1()),("the",f1()),("borogoves",f1()),
1049  ("And",f1()),("the",f1()),("mome", f1()),("raths",f1()),("outgrabe",f1());
1050create function f2() returns int return (select max(b) from t2);
1051insert into t2 select a, f2() from t1;
1052load data infile '../../std_data/words.dat' into table t1 (a) set b:= f1();
1053drop function f1;
1054drop function f2;
1055drop table t1, t2;
1056
1057#
1058# Test for bug #16021 "Wrong index given to function in trigger" which
1059# was caused by the same bulk insert optimization as bug #17764 but had
1060# slightly different symptoms (instead of reporting table as crashed
1061# storage engine reported error number 124)
1062#
1063create table t1(i int not null, j int not null, n numeric(15,2), primary key(i,j));
1064create table t2(i int not null, n numeric(15,2), primary key(i));
1065delimiter |;
1066create trigger t1_ai after insert on t1 for each row
1067begin
1068  declare sn numeric(15,2);
1069  select sum(n) into sn from t1 where i=new.i;
1070  replace into t2 values(new.i, sn);
1071end|
1072delimiter ;|
1073insert into t1 values
1074  (1,1,10.00),(1,2,10.00),(1,3,10.00),(1,4,10.00),(1,5,10.00),
1075  (1,6,10.00),(1,7,10.00),(1,8,10.00),(1,9,10.00),(1,10,10.00),
1076  (1,11,10.00),(1,12,10.00),(1,13,10.00),(1,14,10.00),(1,15,10.00);
1077select * from t1;
1078select * from t2;
1079drop tables t1, t2;
1080
1081#
1082# Test for Bug #16461 connection_id() does not work properly inside trigger
1083#
1084CREATE TABLE t1 (
1085    conn_id INT,
1086    trigger_conn_id INT
1087);
1088CREATE TRIGGER t1_bi BEFORE INSERT ON t1 FOR EACH ROW
1089  SET NEW.trigger_conn_id = CONNECTION_ID();
1090
1091INSERT INTO t1 (conn_id, trigger_conn_id) VALUES (CONNECTION_ID(), -1);
1092
1093connect (con1,localhost,root,,);
1094INSERT INTO t1 (conn_id, trigger_conn_id) VALUES (CONNECTION_ID(), -1);
1095connection default;
1096disconnect con1;
1097
1098SELECT * FROM t1 WHERE conn_id != trigger_conn_id;
1099
1100DROP TRIGGER t1_bi;
1101DROP TABLE t1;
1102
1103
1104#
1105# Bug#6951: Triggers/Traditional: SET @ result wrong
1106#
1107
1108CREATE TABLE t1 (i1 INT);
1109
1110SET @save_sql_mode=@@sql_mode;
1111
1112SET SQL_MODE='';
1113
1114CREATE TRIGGER t1_ai AFTER INSERT ON t1 FOR EACH ROW
1115  SET @x = 5/0;
1116
1117SET SQL_MODE='traditional';
1118
1119CREATE TRIGGER t1_au AFTER UPDATE ON t1 FOR EACH ROW
1120  SET @x = 5/0;
1121
1122SET @x=1;
1123INSERT INTO t1 VALUES (@x);
1124SELECT @x;
1125
1126SET @x=2;
1127UPDATE t1 SET i1 = @x;
1128SELECT @x;
1129
1130SET SQL_MODE='';
1131
1132SET @x=3;
1133INSERT INTO t1 VALUES (@x);
1134SELECT @x;
1135
1136SET @x=4;
1137UPDATE t1 SET i1 = @x;
1138SELECT @x;
1139
1140SET @@sql_mode=@save_sql_mode;
1141
1142DROP TRIGGER t1_ai;
1143DROP TRIGGER t1_au;
1144DROP TABLE t1;
1145
1146
1147#
1148# Test for bug #14635 Accept NEW.x as INOUT parameters to stored
1149# procedures from within triggers
1150#
1151--disable_warnings
1152DROP TABLE IF EXISTS t1;
1153DROP PROCEDURE IF EXISTS p1;
1154DROP PROCEDURE IF EXISTS p2;
1155--enable_warnings
1156
1157CREATE TABLE t1 (i1 INT);
1158
1159# Check that NEW.x pseudo variable is accepted as INOUT and OUT
1160# parameter to stored routine.
1161INSERT INTO t1 VALUES (3);
1162CREATE PROCEDURE p1(OUT i1 INT) DETERMINISTIC NO SQL SET i1 = 5;
1163CREATE PROCEDURE p2(INOUT i1 INT) DETERMINISTIC NO SQL SET i1 = i1 * 7;
1164delimiter //;
1165CREATE TRIGGER t1_bu BEFORE UPDATE ON t1 FOR EACH ROW
1166BEGIN
1167  CALL p1(NEW.i1);
1168  CALL p2(NEW.i1);
1169END//
1170delimiter ;//
1171UPDATE t1 SET i1 = 11 WHERE i1 = 3;
1172DROP TRIGGER t1_bu;
1173DROP PROCEDURE p2;
1174DROP PROCEDURE p1;
1175
1176# Check that OLD.x pseudo variable is not accepted as INOUT and OUT
1177# parameter to stored routine.
1178INSERT INTO t1 VALUES (13);
1179CREATE PROCEDURE p1(OUT i1 INT) DETERMINISTIC NO SQL SET @a = 17;
1180CREATE TRIGGER t1_bu BEFORE UPDATE ON t1 FOR EACH ROW
1181  CALL p1(OLD.i1);
1182--error ER_SP_NOT_VAR_ARG
1183UPDATE t1 SET i1 = 19 WHERE i1 = 13;
1184DROP TRIGGER t1_bu;
1185DROP PROCEDURE p1;
1186
1187INSERT INTO t1 VALUES (23);
1188CREATE PROCEDURE p1(INOUT i1 INT) DETERMINISTIC NO SQL SET @a = i1 * 29;
1189CREATE TRIGGER t1_bu BEFORE UPDATE ON t1 FOR EACH ROW
1190  CALL p1(OLD.i1);
1191--error ER_SP_NOT_VAR_ARG
1192UPDATE t1 SET i1 = 31 WHERE i1 = 23;
1193DROP TRIGGER t1_bu;
1194DROP PROCEDURE p1;
1195
1196# Check that NEW.x pseudo variable is read-only in the AFTER TRIGGER.
1197INSERT INTO t1 VALUES (37);
1198CREATE PROCEDURE p1(OUT i1 INT) DETERMINISTIC NO SQL SET @a = 41;
1199CREATE TRIGGER t1_au AFTER UPDATE ON t1 FOR EACH ROW
1200  CALL p1(NEW.i1);
1201--error ER_SP_NOT_VAR_ARG
1202UPDATE t1 SET i1 = 43 WHERE i1 = 37;
1203DROP TRIGGER t1_au;
1204DROP PROCEDURE p1;
1205
1206INSERT INTO t1 VALUES (47);
1207CREATE PROCEDURE p1(INOUT i1 INT) DETERMINISTIC NO SQL SET @a = i1 * 49;
1208CREATE TRIGGER t1_au AFTER UPDATE ON t1 FOR EACH ROW
1209  CALL p1(NEW.i1);
1210--error ER_SP_NOT_VAR_ARG
1211UPDATE t1 SET i1 = 51 WHERE i1 = 47;
1212DROP TRIGGER t1_au;
1213DROP PROCEDURE p1;
1214
1215# Post requisite.
1216SELECT * FROM t1;
1217
1218DROP TABLE t1;
1219
1220#
1221# Bug #18005: Creating a trigger on mysql.event leads to server crash on
1222# scheduler startup
1223#
1224# Bug #18361: Triggers on mysql.user table cause server crash
1225#
1226# We don't allow triggers on the mysql schema
1227delimiter |;
1228--error ER_NO_TRIGGERS_ON_SYSTEM_SCHEMA
1229create trigger wont_work after update on mysql.user for each row
1230begin
1231 set @a:= 1;
1232end|
1233# Try when we're already using the mysql schema
1234use mysql|
1235--error ER_NO_TRIGGERS_ON_SYSTEM_SCHEMA
1236create trigger wont_work after update on event for each row
1237begin
1238 set @a:= 1;
1239end|
1240use test|
1241delimiter ;|
1242
1243
1244#
1245# Test for BUG#16899: Possible buffer overflow in handling of DEFINER-clause.
1246#
1247
1248# Prepare.
1249
1250--disable_warnings
1251DROP TABLE IF EXISTS t1;
1252DROP TABLE IF EXISTS t2;
1253--enable_warnings
1254
1255CREATE TABLE t1(c INT);
1256CREATE TABLE t2(c INT);
1257
1258--error ER_WRONG_STRING_LENGTH
1259CREATE DEFINER=1234567890abcdefGHIKL1234567890abcdefGHIKL@localhost
1260  TRIGGER t1_bi BEFORE INSERT ON t1 FOR EACH ROW SET @a = 1;
1261
1262--error ER_WRONG_STRING_LENGTH
1263CREATE DEFINER=some_user_name@host_1234567890abcdefghij1234567890abcdefghij1234567890abcdefghij1234567890abcdefghij1234567890abcdefghij1234567890abcdefghij1234567890abcdefghij1234567890abcdefghij1234567890abcdefghij1234567890abcdefghij1234567890abcdefghij1234567890abcdefghij1234567890X
1264  TRIGGER t2_bi BEFORE INSERT ON t2 FOR EACH ROW SET @a = 2;
1265
1266# Cleanup.
1267
1268DROP TABLE t1;
1269DROP TABLE t2;
1270
1271#
1272# Bug#20028 Function with select return no data
1273#
1274
1275--disable_warnings
1276drop table if exists t1;
1277drop table if exists t2;
1278drop table if exists t3;
1279drop table if exists t4;
1280--enable_warnings
1281
1282SET @save_sql_mode=@@sql_mode;
1283
1284delimiter |;
1285SET sql_mode='TRADITIONAL'|
1286create table t1 (id int(10) not null primary key, v int(10) )|
1287create table t2 (id int(10) not null primary key, v int(10) )|
1288create table t3 (id int(10) not null primary key, v int(10) )|
1289create table t4 (c int)|
1290
1291create trigger t4_bi before insert on t4 for each row set @t4_bi_called:=1|
1292create trigger t4_bu before update on t4 for each row set @t4_bu_called:=1|
1293
1294insert into t1 values(10, 10)|
1295set @a:=1/0|
1296select 1/0 from t1|
1297
1298create trigger t1_bi before insert on t1 for each row set @a:=1/0|
1299
1300insert into t1 values(20, 20)|
1301
1302drop trigger t1_bi|
1303create trigger t1_bi before insert on t1 for each row
1304begin
1305  insert into t2 values (new.id, new.v);
1306  update t2 set v=v+1 where id= new.id;
1307  replace t3 values (new.id, 0);
1308  update t2, t3 set t2.v=new.v, t3.v=new.v where t2.id=t3.id;
1309  create temporary table t5 select * from t1;
1310  delete from t5;
1311  insert into t5 select * from t1;
1312  insert into t4 values (0);
1313  set @check= (select count(*) from t5);
1314  update t4 set c= @check;
1315  drop temporary table t5;
1316
1317  set @a:=1/0;
1318end|
1319
1320set @check=0, @t4_bi_called=0, @t4_bu_called=0|
1321insert into t1 values(30, 30)|
1322select @check, @t4_bi_called, @t4_bu_called|
1323
1324delimiter ;|
1325
1326SET @@sql_mode=@save_sql_mode;
1327
1328drop table t1;
1329drop table t2;
1330drop table t3;
1331drop table t4;
1332
1333#
1334# Bug#20670 "UPDATE using key and invoking trigger that modifies
1335#            this key does not stop"
1336#
1337
1338create table t1 (i int, j int key);
1339insert into t1 values (1,1), (2,2), (3,3);
1340create trigger t1_bu before update on t1 for each row
1341  set new.j = new.j + 10;
1342# This should not work indefinitely and should cause
1343# expected result
1344update t1 set i= i+ 10 where j > 2;
1345select * from t1;
1346drop table t1;
1347
1348#
1349# Bug#23556 TRUNCATE TABLE still maps to DELETE
1350#
1351CREATE TABLE t1 (a INT PRIMARY KEY);
1352CREATE TABLE t2 (a INT PRIMARY KEY);
1353INSERT INTO t1 VALUES (1),(2),(3),(4),(5),(6),(7),(8);
1354
1355CREATE TRIGGER trg_t1 BEFORE DELETE on t1 FOR EACH ROW
1356  INSERT INTO t2 VALUES (OLD.a);
1357
1358FLUSH STATUS;
1359TRUNCATE t1;
1360SHOW STATUS LIKE 'handler_delete';
1361SELECT COUNT(*) FROM t2;
1362
1363INSERT INTO t1 VALUES (1),(2),(3),(4),(5),(6),(7),(8);
1364DELETE FROM t2;
1365
1366FLUSH STATUS;
1367DELETE FROM t1;
1368SHOW STATUS LIKE 'handler_delete';
1369SELECT COUNT(*) FROM t2;
1370
1371DROP TRIGGER trg_t1;
1372DROP TABLE t1,t2;
1373
1374#
1375# Bug #23651 "Server crashes when trigger which uses stored function
1376#             invoked from different connections".
1377#
1378--disable_warnings
1379drop table if exists t1;
1380drop function if exists f1;
1381--enable_warnings
1382create table t1 (i int);
1383create function f1() returns int return 10;
1384create trigger t1_bi before insert on t1 for each row set @a:= f1() + 10;
1385insert into t1 values ();
1386select @a;
1387connection addconroot1;
1388insert into t1 values ();
1389select @a;
1390connection default;
1391drop table t1;
1392drop function f1;
1393
1394#
1395# Bug#23703: DROP TRIGGER needs an IF EXISTS
1396#
1397
1398create table t1(a int, b varchar(50));
1399
1400-- error ER_TRG_DOES_NOT_EXIST
1401drop trigger not_a_trigger;
1402
1403drop trigger if exists not_a_trigger;
1404
1405create trigger t1_bi before insert on t1
1406for each row set NEW.b := "In trigger t1_bi";
1407
1408insert into t1 values (1, "a");
1409drop trigger if exists t1_bi;
1410insert into t1 values (2, "b");
1411drop trigger if exists t1_bi;
1412insert into t1 values (3, "c");
1413
1414select * from t1;
1415
1416drop table t1;
1417
1418#
1419# Bug#25398: crash when a trigger contains a SELECT with
1420#            trigger fields in the select list under DISTINCT
1421#
1422SET sql_mode = 'NO_ENGINE_SUBSTITUTION';
1423CREATE TABLE t1 (
1424  id int NOT NULL DEFAULT '0',
1425  a  varchar(10) NOT NULL,
1426  b  varchar(10),
1427  c  varchar(10),
1428  d  timestamp NOT NULL,
1429  PRIMARY KEY (id, a)
1430);
1431
1432CREATE TABLE t2 (
1433  fubar_id         int unsigned NOT NULL DEFAULT '0',
1434  last_change_time datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
1435  PRIMARY KEY  (fubar_id)
1436);
1437
1438DELIMITER |;
1439
1440CREATE TRIGGER fubar_change
1441  AFTER UPDATE ON t1
1442    FOR EACH ROW
1443      BEGIN
1444        INSERT INTO t2 (fubar_id, last_change_time)
1445          SELECT DISTINCT NEW.id AS fubar_id, NOW() AS last_change_time
1446            FROM t1 WHERE (id = NEW.id) AND (OLD.c != NEW.c)
1447        ON DUPLICATE KEY UPDATE
1448          last_change_time =
1449            IF((fubar_id = NEW.id)AND(OLD.c != NEW.c),NOW(),last_change_time);
1450      END
1451|
1452
1453DELIMITER ;|
1454
1455INSERT INTO t1 (id,a, b,c,d) VALUES
1456 (1,'a','b','c',now()),(2,'a','b','c',now());
1457
1458UPDATE t1 SET c='Bang!' WHERE id=1;
1459
1460SELECT fubar_id FROM t2;
1461
1462DROP TABLE t1,t2;
1463SET sql_mode = default;
1464#
1465# Bug#21285 (Incorrect message error deleting records in a table with a
1466#           trigger for inserting)
1467#
1468
1469--disable_warnings
1470DROP TABLE IF EXISTS bug21825_A;
1471DROP TABLE IF EXISTS bug21825_B;
1472--enable_warnings
1473
1474CREATE TABLE bug21825_A (id int(10));
1475CREATE TABLE bug21825_B (id int(10));
1476
1477delimiter //;
1478
1479CREATE TRIGGER trgA AFTER INSERT ON bug21825_A
1480FOR EACH ROW
1481BEGIN
1482  INSERT INTO bug21825_B (id) values (1);
1483END//
1484delimiter ;//
1485
1486INSERT INTO bug21825_A (id) VALUES (10);
1487INSERT INTO bug21825_A (id) VALUES (20);
1488
1489DROP TABLE bug21825_B;
1490
1491# Must pass, the missing table in the insert trigger should not matter.
1492DELETE FROM bug21825_A WHERE id = 20;
1493
1494DROP TABLE bug21825_A;
1495
1496#
1497# Bug#22580 (DROP TABLE in nested stored procedure causes strange dependancy
1498# error)
1499#
1500
1501--disable_warnings
1502DROP TABLE IF EXISTS bug22580_t1;
1503DROP PROCEDURE IF EXISTS bug22580_proc_1;
1504DROP PROCEDURE IF EXISTS bug22580_proc_2;
1505--enable_warnings
1506
1507CREATE TABLE bug22580_t1 (a INT, b INT);
1508
1509DELIMITER ||;
1510
1511CREATE PROCEDURE bug22580_proc_2()
1512BEGIN
1513  DROP TABLE IF EXISTS bug22580_tmp;
1514  CREATE TEMPORARY TABLE bug22580_tmp (a INT);
1515  DROP TABLE bug22580_tmp;
1516END||
1517
1518CREATE PROCEDURE bug22580_proc_1()
1519BEGIN
1520  CALL bug22580_proc_2();
1521END||
1522
1523CREATE TRIGGER t1bu BEFORE UPDATE ON bug22580_t1
1524FOR EACH ROW
1525BEGIN
1526  CALL bug22580_proc_1();
1527END||
1528
1529DELIMITER ;||
1530
1531# Must pass, the actions of the update trigger should not matter
1532INSERT INTO bug22580_t1 VALUES (1,1);
1533
1534DROP TABLE bug22580_t1;
1535DROP PROCEDURE bug22580_proc_1;
1536DROP PROCEDURE bug22580_proc_2;
1537
1538#
1539# Bug#27006: AFTER UPDATE triggers not fired with INSERT ... ON DUPLICATE
1540#
1541--disable_warnings
1542DROP TRIGGER IF EXISTS trg27006_a_update;
1543DROP TRIGGER IF EXISTS trg27006_a_insert;
1544--enable_warnings
1545
1546CREATE TABLE t1 (
1547  `id` int(10) unsigned NOT NULL auto_increment,
1548  `val` varchar(10) NOT NULL,
1549  PRIMARY KEY  (`id`)
1550);
1551CREATE TABLE t2 like t1;
1552DELIMITER |;
1553
1554CREATE TRIGGER trg27006_a_insert AFTER INSERT ON t1 FOR EACH ROW
1555BEGIN
1556    insert into t2 values (NULL,new.val);
1557END |
1558CREATE TRIGGER trg27006_a_update AFTER UPDATE ON t1 FOR EACH ROW
1559BEGIN
1560    insert into t2 values (NULL,new.val);
1561END |
1562DELIMITER ;|
1563
1564INSERT INTO t1(val) VALUES ('test1'),('test2');
1565SELECT * FROM t1;
1566SELECT * FROM t2;
1567--disable_ps_protocol # Different number of warnings until WL#6570.
1568INSERT INTO t1 VALUES (2,'test2') ON DUPLICATE KEY UPDATE val=VALUES(val);
1569INSERT INTO t1 VALUES (2,'test3') ON DUPLICATE KEY UPDATE val=VALUES(val);
1570INSERT INTO t1 VALUES (3,'test4') ON DUPLICATE KEY UPDATE val=VALUES(val);
1571--enable_ps_protocol
1572SELECT * FROM t1;
1573SELECT * FROM t2;
1574DROP TRIGGER trg27006_a_insert;
1575DROP TRIGGER trg27006_a_update;
1576drop table t1,t2;
1577
1578#
1579# Bug #20903 "Crash when using CREATE TABLE .. SELECT and triggers"
1580#
1581
1582create table t1 (i int);
1583create trigger t1_bi before insert on t1 for each row set new.i = 7;
1584create trigger t1_ai after insert on t1 for each row set @a := 7;
1585create table t2 (j int);
1586insert into t2 values (1), (2);
1587set @a:="";
1588insert into t1 select * from t2;
1589select * from t1;
1590select @a;
1591# Let us check that trigger that involves table also works ok.
1592drop trigger t1_bi;
1593drop trigger t1_ai;
1594create table t3 (isave int);
1595create trigger t1_bi before insert on t1 for each row insert into t3 values (new.i);
1596insert into t1 select * from t2;
1597select * from t1;
1598select * from t3;
1599drop table t1, t2, t3;
1600
1601disconnect addconroot1;
1602disconnect addconroot2;
1603disconnect addconwithoutdb;
1604
1605--echo
1606--echo Bug#28502 Triggers that update another innodb table will block
1607--echo on X lock unnecessarily
1608--echo
1609--echo Ensure we do not open and lock tables for triggers we do not fire.
1610--echo
1611--disable_warnings
1612drop table if exists t1, t2;
1613drop trigger if exists trg_bug28502_au;
1614--enable_warnings
1615
1616create table t1 (id int, count int);
1617create table t2 (id int);
1618delimiter |;
1619
1620create trigger trg_bug28502_au before update on t2
1621for each row
1622begin
1623  if (new.id is not null) then
1624    update t1 set count= count + 1 where id = old.id;
1625  end if;
1626end|
1627
1628delimiter ;|
1629insert into t1 (id, count) values (1, 0);
1630
1631lock table t1 write;
1632
1633--connect (connection_insert, localhost, root, , test, , )
1634connection connection_insert;
1635# Is expected to pass.
1636insert into t2 set id=1;
1637connection default;
1638unlock tables;
1639update t2 set id=1 where id=1;
1640select * from t1;
1641select * from t2;
1642# Will drop the trigger
1643drop table t1, t2;
1644disconnect connection_insert;
1645--echo
1646--echo Additionally, provide test coverage for triggers and
1647--echo all MySQL data changing commands.
1648--echo
1649--disable_warnings
1650drop table if exists t1, t2, t1_op_log;
1651drop view if exists v1;
1652drop trigger if exists trg_bug28502_bi;
1653drop trigger if exists trg_bug28502_ai;
1654drop trigger if exists trg_bug28502_bu;
1655drop trigger if exists trg_bug28502_au;
1656drop trigger if exists trg_bug28502_bd;
1657drop trigger if exists trg_bug28502_ad;
1658--enable_warnings
1659create table t1 (id int primary key auto_increment, operation varchar(255));
1660create table t2 (id int primary key);
1661create table t1_op_log(operation varchar(255));
1662create view v1 as select * from t1;
1663create trigger trg_bug28502_bi before insert on t1
1664for each row
1665  insert into t1_op_log (operation)
1666  values (concat("Before INSERT, new=", new.operation));
1667
1668create trigger trg_bug28502_ai after insert on t1
1669for each row
1670  insert into t1_op_log (operation)
1671  values (concat("After INSERT, new=", new.operation));
1672
1673create trigger trg_bug28502_bu before update on t1
1674for each row
1675  insert into t1_op_log (operation)
1676  values (concat("Before UPDATE, new=", new.operation,
1677                 ", old=", old.operation));
1678
1679create trigger trg_bug28502_au after update on t1
1680for each row
1681  insert into t1_op_log (operation)
1682  values (concat("After UPDATE, new=", new.operation,
1683                 ", old=", old.operation));
1684
1685create trigger trg_bug28502_bd before delete on t1
1686for each row
1687  insert into t1_op_log (operation)
1688  values (concat("Before DELETE, old=", old.operation));
1689
1690create trigger trg_bug28502_ad after delete on t1
1691for each row
1692  insert into t1_op_log (operation)
1693  values (concat("After DELETE, old=", old.operation));
1694
1695insert into t1 (operation) values ("INSERT");
1696
1697set @id=last_insert_id();
1698
1699select * from t1;
1700select * from t1_op_log;
1701truncate t1_op_log;
1702
1703update t1 set operation="UPDATE" where id=@id;
1704
1705select * from t1;
1706select * from t1_op_log;
1707truncate t1_op_log;
1708
1709delete from t1 where id=@id;
1710
1711select * from t1;
1712select * from t1_op_log;
1713truncate t1;
1714truncate t1_op_log;
1715
1716insert into t1 (id, operation) values
1717(NULL, "INSERT ON DUPLICATE KEY UPDATE, inserting a new key")
1718on duplicate key update id=NULL, operation="Should never happen";
1719
1720set @id=last_insert_id();
1721
1722select * from t1;
1723select * from t1_op_log;
1724truncate t1_op_log;
1725
1726insert into t1 (id, operation) values
1727(@id, "INSERT ON DUPLICATE KEY UPDATE, the key value is the same")
1728on duplicate key update id=NULL,
1729operation="INSERT ON DUPLICATE KEY UPDATE, updating the duplicate";
1730
1731select * from t1;
1732select * from t1_op_log;
1733truncate t1;
1734truncate t1_op_log;
1735
1736replace into t1 values (NULL, "REPLACE, inserting a new key");
1737
1738set @id=last_insert_id();
1739
1740select * from t1;
1741select * from t1_op_log;
1742truncate t1_op_log;
1743
1744replace into t1 values (@id, "REPLACE, deleting the duplicate");
1745
1746select * from t1;
1747select * from t1_op_log;
1748truncate t1;
1749truncate t1_op_log;
1750
1751insert into t1
1752select NULL, "CREATE TABLE ... SELECT, inserting a new key";
1753
1754set @id=last_insert_id();
1755
1756select * from t1;
1757select * from t1_op_log;
1758truncate t1_op_log;
1759
1760replace into t1
1761select @id, "CREATE TABLE ... REPLACE SELECT, deleting a duplicate key";
1762
1763select * from t1;
1764select * from t1_op_log;
1765truncate t1;
1766truncate t1_op_log;
1767
1768insert into t1 (id, operation)
1769select NULL, "INSERT ... SELECT, inserting a new key";
1770
1771set @id=last_insert_id();
1772
1773select * from t1;
1774select * from t1_op_log;
1775truncate t1_op_log;
1776
1777insert into t1 (id, operation)
1778select @id,
1779"INSERT ... SELECT ... ON DUPLICATE KEY UPDATE, updating a duplicate"
1780on duplicate key update id=NULL,
1781operation="INSERT ... SELECT ... ON DUPLICATE KEY UPDATE, updating a duplicate";
1782
1783select * from t1;
1784select * from t1_op_log;
1785truncate t1;
1786truncate t1_op_log;
1787
1788replace into t1 (id, operation)
1789select NULL, "REPLACE ... SELECT, inserting a new key";
1790
1791set @id=last_insert_id();
1792
1793select * from t1;
1794select * from t1_op_log;
1795truncate t1_op_log;
1796
1797replace into t1 (id, operation)
1798select @id, "REPLACE ... SELECT, deleting a duplicate";
1799
1800select * from t1;
1801select * from t1_op_log;
1802truncate t1;
1803truncate t1_op_log;
1804
1805insert into t1 (id, operation) values (1, "INSERT for multi-DELETE");
1806insert into t2 (id) values (1);
1807
1808delete t1.*, t2.* from t1, t2 where t1.id=1;
1809
1810select * from t1;
1811select * from t2;
1812select * from t1_op_log;
1813truncate t1;
1814truncate t2;
1815truncate t1_op_log;
1816
1817insert into t1 (id, operation) values (1, "INSERT for multi-UPDATE");
1818insert into t2 (id) values (1);
1819update t1, t2 set t1.id=2, operation="multi-UPDATE" where t1.id=1;
1820update t1, t2
1821set t2.id=3, operation="multi-UPDATE, SET for t2, but the trigger is fired" where t1.id=2;
1822
1823select * from t1;
1824select * from t2;
1825select * from t1_op_log;
1826truncate table t1;
1827truncate table t2;
1828truncate table t1_op_log;
1829
1830--echo
1831--echo Now do the same but use a view instead of the base table.
1832--echo
1833
1834insert into v1 (operation) values ("INSERT");
1835
1836set @id=last_insert_id();
1837
1838select * from t1;
1839select * from t1_op_log;
1840truncate t1_op_log;
1841
1842update v1 set operation="UPDATE" where id=@id;
1843
1844select * from t1;
1845select * from t1_op_log;
1846truncate t1_op_log;
1847
1848delete from v1 where id=@id;
1849
1850select * from t1;
1851select * from t1_op_log;
1852truncate t1;
1853truncate t1_op_log;
1854
1855insert into v1 (id, operation) values
1856(NULL, "INSERT ON DUPLICATE KEY UPDATE, inserting a new key")
1857on duplicate key update id=NULL, operation="Should never happen";
1858
1859set @id=last_insert_id();
1860
1861select * from t1;
1862select * from t1_op_log;
1863truncate t1_op_log;
1864
1865insert into v1 (id, operation) values
1866(@id, "INSERT ON DUPLICATE KEY UPDATE, the key value is the same")
1867on duplicate key update id=NULL,
1868operation="INSERT ON DUPLICATE KEY UPDATE, updating the duplicate";
1869
1870select * from t1;
1871select * from t1_op_log;
1872truncate t1;
1873truncate t1_op_log;
1874
1875replace into v1 values (NULL, "REPLACE, inserting a new key");
1876
1877set @id=last_insert_id();
1878
1879select * from t1;
1880select * from t1_op_log;
1881truncate t1_op_log;
1882
1883replace into v1 values (@id, "REPLACE, deleting the duplicate");
1884
1885select * from t1;
1886select * from t1_op_log;
1887truncate t1;
1888truncate t1_op_log;
1889
1890insert into v1
1891select NULL, "CREATE TABLE ... SELECT, inserting a new key";
1892
1893set @id=last_insert_id();
1894
1895select * from t1;
1896select * from t1_op_log;
1897truncate t1_op_log;
1898
1899replace into v1
1900select @id, "CREATE TABLE ... REPLACE SELECT, deleting a duplicate key";
1901
1902select * from t1;
1903select * from t1_op_log;
1904truncate t1;
1905truncate t1_op_log;
1906
1907insert into v1 (id, operation)
1908select NULL, "INSERT ... SELECT, inserting a new key";
1909
1910set @id=last_insert_id();
1911
1912select * from t1;
1913select * from t1_op_log;
1914truncate t1_op_log;
1915
1916insert into v1 (id, operation)
1917select @id,
1918"INSERT ... SELECT ... ON DUPLICATE KEY UPDATE, updating a duplicate"
1919on duplicate key update id=NULL,
1920operation="INSERT ... SELECT ... ON DUPLICATE KEY UPDATE, updating a duplicate";
1921
1922select * from t1;
1923select * from t1_op_log;
1924truncate t1;
1925truncate t1_op_log;
1926
1927replace into v1 (id, operation)
1928select NULL, "REPLACE ... SELECT, inserting a new key";
1929
1930set @id=last_insert_id();
1931
1932select * from t1;
1933select * from t1_op_log;
1934truncate t1_op_log;
1935
1936replace into v1 (id, operation)
1937select @id, "REPLACE ... SELECT, deleting a duplicate";
1938
1939select * from t1;
1940select * from t1_op_log;
1941truncate t1;
1942truncate t1_op_log;
1943
1944insert into v1 (id, operation) values (1, "INSERT for multi-DELETE");
1945insert into t2 (id) values (1);
1946
1947delete v1.*, t2.* from v1, t2 where v1.id=1;
1948
1949select * from t1;
1950select * from t2;
1951select * from t1_op_log;
1952truncate t1;
1953truncate t2;
1954truncate t1_op_log;
1955
1956insert into v1 (id, operation) values (1, "INSERT for multi-UPDATE");
1957insert into t2 (id) values (1);
1958update v1, t2 set v1.id=2, operation="multi-UPDATE" where v1.id=1;
1959update v1, t2
1960set t2.id=3, operation="multi-UPDATE, SET for t2, but the trigger is fired" where v1.id=2;
1961
1962select * from t1;
1963select * from t2;
1964select * from t1_op_log;
1965
1966drop view v1;
1967drop table t1, t2, t1_op_log;
1968
1969#
1970# TODO: test LOAD DATA INFILE
1971#
1972--echo
1973--echo Bug#27248 Triggers: error if insert affects temporary table
1974--echo
1975--echo The bug was fixed by the fix for Bug#26141
1976--echo
1977--disable_warnings
1978drop table if exists t1;
1979drop temporary table if exists t2;
1980--enable_warnings
1981create table t1 (s1 int);
1982create temporary table t2 (s1 int);
1983create trigger t1_bi before insert on t1 for each row insert into t2 values (0);
1984create trigger t1_bd before delete on t1 for each row delete from t2;
1985insert into t1 values (0);
1986insert into t1 values (0);
1987select * from t1;
1988select * from t2;
1989delete from t1;
1990select * from t1;
1991select * from t2;
1992drop table t1;
1993drop temporary table t2;
1994
1995--echo #------------------------------------------------------------------------
1996--echo # Bug#39953 Triggers are not working properly with multi table updates
1997--echo #------------------------------------------------------------------------
1998
1999--disable_warnings
2000DROP TABLE IF EXISTS t1;
2001DROP TRIGGER IF EXISTS t_insert;
2002DROP TABLE IF EXISTS t2;
2003--enable_warnings
2004
2005CREATE TABLE t1 (a int, date_insert timestamp, PRIMARY KEY (a));
2006INSERT INTO t1 (a) VALUES (2),(5);
2007CREATE TABLE t2 (a int, b int, PRIMARY KEY (a));
2008DELIMITER |;
2009CREATE TRIGGER t_insert AFTER INSERT ON t2 FOR EACH ROW BEGIN UPDATE t1,t2 SET
2010date_insert=NOW() WHERE t1.a=t2.b AND t2.a=NEW.a; END |
2011DELIMITER ;|
2012INSERT INTO t2 (a,b) VALUES (1,2);
2013
2014DROP TRIGGER t_insert;
2015
2016DELIMITER |;
2017CREATE TRIGGER t_insert AFTER INSERT ON t2 FOR EACH ROW BEGIN UPDATE t1,t2 SET
2018date_insert=NOW(),b=b+1 WHERE t1.a=t2.b AND t2.a=NEW.a; END |
2019DELIMITER ;|
2020--error ER_CANT_UPDATE_USED_TABLE_IN_SF_OR_TRG
2021INSERT INTO t2 (a,b) VALUES (3,5);
2022
2023DROP TABLE t1;
2024DROP TRIGGER t_insert;
2025DROP TABLE t2;
2026
2027--echo End of 5.0 tests
2028
2029#
2030# Bug#25411 (trigger code truncated)
2031#
2032
2033--disable_warnings
2034drop table if exists table_25411_a;
2035drop table if exists table_25411_b;
2036--enable_warnings
2037
2038create table table_25411_a(a int);
2039create table table_25411_b(b int);
2040
2041create trigger trg_25411a_ai after insert on table_25411_a
2042for each row
2043  insert into table_25411_b select new.*;
2044
2045select * from table_25411_a;
2046
2047--error ER_BAD_TABLE_ERROR
2048insert into table_25411_a values (1);
2049
2050select * from table_25411_a;
2051
2052drop table table_25411_a;
2053drop table table_25411_b;
2054
2055#
2056# Bug #31866: MySQL Server crashes on SHOW CREATE TRIGGER statement
2057#
2058
2059--error ER_TRG_DOES_NOT_EXIST
2060SHOW CREATE TRIGGER trg;
2061
2062#
2063# Bug#23713 LOCK TABLES + CREATE TRIGGER + FLUSH TABLES WITH READ LOCK = deadlock
2064#
2065# Test of trigger creation and removal under LOCK TABLES
2066#
2067
2068create table t1 (i int, j int);
2069
2070create trigger t1_bi before insert on t1 for each row begin end;
2071--error ER_TRG_ALREADY_EXISTS
2072create trigger t1_bi before insert on t1 for each row begin end;
2073drop trigger t1_bi;
2074--error ER_TRG_DOES_NOT_EXIST
2075drop trigger t1_bi;
2076
2077lock tables t1 read;
2078--error ER_TABLE_NOT_LOCKED_FOR_WRITE
2079create trigger t1_bi before insert on t1 for each row begin end;
2080--error ER_TABLE_NOT_LOCKED_FOR_WRITE
2081create trigger t1_bi before insert on t1 for each row begin end;
2082--error ER_TRG_DOES_NOT_EXIST
2083drop trigger t1_bi;
2084unlock tables;
2085
2086create trigger t1_bi before insert on t1 for each row begin end;
2087lock tables t1 read;
2088--error ER_TABLE_NOT_LOCKED_FOR_WRITE
2089create trigger t1_bi before insert on t1 for each row begin end;
2090--error ER_TABLE_NOT_LOCKED_FOR_WRITE
2091drop trigger t1_bi;
2092unlock tables;
2093drop trigger t1_bi;
2094
2095lock tables t1 write;
2096create trigger b1_bi before insert on t1 for each row set new.i = new.i + 10;
2097insert into t1 values (10, 10);
2098drop trigger b1_bi;
2099insert into t1 values (10, 10);
2100select * from t1;
2101unlock tables;
2102
2103drop table t1;
2104
2105#
2106# Bug#23771 AFTER UPDATE trigger not invoked when there are no changes of the data
2107#
2108
2109--disable_warnings
2110drop table if exists t1, t2;
2111drop trigger if exists trg1;
2112drop trigger if exists trg2;
2113--enable_warnings
2114create table t1 (a int);
2115create table t2 (b int);
2116create trigger trg1 after update on t1 for each row set @a= @a+1;
2117create trigger trg2 after update on t2 for each row set @b= @b+1;
2118insert into t1 values (1), (2), (3);
2119insert into t2 values (1), (2), (3);
2120set @a= 0;
2121set @b= 0;
2122update t1, t2 set t1.a= t1.a, t2.b= t2.b;
2123select @a, @b;
2124update t1, t2 set t1.a= t2.b, t2.b= t1.a;
2125select @a, @b;
2126update t1 set a= a;
2127select @a, @b;
2128update t2 set b= b;
2129select @a, @b;
2130update t1 set a= 1;
2131select @a, @b;
2132update t2 set b= 1;
2133select @a, @b;
2134drop trigger trg1;
2135drop trigger trg2;
2136drop table t1, t2;
2137
2138#
2139# Bug#44653: Server crash noticed when executing random queries with partitions.
2140#
2141CREATE TABLE t1 ( a INT, b INT );
2142CREATE TABLE t2 ( a INT AUTO_INCREMENT KEY, b INT );
2143
2144INSERT INTO t1 (a) VALUES (1);
2145
2146delimiter //;
2147CREATE TRIGGER tr1
2148BEFORE INSERT ON t2
2149FOR EACH ROW
2150BEGIN
2151  UPDATE a_nonextisting_table SET a = 1;
2152END//
2153delimiter ;//
2154
2155--disable_abort_on_error ONCE
2156CREATE TABLE IF NOT EXISTS t2 ( a INT, b INT ) SELECT a, b FROM t1;
2157
2158# Caused failed assertion
2159SELECT * FROM t2;
2160
2161DROP TABLE t1, t2;
2162
2163--echo #
2164--echo # Bug#51650 crash with user variables and triggers
2165--echo #
2166
2167--disable_warnings
2168DROP TRIGGER IF EXISTS trg1;
2169DROP TABLE IF EXISTS t1, t2;
2170--enable_warnings
2171
2172CREATE TABLE t1 (b VARCHAR(50) NOT NULL);
2173CREATE TABLE t2 (a VARCHAR(10) NOT NULL DEFAULT '');
2174
2175delimiter //;
2176CREATE TRIGGER trg1 AFTER INSERT ON t2
2177FOR EACH ROW BEGIN
2178  SELECT 1 FROM t1 c WHERE
2179    (@bug51650 IS NULL OR @bug51650 != c.b) AND c.b = NEW.a LIMIT 1 INTO @foo;
2180END//
2181delimiter ;//
2182
2183SET @bug51650 = 1;
2184INSERT IGNORE INTO t2 VALUES();
2185INSERT IGNORE INTO t1 SET b = '777';
2186INSERT IGNORE INTO t2 SET a = '111';
2187SET @bug51650 = 1;
2188INSERT IGNORE INTO t2 SET a = '777';
2189
2190DROP TRIGGER trg1;
2191DROP TABLE t1, t2;
2192
2193--echo #
2194--echo # Bug#50755: Crash if stored routine def contains version comments
2195--echo #
2196--echo # With WL#9494, the SHOW TRIGGERS no more parses
2197--echo # the underlaying trigger, and hence it would not
2198--echo # report parse error. This means that the code
2199--echo # causing this issue is removed now with WL#9494.
2200--echo # This test case is kept for reference.
2201--echo #
2202
2203--disable_warnings
2204DROP DATABASE IF EXISTS db1;
2205DROP TRIGGER IF EXISTS trg1;
2206DROP TABLE IF EXISTS t1, t2;
2207--enable_warnings
2208
2209CREATE DATABASE db1;
2210USE db1;
2211
2212CREATE TABLE t1 (b INT);
2213CREATE TABLE t2 (a INT);
2214
2215CREATE TRIGGER trg1 BEFORE INSERT ON t2 FOR EACH ROW INSERT/*!INTO*/t1 VALUES (1);
2216--replace_column 6 #
2217SHOW TRIGGERS IN db1;
2218--error ER_PARSE_ERROR
2219INSERT INTO t2 VALUES (1);
2220SELECT * FROM t1;
2221
2222DROP DATABASE db1;
2223USE test;
2224
2225--echo End of 5.1 tests.
2226
2227
2228--echo #
2229--echo # Bug#34453 Can't change size of file (Errcode: 1224)
2230--echo #
2231
2232--disable_warnings
2233DROP TRIGGER IF EXISTS t1_bi;
2234DROP TRIGGER IF EXISTS t1_bd;
2235DROP TABLE IF EXISTS t1;
2236DROP TEMPORARY TABLE IF EXISTS t2;
2237--enable_warnings
2238
2239CREATE TABLE t1 (s1 INT);
2240CREATE TEMPORARY TABLE t2 (s1 INT);
2241CREATE TRIGGER t1_bi BEFORE INSERT ON t1 FOR EACH ROW INSERT INTO t2 VALUES (0);
2242CREATE TRIGGER t1_bd BEFORE DELETE ON t1 FOR EACH ROW DELETE FROM t2;
2243INSERT INTO t1 VALUES (0);
2244INSERT INTO t1 VALUES (0);
2245SELECT * FROM t1;
2246SELECT * FROM t2;
2247-- echo # Reported to give ERROR 14 (HY000):
2248-- echo # Can't change size of file (Errcode: 1224)
2249-- echo # on Windows
2250DELETE FROM t1;
2251
2252DROP TABLE t1;
2253DROP TEMPORARY TABLE t2;
2254
2255#
2256# Bug#36649: Condition area is not properly cleaned up after stored routine invocation
2257#
2258SET sql_mode = 'NO_ENGINE_SUBSTITUTION';
2259--disable_warnings
2260DROP TRIGGER IF EXISTS trg1;
2261DROP TABLE IF EXISTS t1;
2262--enable_warnings
2263
2264CREATE TABLE t1 (a INT);
2265
2266delimiter |;
2267CREATE TRIGGER trg1 BEFORE INSERT ON t1 FOR EACH ROW
2268BEGIN
2269  DECLARE a CHAR;
2270  SELECT 'ab' INTO a;
2271  SELECT 'ab' INTO a;
2272  SELECT 'a' INTO a;
2273END|
2274delimiter ;|
2275
2276INSERT INTO t1 VALUES (1);
2277
2278DROP TRIGGER trg1;
2279DROP TABLE t1;
2280
2281#
2282# Successive trigger actuations
2283#
2284
2285--disable_warnings
2286DROP TRIGGER IF EXISTS trg1;
2287DROP TRIGGER IF EXISTS trg2;
2288DROP TABLE IF EXISTS t1;
2289--enable_warnings
2290
2291CREATE TABLE t1 (a INT);
2292
2293delimiter |;
2294
2295CREATE TRIGGER trg1 BEFORE INSERT ON t1 FOR EACH ROW
2296BEGIN
2297  DECLARE trg1 CHAR;
2298  SELECT 'ab' INTO trg1;
2299END|
2300
2301CREATE TRIGGER trg2 AFTER INSERT ON t1 FOR EACH ROW
2302BEGIN
2303  DECLARE trg2 CHAR;
2304  SELECT 'ab' INTO trg2;
2305END|
2306
2307delimiter ;|
2308
2309INSERT INTO t1 VALUES (0);
2310SELECT * FROM t1;
2311SHOW WARNINGS;
2312INSERT INTO t1 VALUES (1),(2);
2313
2314DROP TRIGGER trg1;
2315DROP TRIGGER trg2;
2316DROP TABLE t1;
2317SET sql_mode = default;
2318
2319--echo #
2320--echo # Bug #46747 "Crash in MDL_ticket::upgrade_shared_lock_to_exclusive
2321--echo #             on TRIGGER + TEMP table".
2322--echo #
2323
2324--disable_warnings
2325drop trigger if exists t1_bi;
2326drop temporary table if exists t1;
2327drop table if exists t1;
2328--enable_warnings
2329
2330create table t1 (i int);
2331create trigger t1_bi before insert on t1 for each row set @a:=1;
2332--echo # Create temporary table which shadows base table with trigger.
2333create temporary table t1 (j int);
2334--echo # Dropping of trigger should succeed.
2335drop trigger t1_bi;
2336select trigger_name from information_schema.triggers
2337  where event_object_schema = 'test' and event_object_table = 't1';
2338--echo # Clean-up.
2339drop temporary table t1;
2340drop table t1;
2341
2342
2343--echo
2344--echo #
2345--echo # Bug #12362125: SP INOUT HANDLING IS BROKEN FOR TEXT TYPE.
2346--echo #
2347
2348CREATE TABLE t1(c TEXT);
2349
2350delimiter |;
2351CREATE TRIGGER t1_bi BEFORE INSERT ON t1 FOR EACH ROW
2352BEGIN
2353  DECLARE v TEXT;
2354  SET v = 'aaa';
2355  SET NEW.c = v;
2356END|
2357delimiter ;|
2358
2359INSERT INTO t1 VALUES('qazwsxedc');
2360
2361SELECT c FROM t1;
2362
2363DROP TABLE t1;
2364
2365--echo
2366--echo End of 5.5 tests.
2367--echo
2368
2369
2370--echo #
2371--echo # Bug#34432 Wrong lock type passed to the engine if pre-locking +
2372--echo #           multi-update in a trigger
2373--echo #
2374--disable_warnings
2375DROP TABLE IF EXISTS t1, t2, t3;
2376DROP TRIGGER IF EXISTS t2_ai;
2377--enable_warnings
2378CREATE TABLE t2
2379       (
2380         value CHAR(30),
2381         domain_id INT,
2382         mailaccount_id INT,
2383         program CHAR(30),
2384         keey CHAR(30),
2385         PRIMARY KEY(domain_id)
2386       );
2387CREATE TABLE t3
2388       (
2389         value CHAR(30),
2390         domain_id INT,
2391         mailaccount_id INT,
2392         program CHAR(30),
2393         keey CHAR(30),
2394         PRIMARY KEY(domain_id)
2395       );
2396CREATE TABLE t1 (id INT,domain CHAR(30),PRIMARY KEY(id));
2397
2398delimiter |;
2399CREATE TRIGGER t2_ai AFTER INSERT ON t2 FOR EACH ROW
2400  UPDATE t3 ms, t1 d SET ms.value='No'
2401  WHERE ms.domain_id =
2402    (SELECT max(id) FROM t1 WHERE domain='example.com')
2403  AND ms.mailaccount_id IS NULL
2404  AND ms.program='spamfilter'
2405  AND ms.keey='scan_incoming';
2406|
2407delimiter ;|
2408
2409INSERT INTO t1 VALUES (1, 'example.com'),
2410                      (2, 'mysql.com'),
2411                      (3, 'earthmotherwear.com'),
2412                      (4, 'yahoo.com'),
2413                      (5, 'example.com');
2414INSERT INTO t2 VALUES ('Yes', 1, NULL, 'spamfilter','scan_incoming');
2415DROP TRIGGER t2_ai;
2416DROP TABLE t1, t2, t3;
2417
2418--echo #
2419--echo # Bug#14493938 - CREATE TEMPORARY TABLE INSIDE TRIGGER -- CRASH (DEBUG ONLY)
2420--echo #
2421
2422CREATE TABLE t1 (a INT, b INT DEFAULT 150);
2423
2424delimiter |;
2425CREATE TRIGGER t1_bi BEFORE INSERT ON t1
2426FOR EACH ROW
2427BEGIN
2428  CREATE TEMPORARY TABLE t2 AS SELECT NEW.a, NEW.b;
2429  INSERT INTO t2(a) VALUES (10);
2430  INSERT INTO t2 VALUES (100, 500);
2431  INSERT INTO t2(a) VALUES (1000);
2432END
2433|
2434delimiter ;|
2435
2436INSERT INTO t1 VALUES (1, 2);
2437SELECT * FROM t2;
2438
2439DROP TABLE t1;
2440DROP TEMPORARY TABLE t2;
2441
2442--echo #
2443--echo # Bug#15985318 - ASSERTION FAILED: ! thd->in_sub_stmt with certain
2444--echo #                                    commands in triggers
2445--echo #
2446
2447CREATE TABLE t1(a INT);
2448
2449--error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG
2450CREATE TRIGGER t1_au AFTER UPDATE ON t1 FOR EACH ROW START SLAVE;
2451
2452--error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG
2453CREATE TRIGGER t1_au AFTER UPDATE ON t1 FOR EACH ROW STOP SLAVE;
2454
2455--error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG
2456CREATE TRIGGER t1_au AFTER UPDATE ON t1 FOR EACH ROW
2457CREATE SERVER s FOREIGN DATA WRAPPER mysql OPTIONS (USER 'Remote',
2458                                                    HOST '192.168.1.106',
2459                                                    DATABASE 'test');
2460
2461--error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG
2462CREATE TRIGGER t1_bu BEFORE UPDATE ON t1 FOR EACH ROW
2463ALTER SERVER s OPTIONS (password '1');
2464
2465--error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG
2466CREATE TRIGGER tr1 AFTER UPDATE ON t1 FOR EACH ROW
2467DROP SERVER IF EXISTS s;
2468
2469CREATE DATABASE db1;
2470
2471--error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG
2472CREATE TRIGGER tr1 AFTER UPDATE ON t1 FOR EACH ROW
2473ALTER DATABASE db1 CHARACTER SET latin1;
2474
2475DROP DATABASE db1;
2476
2477CREATE USER 'u1'@'localhost' IDENTIFIED BY 'pass';
2478
2479--error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG
2480CREATE TRIGGER tr1 AFTER UPDATE ON t1 FOR EACH ROW
2481ALTER USER 'u1'@'localhost' PASSWORD EXPIRE;
2482
2483DROP USER 'u1'@'localhost';
2484
2485--error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG
2486CREATE TRIGGER tr1 AFTER UPDATE ON t1 FOR EACH ROW
2487CHANGE MASTER TO MASTER_SSL = 0;
2488
2489DROP TABLE t1;
2490
2491--echo #
2492--echo # Bug#17864349 - CHANGING TRUNCATE TABLE TO DROP TABLE & CREATE TABLE
2493--echo # MAKES TRIGGER.TEST FAIL
2494--echo #
2495
2496--echo # The following tests check for non in-place update (i.e. using temporary table)
2497--echo # of column with different datatypes
2498
2499SET @save_sql_mode= @@sql_mode;
2500SET sql_mode= 'traditional';
2501
2502let $column1_type_name = INT;
2503let $column2_type_name = INT;
2504let $column2_value = 3;
2505--source include/trigger_17864349.inc
2506
2507let $column1_type_name = TINYINT;
2508let $column2_type_name = TINYINT;
2509let $column2_value = 3;
2510--source include/trigger_17864349.inc
2511
2512let $column1_type_name = BOOL;
2513let $column2_type_name = BOOL;
2514let $column2_value = TRUE;
2515--source include/trigger_17864349.inc
2516
2517let $column1_type_name = SMALLINT;
2518let $column2_type_name = SMALLINT;
2519let $column2_value = 3;
2520--source include/trigger_17864349.inc
2521
2522let $column1_type_name = BIGINT;
2523let $column2_type_name = BIGINT;
2524let $column2_value = 3;
2525--source include/trigger_17864349.inc
2526
2527let $column1_type_name = DECIMAL;
2528let $column2_type_name = DECIMAL;
2529let $column2_value = 3;
2530--source include/trigger_17864349.inc
2531
2532let $column1_type_name = FLOAT;
2533let $column2_type_name = FLOAT;
2534let $column2_value = 3;
2535--source include/trigger_17864349.inc
2536
2537let $column1_type_name = DOUBLE;
2538let $column2_type_name = DOUBLE;
2539let $column2_value = 3;
2540--source include/trigger_17864349.inc
2541
2542let $column1_type_name = BIT;
2543let $column2_type_name = BIT;
2544let $column2_value = 1;
2545--source include/trigger_17864349.inc
2546
2547let $column1_type_name = ENUM('a', 'b', 'c');
2548let $column2_type_name = ENUM('a', 'b', 'c');
2549let $column2_value = 'b';
2550--source include/trigger_17864349.inc
2551
2552let $column1_type_name = SET('a', 'b', 'c');
2553let $column2_type_name = SET('a', 'b', 'c');
2554let $column2_value = 'b';
2555--source include/trigger_17864349.inc
2556
2557let $column1_type_name = VARBINARY(10);
2558let $column2_type_name = VARBINARY(10);
2559let $column2_value = 'binary';
2560--source include/trigger_17864349.inc
2561
2562let $column1_type_name = BINARY(10);
2563let $column2_type_name = BINARY(10);
2564let $column2_value = 'binary';
2565--source include/trigger_17864349.inc
2566
2567let $column1_type_name = TINYTEXT;
2568let $column2_type_name = TINYTEXT;
2569let $column2_value = 'text';
2570--source include/trigger_17864349.inc
2571
2572let $column1_type_name = TEXT(10);
2573let $column2_type_name = TEXT(10);
2574let $column2_value = 'text';
2575--source include/trigger_17864349.inc
2576
2577let $column1_type_name = BLOB;
2578let $column2_type_name = BLOB;
2579let $column2_value = 'binary';
2580--source include/trigger_17864349.inc
2581
2582let $column1_type_name = VARCHAR(5);
2583let $column2_type_name = INT;
2584let $column2_value = 3;
2585--source include/trigger_17864349.inc
2586
2587let $column1_type_name = INT;
2588let $column2_type_name = VARCHAR(5);
2589let $column2_value = 'str';
2590--source include/trigger_17864349.inc
2591
2592let $column1_type_name = VARCHAR(15);
2593let $column2_type_name = VARCHAR(5);
2594let $column2_value = 'str';
2595--source include/trigger_17864349.inc
2596
2597let $column1_type_name = VARCHAR(15);
2598let $column2_type_name = BLOB;
2599let $column2_value = 'str';
2600--source include/trigger_17864349.inc
2601
2602let $column1_type_name = TEXT(20);
2603let $column2_type_name = TEXT(10);
2604let $column2_value = 'text';
2605--source include/trigger_17864349.inc
2606
2607SET sql_mode= @save_sql_mode;
2608
2609# End of tests for Bug#17864349
2610--echo #
2611--echo # Bug#18596756 - FAILED PREPARING OF TRIGGER ON TRUNCATED TABLES CAUSE
2612--echo #                ERROR 1054.
2613--echo #
2614
2615CREATE TABLE t1(id INT);
2616CREATE TABLE t2(id INT);
2617
2618CREATE TRIGGER trigger1 BEFORE INSERT ON t1 FOR EACH ROW
2619  SET NEW.id= (SELECT * FROM t2);
2620
2621INSERT INTO t2 VALUES(0);
2622INSERT INTO t1 VALUES(0);
2623
2624TRUNCATE TABLE t2;
2625
2626INSERT INTO t2 VALUES(0);
2627#Without the fix, trigger fired for following INSERT operation reports
2628#"ER_BAD_FIELD_ERROR" error.
2629INSERT INTO t1 VALUES(0);
2630
2631DROP TABLE t2;
2632
2633--error ER_NO_SUCH_TABLE
2634#Without the fix, trigger fired for following INSERT operation reports
2635#"ER_BAD_FIELD_ERROR" error also here.
2636INSERT INTO t1 VALUES(0);
2637
2638DROP TABLE t1;
2639
2640--echo #
2641--echo # Bug#16522924 : UPDATE TRIGGER INVOKED WHEN UPDATE IGNORE MEANS
2642--echo #                      THAT NO UPDATE IS PERFORMED
2643--echo #
2644CREATE TABLE t1 (a INT PRIMARY KEY);
2645CREATE TABLE t2 (after_update CHAR(50));
2646CREATE TABLE t3(b INT PRIMARY KEY);
2647INSERT INTO t1 VALUES (1), (2);
2648INSERT INTO t3 VALUES (1);
2649DELIMITER |;
2650CREATE TRIGGER post_update_t1 AFTER UPDATE ON t1
2651FOR EACH ROW BEGIN
2652  INSERT INTO t2 VALUES("POST UPDATE TRIGGER FOR UPDATE IGNORE ON t1 FIRED");
2653END|
2654DELIMITER ;|
2655UPDATE IGNORE t1 SET a=2 WHERE a=1;
2656SELECT * FROM t2;
2657UPDATE IGNORE t1,t3 SET t1.a=2 WHERE t1.a=1;
2658SELECT * FROM t2;
2659UPDATE IGNORE t3,t1 SET t1.a=2 WHERE t1.a=1;
2660SELECT * FROM t1;
2661SELECT * FROM t2;
2662DROP TRIGGER post_update_t1;
2663DROP TABLE t1,t2,t3;
2664
2665--echo #
2666--echo # WL#9262: All system tables should support 32 character length user names
2667--echo #
2668
2669CREATE USER user_name_robert_golebiowski@oh_my_gosh_this_is_a_long_hostname_look_at_it_it_has_60_char;
2670CREATE TABLE test.silly_one (ID INT);
2671
2672CREATE DEFINER=user_name_robert_golebiowski@oh_my_gosh_this_is_a_long_hostname_look_at_it_it_has_60_char TRIGGER test.silly_trigger BEFORE INSERT ON test.silly_one FOR EACH ROW SET @x=1;
2673
2674--replace_column 6 #
2675SHOW TRIGGERS FROM test LIKE 'silly_one';
2676
2677SELECT DEFINER FROM information_schema.triggers WHERE TRIGGER_NAME='silly_trigger';
2678
2679DROP USER user_name_robert_golebiowski@oh_my_gosh_this_is_a_long_hostname_look_at_it_it_has_60_char;
2680DROP TRIGGER test.silly_trigger;
2681DROP TABLE test.silly_one;
2682
2683--echo #
2684--echo # Bug #22512899 - DROP TRIGGER AFTER RENAME TABLE RESULTS IN ERROR CODE 1146: TABLE DOESN'T EXIST
2685--echo #
2686
2687CREATE TABLE t1 (a INT) ENGINE=InnoDB;
2688CREATE TRIGGER t1_bi BEFORE INSERT ON t1 FOR EACH ROW BEGIN END;
2689
2690RENAME TABLE t1 TO t1Renamed;
2691
2692DROP TRIGGER t1_bi;
2693DROP TABLE t1Renamed;
2694
2695--echo End of 5.7 tests.
2696
2697--echo #
2698--echo # Bug #23624693 - USING SHOW CREATE TRIGGER IN A TRANSACTION CRASHES THE SERVER
2699--echo #
2700
2701--error ER_BAD_DB_ERROR
2702SHOW CREATE TRIGGER non_existence_db.some_trigger;
2703
2704--error ER_BAD_DB_ERROR
2705CREATE TRIGGER non_existent_db.trg1 BEFORE INSERT ON t1 FOR EACH ROW BEGIN END;
2706
2707--error ER_BAD_DB_ERROR
2708DROP TRIGGER non_existent_db.trg1;
2709
2710--echo #
2711--echo # Bug#24449174 - DROPPING A PARTITION DROPS THE TRIGGER ON 8.0.0
2712--echo #
2713
2714CREATE TABLE t1 (val INT NOT NULL) ENGINE=InnoDB
2715PARTITION BY LIST(val) (
2716  PARTITION p1 VALUES IN (1,2,3),
2717  PARTITION p2 VALUES IN (4,5)
2718);
2719
2720CREATE TRIGGER t1_bi BEFORE INSERT ON t1 FOR EACH ROW BEGIN END;
2721
2722--replace_column 7 #
2723SHOW CREATE TRIGGER t1_bi;
2724
2725ALTER TABLE t1 DROP PARTITION p1;
2726
2727--replace_column 7 #
2728SHOW CREATE TRIGGER t1_bi;
2729
2730DROP TABLE t1;
2731
2732--echo #
2733--echo # Bug#24482919 -- SHOW TRIGGER OUTPUT INCONSISTENCY ON WINDOWS / LINUX ON 8.0.0
2734--echo #
2735CREATE TABLE t1 (a INT);
2736CREATE TRIGGER trg1a BEFORE INSERT ON t1 FOR EACH ROW BEGIN END;
2737CREATE TRIGGER trg1b AFTER INSERT ON t1 FOR EACH ROW BEGIN END;
2738CREATE TRIGGER trg1c BEFORE UPDATE ON t1 FOR EACH ROW BEGIN END;
2739CREATE TRIGGER trg1d AFTER UPDATE ON t1 FOR EACH ROW BEGIN END;
2740CREATE TRIGGER trg1e BEFORE DELETE ON t1 FOR EACH ROW BEGIN END;
2741CREATE TRIGGER trg1f AFTER DELETE ON t1 FOR EACH ROW BEGIN END;
2742CREATE TRIGGER trg1a2 BEFORE INSERT ON t1 FOR EACH ROW BEGIN END;
2743CREATE TRIGGER trg1b2 AFTER INSERT ON t1 FOR EACH ROW BEGIN END;
2744CREATE TRIGGER trg1c2 BEFORE UPDATE ON t1 FOR EACH ROW BEGIN END;
2745CREATE TRIGGER trg1d2 AFTER UPDATE ON t1 FOR EACH ROW BEGIN END;
2746CREATE TRIGGER trg1f2 AFTER DELETE ON t1 FOR EACH ROW BEGIN END;
2747CREATE TRIGGER trg1a0 BEFORE INSERT ON t1 FOR EACH ROW PRECEDES trg1a BEGIN END;
2748CREATE TRIGGER trg1a3 BEFORE INSERT ON t1 FOR EACH ROW FOLLOWS trg1a2 BEGIN END;
2749CREATE TRIGGER trg1b0 AFTER INSERT ON t1 FOR EACH ROW PRECEDES trg1b BEGIN END;
2750CREATE TRIGGER trg1b3 AFTER INSERT ON t1 FOR EACH ROW FOLLOWS trg1b2 BEGIN END;
2751CREATE TRIGGER trg1c0 BEFORE UPDATE ON t1 FOR EACH ROW PRECEDES trg1c BEGIN END;
2752CREATE TRIGGER trg1c3 BEFORE UPDATE ON t1 FOR EACH ROW FOLLOWS trg1c2 BEGIN END;
2753SELECT TRIGGER_NAME FROM information_schema.triggers WHERE TRIGGER_NAME LIKE 'trg1%' ORDER BY TRIGGER_NAME;
2754--replace_column 6 # 7 #
2755SHOW TRIGGERS;
2756DROP TABLE t1;
2757
2758
2759--echo #
2760--echo # Bug#25581925: 'MDL_CHECKER::IS_READ_LOCKED(M_THD, *OBJECT)' AT
2761--echo #               DD::CACHE::DICTIONARY_CLIENT:
2762--echo #
2763
2764CREATE TABLE t1(a INT);
2765CREATE SCHEMA s1;
2766CREATE VIEW s1.v1 AS SELECT * FROM t1;
2767CREATE TRIGGER trg1 BEFORE INSERT ON t1 FOR EACH ROW BEGIN END;
2768LOCK TABLE s1.v1 WRITE;
2769# This used to cause an assert.
2770DROP TRIGGER trg1;
2771UNLOCK TABLES;
2772DROP VIEW s1.v1;
2773DROP TABLE t1;
2774DROP SCHEMA s1;
2775
2776###########################################################################
2777#
2778# Tests for WL#2818:
2779#   - Check that triggers are executed under the authorization of the definer.
2780#   - Check DEFINER clause of CREATE TRIGGER statement;
2781#     - Check that SUPER privilege required to create a trigger with different
2782#       definer.
2783#     - Check that if the user specified as DEFINER does not exist, a warning
2784#       is emitted.
2785#     - Check that the definer of a trigger does not exist, the trigger will
2786#       not be activated.
2787#   - Check that SHOW TRIGGERS statement provides "Definer" column.
2788#   - Check that if trigger contains NEW/OLD variables, the definer must have
2789#     SELECT privilege on the subject table (aka BUG#15166/BUG#15196).
2790#
2791#  Let's also check that user name part of definer can contain '@' symbol (to
2792#  check that triggers are not affected by BUG#13310 "incorrect user parsing
2793#  by SP").
2794#
2795###########################################################################
2796
2797#
2798# Prepare environment.
2799#
2800
2801DELETE FROM mysql.user WHERE User LIKE 'mysqltest_%';
2802DELETE FROM mysql.db WHERE User LIKE 'mysqltest_%';
2803DELETE FROM mysql.tables_priv WHERE User LIKE 'mysqltest_%';
2804DELETE FROM mysql.columns_priv WHERE User LIKE 'mysqltest_%';
2805FLUSH PRIVILEGES;
2806
2807--disable_warnings
2808DROP DATABASE IF EXISTS mysqltest_db1;
2809--enable_warnings
2810
2811CREATE DATABASE mysqltest_db1;
2812
2813CREATE USER mysqltest_dfn@localhost;
2814CREATE USER mysqltest_inv@localhost;
2815
2816GRANT CREATE ON mysqltest_db1.* TO mysqltest_dfn@localhost;
2817
2818--connect (wl2818_definer_con,localhost,mysqltest_dfn,,mysqltest_db1)
2819--connection wl2818_definer_con
2820--echo
2821--echo ---> connection: wl2818_definer_con
2822
2823CREATE TABLE t1(num_value INT);
2824CREATE TABLE t2(user_str TEXT);
2825
2826--disconnect wl2818_definer_con
2827
2828--connection default
2829--echo
2830--echo ---> connection: default
2831
2832GRANT INSERT, DROP ON mysqltest_db1.t1 TO mysqltest_dfn@localhost;
2833GRANT INSERT, DROP ON mysqltest_db1.t2 TO mysqltest_dfn@localhost;
2834
2835#
2836# Check that the user must have TRIGGER privilege to create a trigger.
2837#
2838
2839--connection default
2840--echo
2841--echo ---> connection: default
2842
2843GRANT SUPER ON *.* TO mysqltest_dfn@localhost;
2844
2845--connect (wl2818_definer_con,localhost,mysqltest_dfn,,mysqltest_db1)
2846--connection wl2818_definer_con
2847--echo
2848--echo ---> connection: wl2818_definer_con
2849
2850--error ER_TABLEACCESS_DENIED_ERROR
2851CREATE TRIGGER trg1 AFTER INSERT ON t1
2852  FOR EACH ROW
2853    INSERT INTO t2 VALUES(CURRENT_USER());
2854
2855--disconnect wl2818_definer_con
2856
2857#
2858# Check that the user must have TRIGGER privilege to drop a trigger.
2859#
2860
2861--connection default
2862--echo
2863--echo ---> connection: default
2864
2865GRANT TRIGGER ON mysqltest_db1.t1 TO mysqltest_dfn@localhost;
2866
2867--connect (wl2818_definer_con,localhost,mysqltest_dfn,,mysqltest_db1)
2868--connection wl2818_definer_con
2869--echo
2870--echo ---> connection: wl2818_definer_con
2871
2872CREATE TRIGGER trg1 AFTER INSERT ON t1
2873  FOR EACH ROW
2874    INSERT INTO t2 VALUES(CURRENT_USER());
2875
2876--disconnect wl2818_definer_con
2877
2878--connection default
2879--echo
2880--echo ---> connection: default
2881
2882REVOKE TRIGGER ON mysqltest_db1.t1 FROM mysqltest_dfn@localhost;
2883
2884--connect (wl2818_definer_con,localhost,mysqltest_dfn,,mysqltest_db1)
2885--connection wl2818_definer_con
2886--echo
2887--echo ---> connection: wl2818_definer_con
2888
2889--error ER_TABLEACCESS_DENIED_ERROR
2890DROP TRIGGER trg1;
2891
2892--disconnect wl2818_definer_con
2893
2894#
2895# Check that the definer must have TRIGGER privilege to activate a trigger.
2896#
2897
2898--connect (wl2818_definer_con,localhost,mysqltest_dfn,,mysqltest_db1)
2899--connection wl2818_definer_con
2900--echo
2901--echo ---> connection: wl2818_definer_con
2902
2903--error ER_TABLEACCESS_DENIED_ERROR
2904INSERT INTO t1 VALUES(0);
2905
2906--disconnect wl2818_definer_con
2907
2908--connection default
2909--echo
2910--echo ---> connection: default
2911
2912GRANT TRIGGER ON mysqltest_db1.t1 TO mysqltest_dfn@localhost;
2913
2914--connect (wl2818_definer_con,localhost,mysqltest_dfn,,mysqltest_db1)
2915--connection wl2818_definer_con
2916--echo
2917--echo ---> connection: wl2818_definer_con
2918
2919INSERT INTO t1 VALUES(0);
2920
2921# Cleanup for further tests.
2922DROP TRIGGER trg1;
2923TRUNCATE TABLE t1;
2924TRUNCATE TABLE t2;
2925
2926--disconnect wl2818_definer_con
2927
2928--connection default
2929--echo
2930--echo ---> connection: default
2931
2932REVOKE SUPER ON *.* FROM mysqltest_dfn@localhost;
2933
2934#
2935# Check that triggers are executed under the authorization of the definer:
2936#   - create two tables under "definer";
2937#   - grant all privileges on the test db to "definer";
2938#   - grant all privileges on the first table to "invoker";
2939#   - grant only select privilege on the second table to "invoker";
2940#   - create a trigger, which inserts a row into the second table after
2941#     inserting into the first table.
2942#   - insert a row into the first table under "invoker". A row also should be
2943#     inserted into the second table.
2944#
2945
2946--connect (wl2818_definer_con,localhost,mysqltest_dfn,,mysqltest_db1)
2947--connection wl2818_definer_con
2948--echo
2949--echo ---> connection: wl2818_definer_con
2950
2951CREATE TRIGGER trg1 AFTER INSERT ON t1
2952  FOR EACH ROW
2953    INSERT INTO t2 VALUES(CURRENT_USER());
2954
2955--connection default
2956--echo
2957--echo ---> connection: default
2958
2959# Setup definer's privileges.
2960
2961GRANT ALL PRIVILEGES ON mysqltest_db1.t1 TO mysqltest_dfn@localhost;
2962GRANT ALL PRIVILEGES ON mysqltest_db1.t2 TO mysqltest_dfn@localhost;
2963
2964# Setup invoker's privileges.
2965
2966GRANT ALL PRIVILEGES ON mysqltest_db1.t1
2967  TO 'mysqltest_inv'@localhost;
2968
2969GRANT SELECT ON mysqltest_db1.t2
2970  TO 'mysqltest_inv'@localhost;
2971
2972--connection wl2818_definer_con
2973--echo
2974--echo ---> connection: wl2818_definer_con
2975
2976use mysqltest_db1;
2977
2978INSERT INTO t1 VALUES(1);
2979
2980SELECT * FROM t1;
2981SELECT * FROM t2;
2982
2983--connect (wl2818_invoker_con,localhost,mysqltest_inv,,mysqltest_db1)
2984--connection wl2818_invoker_con
2985--echo
2986--echo ---> connection: wl2818_invoker_con
2987
2988use mysqltest_db1;
2989
2990INSERT INTO t1 VALUES(2);
2991
2992SELECT * FROM t1;
2993SELECT * FROM t2;
2994
2995#
2996# Check that if definer lost some privilege required to execute (activate) a
2997# trigger, the trigger will not be activated:
2998#  - create a trigger on insert into the first table, which will insert a row
2999#    into the second table;
3000#  - revoke INSERT privilege on the second table from the definer;
3001#  - insert a row into the first table;
3002#  - check that an error has been risen;
3003#  - check that no row has been inserted into the second table;
3004#
3005
3006--connection default
3007--echo
3008--echo ---> connection: default
3009
3010use mysqltest_db1;
3011
3012REVOKE INSERT ON mysqltest_db1.t2 FROM mysqltest_dfn@localhost;
3013
3014--connection wl2818_invoker_con
3015--echo
3016--echo ---> connection: wl2818_invoker_con
3017
3018use mysqltest_db1;
3019
3020--error ER_TABLEACCESS_DENIED_ERROR
3021INSERT INTO t1 VALUES(3);
3022
3023SELECT * FROM t1;
3024SELECT * FROM t2;
3025
3026#
3027# Check DEFINER clause of CREATE TRIGGER statement.
3028#
3029#   - Check that SUPER privilege required to create a trigger with different
3030#     definer:
3031#     - try to create a trigger with DEFINER="definer@localhost" under
3032#       "invoker";
3033#     - analyze error code;
3034#   - Check that if the user specified as DEFINER does not exist, a warning is
3035#     emitted:
3036#     - create a trigger with DEFINER="non_existent_user@localhost" from
3037#       "definer";
3038#     - check that a warning emitted;
3039#   - Check that the definer of a trigger does not exist, the trigger will not
3040#     be activated:
3041#     - activate just created trigger;
3042#     - check error code;
3043#
3044
3045--connection wl2818_definer_con
3046--echo
3047--echo ---> connection: wl2818_definer_con
3048
3049use mysqltest_db1;
3050
3051DROP TRIGGER trg1;
3052
3053# Check that SUPER is required to specify different DEFINER.
3054
3055--error ER_SPECIFIC_ACCESS_DENIED_ERROR
3056CREATE DEFINER='mysqltest_inv'@'localhost'
3057  TRIGGER trg1 BEFORE INSERT ON t1
3058  FOR EACH ROW
3059    SET @new_sum = 0;
3060
3061--connection default
3062--echo
3063--echo ---> connection: default
3064
3065use mysqltest_db1;
3066
3067GRANT SUPER ON *.* TO mysqltest_dfn@localhost;
3068
3069--disconnect wl2818_definer_con
3070--connect (wl2818_definer_con,localhost,mysqltest_dfn,,mysqltest_db1)
3071--connection wl2818_definer_con
3072--echo
3073--echo ---> connection: wl2818_definer_con
3074
3075CREATE DEFINER='mysqltest_inv'@'localhost'
3076  TRIGGER trg1 BEFORE INSERT ON t1
3077  FOR EACH ROW
3078    SET @new_sum = 0;
3079
3080# Create with non-existent user.
3081
3082CREATE DEFINER='mysqltest_nonexs'@'localhost'
3083  TRIGGER trg2 AFTER INSERT ON t1
3084  FOR EACH ROW
3085    SET @new_sum = 0;
3086
3087# Check that trg2 will not be activated.
3088
3089--error ER_NO_SUCH_USER
3090INSERT INTO t1 VALUES(6);
3091
3092#
3093# Check that SHOW TRIGGERS statement provides "Definer" column.
3094#
3095
3096--replace_column 6 #
3097SHOW TRIGGERS;
3098
3099DROP TRIGGER trg1;
3100DROP TRIGGER trg2;
3101
3102#
3103# Cleanup
3104#
3105
3106--connection default
3107--echo
3108--echo ---> connection: default
3109
3110DROP USER mysqltest_dfn@localhost;
3111DROP USER mysqltest_inv@localhost;
3112
3113DROP DATABASE mysqltest_db1;
3114
3115
3116###########################################################################
3117#
3118# BUG#15166: Wrong update [was: select/update] permissions required to execute
3119# triggers.
3120#
3121# BUG#15196: Wrong select permission required to execute triggers.
3122#
3123###########################################################################
3124
3125#
3126# Prepare environment.
3127#
3128
3129DELETE FROM mysql.user WHERE User LIKE 'mysqltest_%';
3130DELETE FROM mysql.db WHERE User LIKE 'mysqltest_%';
3131DELETE FROM mysql.tables_priv WHERE User LIKE 'mysqltest_%';
3132DELETE FROM mysql.columns_priv WHERE User LIKE 'mysqltest_%';
3133FLUSH PRIVILEGES;
3134
3135--disable_warnings
3136DROP DATABASE IF EXISTS mysqltest_db1;
3137--enable_warnings
3138
3139CREATE DATABASE mysqltest_db1;
3140
3141use mysqltest_db1;
3142
3143# Tables for tesing table-level privileges:
3144CREATE TABLE t1(col CHAR(20)); # table for "read-value" trigger
3145CREATE TABLE t2(col CHAR(20)); # table for "write-value" trigger
3146
3147# Tables for tesing column-level privileges:
3148CREATE TABLE t3(col CHAR(20)); # table for "read-value" trigger
3149CREATE TABLE t4(col CHAR(20)); # table for "write-value" trigger
3150
3151CREATE USER mysqltest_u1@localhost;
3152REVOKE ALL PRIVILEGES, GRANT OPTION FROM mysqltest_u1@localhost;
3153GRANT TRIGGER ON mysqltest_db1.* TO mysqltest_u1@localhost;
3154
3155SET @mysqltest_var = NULL;
3156
3157--connect (bug15166_u1_con,localhost,mysqltest_u1,,mysqltest_db1)
3158
3159# parsing (CREATE TRIGGER) time:
3160#   - check that nor SELECT either UPDATE is required to execute triggger w/o
3161#     NEW/OLD variables.
3162
3163--connection default
3164--echo
3165--echo ---> connection: default
3166
3167use mysqltest_db1;
3168
3169GRANT DELETE ON mysqltest_db1.* TO mysqltest_u1@localhost;
3170SHOW GRANTS FOR mysqltest_u1@localhost;
3171
3172--connection bug15166_u1_con
3173--echo
3174--echo ---> connection: bug15166_u1_con
3175
3176use mysqltest_db1;
3177
3178CREATE TRIGGER t1_trg_after_delete AFTER DELETE ON t1
3179  FOR EACH ROW
3180    SET @mysqltest_var = 'Hello, world!';
3181
3182# parsing (CREATE TRIGGER) time:
3183#   - check that UPDATE is not enough to read the value;
3184#   - check that UPDATE is required to modify the value;
3185
3186--connection default
3187--echo
3188--echo ---> connection: default
3189
3190use mysqltest_db1;
3191
3192GRANT UPDATE ON mysqltest_db1.t1 TO mysqltest_u1@localhost;
3193GRANT UPDATE ON mysqltest_db1.t2 TO mysqltest_u1@localhost;
3194
3195GRANT UPDATE(col) ON mysqltest_db1.t3 TO mysqltest_u1@localhost;
3196GRANT UPDATE(col) ON mysqltest_db1.t4 TO mysqltest_u1@localhost;
3197
3198--connection bug15166_u1_con
3199--echo
3200--echo ---> connection: bug15166_u1_con
3201
3202use mysqltest_db1;
3203
3204# - table-level privileges
3205
3206# TODO: check privileges at CREATE TRIGGER time.
3207# --error ER_COLUMNACCESS_DENIED_ERROR
3208CREATE TRIGGER t1_trg_err_1 BEFORE INSERT ON t1
3209  FOR EACH ROW
3210    SET @mysqltest_var = NEW.col;
3211DROP TRIGGER t1_trg_err_1;
3212
3213# TODO: check privileges at CREATE TRIGGER time.
3214# --error ER_COLUMNACCESS_DENIED_ERROR
3215CREATE TRIGGER t1_trg_err_2 BEFORE DELETE ON t1
3216  FOR EACH ROW
3217    SET @mysqltest_var = OLD.col;
3218DROP TRIGGER t1_trg_err_2;
3219
3220CREATE TRIGGER t2_trg_before_insert BEFORE INSERT ON t2
3221  FOR EACH ROW
3222    SET NEW.col = 't2_trg_before_insert';
3223
3224# - column-level privileges
3225
3226# TODO: check privileges at CREATE TRIGGER time.
3227# --error ER_COLUMNACCESS_DENIED_ERROR
3228CREATE TRIGGER t3_trg_err_1 BEFORE INSERT ON t3
3229  FOR EACH ROW
3230    SET @mysqltest_var = NEW.col;
3231DROP TRIGGER t3_trg_err_1;
3232
3233# TODO: check privileges at CREATE TRIGGER time.
3234# --error ER_COLUMNACCESS_DENIED_ERROR
3235CREATE TRIGGER t3_trg_err_2 BEFORE DELETE ON t3
3236  FOR EACH ROW
3237    SET @mysqltest_var = OLD.col;
3238DROP TRIGGER t3_trg_err_2;
3239
3240CREATE TRIGGER t4_trg_before_insert BEFORE INSERT ON t4
3241  FOR EACH ROW
3242    SET NEW.col = 't4_trg_before_insert';
3243
3244# parsing (CREATE TRIGGER) time:
3245#   - check that SELECT is required to read the value;
3246#   - check that SELECT is not enough to modify the value;
3247
3248--connection default
3249--echo
3250--echo ---> connection: default
3251
3252use mysqltest_db1;
3253
3254REVOKE UPDATE ON mysqltest_db1.t1 FROM mysqltest_u1@localhost;
3255REVOKE UPDATE ON mysqltest_db1.t2 FROM mysqltest_u1@localhost;
3256GRANT SELECT ON mysqltest_db1.t1 TO mysqltest_u1@localhost;
3257GRANT SELECT ON mysqltest_db1.t2 TO mysqltest_u1@localhost;
3258
3259REVOKE UPDATE(col) ON mysqltest_db1.t3 FROM mysqltest_u1@localhost;
3260REVOKE UPDATE(col) ON mysqltest_db1.t4 FROM mysqltest_u1@localhost;
3261GRANT SELECT(col) on mysqltest_db1.t3 TO mysqltest_u1@localhost;
3262GRANT SELECT(col) on mysqltest_db1.t4 TO mysqltest_u1@localhost;
3263
3264--connection bug15166_u1_con
3265--echo
3266--echo ---> connection: bug15166_u1_con
3267
3268use mysqltest_db1;
3269
3270# - table-level privileges
3271
3272CREATE TRIGGER t1_trg_after_insert AFTER INSERT ON t1
3273 FOR EACH ROW
3274  SET @mysqltest_var = NEW.col;
3275
3276CREATE TRIGGER t1_trg_after_update AFTER UPDATE ON t1
3277 FOR EACH ROW
3278  SET @mysqltest_var = OLD.col;
3279
3280# TODO: check privileges at CREATE TRIGGER time.
3281# --error ER_COLUMNACCESS_DENIED_ERROR
3282CREATE TRIGGER t2_trg_err_1 BEFORE UPDATE ON t2
3283 FOR EACH ROW
3284  SET NEW.col = 't2_trg_err_1';
3285DROP TRIGGER t2_trg_err_1;
3286
3287# TODO: check privileges at CREATE TRIGGER time.
3288# --error ER_COLUMNACCESS_DENIED_ERROR
3289CREATE TRIGGER t2_trg_err_2 BEFORE UPDATE ON t2
3290 FOR EACH ROW
3291  SET NEW.col = CONCAT(OLD.col, '(updated)');
3292DROP TRIGGER t2_trg_err_2;
3293
3294# - column-level privileges
3295
3296CREATE TRIGGER t3_trg_after_insert AFTER INSERT ON t3
3297  FOR EACH ROW
3298    SET @mysqltest_var = NEW.col;
3299
3300CREATE TRIGGER t3_trg_after_update AFTER UPDATE ON t3
3301  FOR EACH ROW
3302    SET @mysqltest_var = OLD.col;
3303
3304# TODO: check privileges at CREATE TRIGGER time.
3305# --error ER_COLUMNACCESS_DENIED_ERROR
3306CREATE TRIGGER t4_trg_err_1 BEFORE UPDATE ON t4
3307 FOR EACH ROW
3308  SET NEW.col = 't4_trg_err_1';
3309DROP TRIGGER t4_trg_err_1;
3310
3311# TODO: check privileges at CREATE TRIGGER time.
3312# --error ER_COLUMNACCESS_DENIED_ERROR
3313CREATE TRIGGER t4_trg_err_2 BEFORE UPDATE ON t4
3314 FOR EACH ROW
3315  SET NEW.col = CONCAT(OLD.col, '(updated)');
3316DROP TRIGGER t4_trg_err_2;
3317
3318# execution time:
3319#   - check that UPDATE is not enough to read the value;
3320#   - check that UPDATE is required to modify the value;
3321
3322--connection default
3323--echo
3324--echo ---> connection: default
3325
3326use mysqltest_db1;
3327
3328REVOKE SELECT ON mysqltest_db1.t1 FROM mysqltest_u1@localhost;
3329REVOKE SELECT ON mysqltest_db1.t2 FROM mysqltest_u1@localhost;
3330GRANT UPDATE ON mysqltest_db1.t1 TO mysqltest_u1@localhost;
3331GRANT UPDATE ON mysqltest_db1.t2 TO mysqltest_u1@localhost;
3332
3333REVOKE SELECT(col) ON mysqltest_db1.t3 FROM mysqltest_u1@localhost;
3334REVOKE SELECT(col) ON mysqltest_db1.t4 FROM mysqltest_u1@localhost;
3335GRANT UPDATE(col) ON mysqltest_db1.t3 TO mysqltest_u1@localhost;
3336GRANT UPDATE(col) ON mysqltest_db1.t4 TO mysqltest_u1@localhost;
3337
3338# - table-level privileges
3339
3340--error ER_COLUMNACCESS_DENIED_ERROR
3341INSERT INTO t1 VALUES('line1');
3342
3343SELECT * FROM t1;
3344SELECT @mysqltest_var;
3345
3346INSERT INTO t2 VALUES('line2');
3347
3348SELECT * FROM t2;
3349
3350# - column-level privileges
3351
3352--error ER_COLUMNACCESS_DENIED_ERROR
3353INSERT INTO t3 VALUES('t3_line1');
3354
3355SELECT * FROM t3;
3356SELECT @mysqltest_var;
3357
3358INSERT INTO t4 VALUES('t4_line2');
3359
3360SELECT * FROM t4;
3361
3362# execution time:
3363#   - check that SELECT is required to read the value;
3364#   - check that SELECT is not enough to modify the value;
3365
3366--connection default
3367--echo
3368--echo ---> connection: default
3369
3370use mysqltest_db1;
3371
3372REVOKE UPDATE ON mysqltest_db1.t1 FROM mysqltest_u1@localhost;
3373REVOKE UPDATE ON mysqltest_db1.t2 FROM mysqltest_u1@localhost;
3374GRANT SELECT ON mysqltest_db1.t1 TO mysqltest_u1@localhost;
3375GRANT SELECT ON mysqltest_db1.t2 TO mysqltest_u1@localhost;
3376
3377REVOKE UPDATE(col) ON mysqltest_db1.t3 FROM mysqltest_u1@localhost;
3378REVOKE UPDATE(col) ON mysqltest_db1.t4 FROM mysqltest_u1@localhost;
3379GRANT SELECT(col) ON mysqltest_db1.t3 TO mysqltest_u1@localhost;
3380GRANT SELECT(col) ON mysqltest_db1.t4 TO mysqltest_u1@localhost;
3381
3382# - table-level privileges
3383
3384INSERT INTO t1 VALUES('line3');
3385
3386SELECT * FROM t1;
3387SELECT @mysqltest_var;
3388
3389--error ER_COLUMNACCESS_DENIED_ERROR
3390INSERT INTO t2 VALUES('line4');
3391
3392SELECT * FROM t2;
3393
3394# - column-level privileges
3395
3396INSERT INTO t3 VALUES('t3_line2');
3397
3398SELECT * FROM t3;
3399SELECT @mysqltest_var;
3400
3401--error ER_COLUMNACCESS_DENIED_ERROR
3402INSERT INTO t4 VALUES('t4_line2');
3403
3404SELECT * FROM t4;
3405
3406# execution time:
3407#   - check that nor SELECT either UPDATE is required to execute triggger w/o
3408#     NEW/OLD variables.
3409
3410DELETE FROM t1;
3411
3412SELECT @mysqltest_var;
3413
3414#
3415# Cleanup.
3416#
3417
3418DROP USER mysqltest_u1@localhost;
3419
3420DROP DATABASE mysqltest_db1;
3421
3422
3423#
3424# Test for bug #14635 Accept NEW.x as INOUT parameters to stored
3425# procedures from within triggers
3426#
3427# We require UPDATE privilege when NEW.x passed as OUT parameter, and
3428# SELECT and UPDATE when NEW.x passed as INOUT parameter.
3429#
3430DELETE FROM mysql.user WHERE User LIKE 'mysqltest_%';
3431DELETE FROM mysql.db WHERE User LIKE 'mysqltest_%';
3432DELETE FROM mysql.tables_priv WHERE User LIKE 'mysqltest_%';
3433DELETE FROM mysql.columns_priv WHERE User LIKE 'mysqltest_%';
3434FLUSH PRIVILEGES;
3435
3436--disable_warnings
3437DROP DATABASE IF EXISTS mysqltest_db1;
3438--enable_warnings
3439
3440CREATE DATABASE mysqltest_db1;
3441USE mysqltest_db1;
3442
3443CREATE TABLE t1 (i1 INT);
3444CREATE TABLE t2 (i1 INT);
3445
3446CREATE USER mysqltest_dfn@localhost;
3447CREATE USER mysqltest_inv@localhost;
3448
3449GRANT EXECUTE, CREATE ROUTINE, TRIGGER ON *.* TO mysqltest_dfn@localhost;
3450GRANT INSERT ON mysqltest_db1.* TO mysqltest_inv@localhost;
3451
3452connect (definer,localhost,mysqltest_dfn,,mysqltest_db1);
3453connect (invoker,localhost,mysqltest_inv,,mysqltest_db1);
3454
3455connection definer;
3456CREATE PROCEDURE p1(OUT i INT) DETERMINISTIC NO SQL SET i = 3;
3457CREATE PROCEDURE p2(INOUT i INT) DETERMINISTIC NO SQL SET i = i * 5;
3458
3459# Check that having no privilege won't work.
3460connection definer;
3461CREATE TRIGGER t1_bi BEFORE INSERT ON t1 FOR EACH ROW
3462  CALL p1(NEW.i1);
3463CREATE TRIGGER t2_bi BEFORE INSERT ON t2 FOR EACH ROW
3464  CALL p2(NEW.i1);
3465
3466connection invoker;
3467--error ER_COLUMNACCESS_DENIED_ERROR
3468INSERT INTO t1 VALUES (7);
3469--error ER_COLUMNACCESS_DENIED_ERROR
3470INSERT INTO t2 VALUES (11);
3471
3472connection definer;
3473DROP TRIGGER t2_bi;
3474DROP TRIGGER t1_bi;
3475
3476# Check that having only SELECT privilege is not enough.
3477connection default;
3478GRANT SELECT ON mysqltest_db1.* TO mysqltest_dfn@localhost;
3479
3480connection definer;
3481CREATE TRIGGER t1_bi BEFORE INSERT ON t1 FOR EACH ROW
3482  CALL p1(NEW.i1);
3483CREATE TRIGGER t2_bi BEFORE INSERT ON t2 FOR EACH ROW
3484  CALL p2(NEW.i1);
3485
3486connection invoker;
3487--error ER_COLUMNACCESS_DENIED_ERROR
3488INSERT INTO t1 VALUES (13);
3489--error ER_COLUMNACCESS_DENIED_ERROR
3490INSERT INTO t2 VALUES (17);
3491
3492connection default;
3493REVOKE SELECT ON mysqltest_db1.* FROM mysqltest_dfn@localhost;
3494
3495connection definer;
3496DROP TRIGGER t2_bi;
3497DROP TRIGGER t1_bi;
3498
3499# Check that having only UPDATE privilege is enough for OUT parameter,
3500# but not for INOUT parameter.
3501connection default;
3502GRANT UPDATE ON mysqltest_db1.* TO mysqltest_dfn@localhost;
3503
3504connection definer;
3505CREATE TRIGGER t1_bi BEFORE INSERT ON t1 FOR EACH ROW
3506  CALL p1(NEW.i1);
3507CREATE TRIGGER t2_bi BEFORE INSERT ON t2 FOR EACH ROW
3508  CALL p2(NEW.i1);
3509
3510connection invoker;
3511INSERT INTO t1 VALUES (19);
3512--error ER_COLUMNACCESS_DENIED_ERROR
3513INSERT INTO t2 VALUES (23);
3514
3515connection default;
3516REVOKE UPDATE ON mysqltest_db1.* FROM mysqltest_dfn@localhost;
3517
3518connection definer;
3519DROP TRIGGER t2_bi;
3520DROP TRIGGER t1_bi;
3521
3522# Check that having SELECT and UPDATE privileges is enough.
3523connection default;
3524GRANT SELECT, UPDATE ON mysqltest_db1.* TO mysqltest_dfn@localhost;
3525
3526connection definer;
3527CREATE TRIGGER t1_bi BEFORE INSERT ON t1 FOR EACH ROW
3528  CALL p1(NEW.i1);
3529CREATE TRIGGER t2_bi BEFORE INSERT ON t2 FOR EACH ROW
3530  CALL p2(NEW.i1);
3531
3532connection invoker;
3533INSERT INTO t1 VALUES (29);
3534INSERT INTO t2 VALUES (31);
3535
3536connection default;
3537REVOKE SELECT, UPDATE ON mysqltest_db1.* FROM mysqltest_dfn@localhost;
3538
3539connection definer;
3540DROP TRIGGER t2_bi;
3541DROP TRIGGER t1_bi;
3542
3543connection default;
3544DROP PROCEDURE p2;
3545DROP PROCEDURE p1;
3546
3547# Check that late procedure redefining won't open a security hole.
3548connection default;
3549GRANT UPDATE ON mysqltest_db1.* TO mysqltest_dfn@localhost;
3550
3551connection definer;
3552CREATE PROCEDURE p1(OUT i INT) DETERMINISTIC NO SQL SET i = 37;
3553CREATE TRIGGER t1_bi BEFORE INSERT ON t1 FOR EACH ROW
3554  CALL p1(NEW.i1);
3555
3556connection invoker;
3557INSERT INTO t1 VALUES (41);
3558
3559connection definer;
3560DROP PROCEDURE p1;
3561CREATE PROCEDURE p1(IN i INT) DETERMINISTIC NO SQL SET @v1 = i + 43;
3562
3563connection invoker;
3564--error ER_COLUMNACCESS_DENIED_ERROR
3565INSERT INTO t1 VALUES (47);
3566
3567connection definer;
3568DROP PROCEDURE p1;
3569CREATE PROCEDURE p1(INOUT i INT) DETERMINISTIC NO SQL SET i = i + 51;
3570
3571connection invoker;
3572--error ER_COLUMNACCESS_DENIED_ERROR
3573INSERT INTO t1 VALUES (53);
3574
3575connection default;
3576DROP PROCEDURE p1;
3577REVOKE UPDATE ON mysqltest_db1.* FROM mysqltest_dfn@localhost;
3578
3579connection definer;
3580DROP TRIGGER t1_bi;
3581
3582# Cleanup.
3583disconnect definer;
3584disconnect invoker;
3585connection default;
3586DROP USER mysqltest_inv@localhost;
3587DROP USER mysqltest_dfn@localhost;
3588DROP TABLE t2;
3589DROP TABLE t1;
3590DROP DATABASE mysqltest_db1;
3591USE test;
3592
3593--echo End of 5.0 tests.
3594
3595#
3596# Bug#23713 LOCK TABLES + CREATE TRIGGER + FLUSH TABLES WITH READ LOCK = deadlock
3597#
3598
3599--disable_warnings
3600drop table if exists t1;
3601--enable_warnings
3602create table t1 (i int);
3603connect (flush,localhost,root,,test,,);
3604connection default;
3605--echo connection: default
3606lock tables t1 write;
3607connection flush;
3608--echo connection: flush
3609--send flush tables with read lock;
3610connection default;
3611--echo connection: default
3612let $wait_condition=
3613  select count(*) = 1 from information_schema.processlist
3614  where state = "Waiting for global read lock";
3615--source include/wait_condition.inc
3616create trigger t1_bi before insert on t1 for each row begin end;
3617unlock tables;
3618connection flush;
3619--echo connection: flush
3620--reap
3621unlock tables;
3622connection default;
3623select * from t1;
3624drop table t1;
3625disconnect flush;
3626
3627#
3628# Bug#45412 SHOW CREATE TRIGGER does not require privileges to disclose trigger data
3629#
3630CREATE DATABASE db1;
3631CREATE TABLE db1.t1 (a char(30)) ENGINE=MEMORY;
3632CREATE TRIGGER db1.trg AFTER INSERT ON db1.t1 FOR EACH ROW
3633 INSERT INTO db1.t1 VALUES('Some very sensitive data goes here');
3634
3635CREATE USER 'no_rights'@'localhost';
3636REVOKE ALL ON *.* FROM 'no_rights'@'localhost';
3637FLUSH PRIVILEGES;
3638
3639connect (con1,localhost,no_rights,,);
3640SELECT trigger_name FROM INFORMATION_SCHEMA.TRIGGERS
3641 WHERE trigger_schema = 'db1';
3642--error ER_SPECIFIC_ACCESS_DENIED_ERROR
3643SHOW CREATE TRIGGER db1.trg;
3644
3645connection default;
3646disconnect con1;
3647DROP USER 'no_rights'@'localhost';
3648DROP DATABASE db1;
3649
3650#
3651# Bug#55421 Protocol::end_statement(): Assertion `0' on multi-table UPDATE IGNORE
3652# To reproduce a crash we need to provoke a trigger execution with
3653# the following conditions:
3654#   - active SELECT statement during trigger execution
3655#    (i.e. LEX::current_select != NULL);
3656#   - IGNORE option (i.e. LEX::current_select->no_error == TRUE);
3657--disable_warnings
3658DROP DATABASE IF EXISTS mysqltest_db1;
3659--enable_warnings
3660
3661CREATE DATABASE mysqltest_db1;
3662USE mysqltest_db1;
3663
3664CREATE USER mysqltest_u1@localhost;
3665GRANT ALL ON mysqltest_db1.* TO mysqltest_u1@localhost;
3666
3667--connect(con1,localhost,mysqltest_u1,,mysqltest_db1)
3668
3669CREATE TABLE t1 (
3670  a1 int,
3671  a2 int
3672);
3673INSERT INTO t1 VALUES (1, 20);
3674
3675CREATE TRIGGER mysqltest_db1.upd_t1
3676BEFORE UPDATE ON t1 FOR EACH ROW SET new.a2 = 200;
3677
3678CREATE TABLE t2 (
3679  a1 int
3680);
3681
3682INSERT INTO t2 VALUES (2);
3683
3684--connection default
3685
3686REVOKE ALL PRIVILEGES, GRANT OPTION FROM mysqltest_u1@localhost;
3687
3688--error ER_TABLEACCESS_DENIED_ERROR
3689UPDATE IGNORE t1, t2 SET t1.a1 = 2, t2.a1 = 3 WHERE t1.a1 = 1 AND t2.a1 = 2;
3690# Cleanup
3691
3692DROP DATABASE mysqltest_db1;
3693DROP USER mysqltest_u1@localhost;
3694
3695--disconnect con1
3696--connection default
3697USE test;
3698
3699--echo End of 5.1 tests.
3700
3701--echo #
3702--echo # WL#2284: Increase the length of a user name
3703--echo #
3704
3705CREATE DATABASE test1;
3706CREATE TABLE test1.t1 (
3707  int_field INTEGER UNSIGNED NOT NULL,
3708  char_field CHAR(10),
3709  INDEX(`int_field`)
3710);
3711
3712use test;
3713
3714CREATE USER user_name_len_22_01234@localhost;
3715CREATE USER user_name_len_32_012345678901234@localhost;
3716
3717REVOKE ALL PRIVILEGES, GRANT OPTION FROM user_name_len_22_01234@localhost;
3718REVOKE ALL PRIVILEGES, GRANT OPTION FROM user_name_len_32_012345678901234@localhost;
3719
3720--echo # Check that user_name_len_22_01234 has no SELECT permission ON t1
3721connect (con_user_22,localhost,user_name_len_22_01234,,test);
3722--error ER_TABLEACCESS_DENIED_ERROR
3723SELECT * FROM test1.t1;
3724
3725--echo # Check that user_name_len_32_012345678901234 has no SELECT permission ON t1
3726connect (con_user_32,localhost,user_name_len_32_012345678901234,,test);
3727--error ER_TABLEACCESS_DENIED_ERROR
3728SELECT * FROM test1.t1;
3729
3730connection default;
3731
3732GRANT ALL ON test1.* TO user_name_len_32_012345678901234@localhost;
3733
3734CREATE DEFINER=user_name_len_32_012345678901234@localhost
3735  TRIGGER test1.t1_ai AFTER INSERT ON test1.t1 FOR EACH ROW SET @a = (SELECT COUNT(*) FROM test1.t1);
3736
3737GRANT INSERT ON test1.t1 TO user_name_len_22_01234@localhost;
3738
3739--echo # Now user_name_len_22_01234 should be able to "SELECT" COUNT(*) FROM
3740--echo # test1.t1 by using the trigger
3741
3742connection con_user_22;
3743set @a:=0;
3744INSERT INTO test1.t1 VALUES (1,'haha');
3745SELECT @a;
3746
3747connection default;
3748
3749--error ER_WRONG_STRING_LENGTH
3750CREATE DEFINER=user_name_len_33_0123456789012345@localhost
3751  TRIGGER test1.t1_bi BEFORE INSERT ON test1.t1 FOR EACH ROW SET @a = (SELECT COUNT(*) FROM test1.t1);
3752
3753#Cleanup
3754DROP DATABASE test1;
3755
3756DROP USER user_name_len_22_01234@localhost;
3757DROP USER user_name_len_32_012345678901234@localhost;
3758
3759--echo #
3760--echo # Bug#25209512:  CURRENT_TIMESTAMP PRODUCES ZEROS IN TRIGGER
3761--echo #
3762
3763--echo
3764--echo # Case 1: With BEFORE INSERT triggers
3765--echo # Scenario 1.1: Normal INSERT
3766SET TIMESTAMP= UNIX_TIMESTAMP("2017-03-30 07:07:07");
3767CREATE TABLE t1( a TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP );
3768
3769CREATE TRIGGER trigger_for_normal_insert BEFORE INSERT ON t1 FOR EACH ROW
3770SET @x:= NEW.a;
3771
3772INSERT INTO t1() VALUES();
3773
3774SELECT * FROM t1;
3775--echo # Without the patch, x contained zero timestamp.
3776SELECT @x;
3777
3778DROP TABLE t1;
3779
3780--echo
3781--echo # Scenario 1.2: INSERT INTO... SELECT
3782CREATE TABLE t1(a DATETIME NOT NULL DEFAULT NOW(), b INT);
3783
3784CREATE TRIGGER trigger_for_insert_select BEFORE INSERT ON t1 FOR EACH ROW
3785SET @x:= NEW.a;
3786
3787INSERT INTO t1(b) SELECT 1;
3788
3789SELECT * FROM t1;
3790--echo # Without the patch, x contained zero timestamp.
3791SELECT @x;
3792
3793DROP TABLE t1;
3794
3795--echo
3796--echo # Scenario 1.3: Normal REPLACE
3797CREATE TABLE t1( a TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP );
3798
3799CREATE TRIGGER trigger_for_normal_replace BEFORE INSERT ON t1 FOR EACH ROW
3800SET @x:= NEW.a;
3801
3802REPLACE INTO t1() VALUES();
3803
3804SELECT * FROM t1;
3805--echo # Without the patch, x contained zero timestamp.
3806SELECT @x;
3807
3808DROP TABLE t1;
3809
3810--echo
3811--echo # Scenario 1.4: REPLACE INTO... SELECT
3812CREATE TABLE t1(a DATETIME NOT NULL DEFAULT NOW(), b INT);
3813
3814CREATE TRIGGER trigger_for_replace_select BEFORE INSERT ON t1 FOR EACH ROW
3815SET @x:= NEW.a;
3816
3817REPLACE INTO t1(b) SELECT 1;
3818
3819SELECT * FROM t1;
3820--echo # Without the patch, x contained zero timestamp.
3821SELECT @x;
3822
3823DROP TABLE t1;
3824
3825--echo
3826--echo # Case 2: With BEFORE UPDATE triggers
3827--echo # Scenario 2.1: Normal UPDATE with ON UPDATE NOW() clause for
3828--echo #               the timestamp field.
3829SET TIMESTAMP= UNIX_TIMESTAMP("2017-04-11 09:09:09");
3830CREATE TABLE t1( a TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
3831                 ON UPDATE NOW(), b INT DEFAULT 1 );
3832
3833CREATE TRIGGER trigger_before_update BEFORE UPDATE ON t1 FOR EACH ROW
3834SET @x:= NEW.a;
3835
3836INSERT INTO t1 VALUES();
3837SELECT * FROM t1;
3838
3839SET TIMESTAMP= UNIX_TIMESTAMP("2017-04-12 10:10:10");
3840UPDATE t1 SET b= 2;
3841
3842SELECT * FROM t1;
3843--echo # Without the patch, x contained the old timestamp.
3844SELECT @x;
3845
3846DROP TABLE t1;
3847
3848--echo
3849--echo # Scenario 2.2: Normal UPDATE without ON UPDATE NOW() clause for
3850--echo #               the timestamp field.
3851SET TIMESTAMP= UNIX_TIMESTAMP("2017-04-13 08:08:08");
3852CREATE TABLE t1( a TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
3853                 b INT DEFAULT 1 );
3854
3855CREATE TRIGGER trigger_before_update BEFORE UPDATE ON t1 FOR EACH ROW
3856SET @x:= NEW.a;
3857
3858INSERT INTO t1 VALUES();
3859SELECT * FROM t1;
3860
3861SET TIMESTAMP= UNIX_TIMESTAMP("2017-05-04 05:05:05");
3862UPDATE t1 SET b= 2;
3863
3864SELECT * FROM t1;
3865--echo # x contains the old timestamp because of the absence of
3866--echo # ON UPDATE clause for the timestamp field.
3867SELECT @x;
3868
3869DROP TABLE t1;
3870
3871--echo
3872--echo # Scenario 2.3: UPDATE with join
3873SET TIMESTAMP= UNIX_TIMESTAMP("2017-04-25 11:11:11");
3874CREATE TABLE t1( a DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
3875                 ON UPDATE CURRENT_TIMESTAMP, b INT);
3876CREATE TABLE t2( d INT);
3877
3878INSERT INTO t1(b) VALUES(1);
3879INSERT INTO t2 VALUES(2);
3880
3881SELECT * FROM t1;
3882SELECT * FROM t2;
3883
3884CREATE TRIGGER trigger_before_update_with_join BEFORE UPDATE ON t1 FOR EACH ROW
3885SET @x:= NEW.a;
3886
3887SET TIMESTAMP= UNIX_TIMESTAMP("2017-04-25 01:01:01");
3888UPDATE t1, t2 SET t1.b= t2.d;
3889
3890SELECT * FROM t1;
3891--echo # Without the patch, x contained old timestamp
3892SELECT @x;
3893
3894DROP TABLE t1, t2;
3895
3896--echo
3897--echo # Case 3: LOAD DATA INFILE... with BEFORE INSERT triggers
3898SET TIMESTAMP= UNIX_TIMESTAMP("2017-04-17 06:06:06");
3899CREATE TABLE t1( a TIMESTAMP NOT NULL DEFAULT NOW(), b INT );
3900
3901CREATE TRIGGER trigger_for_load_infile BEFORE INSERT ON t1 FOR EACH ROW
3902SET @x:= NEW.a;
3903
3904SELECT 1 INTO OUTFILE 't1.dat' FROM dual;
3905LOAD DATA INFILE 't1.dat' INTO TABLE t1(b);
3906
3907SELECT * FROM t1;
3908--echo # Without the patch, x contained zero timestamp
3909SELECT @x;
3910
3911DROP TABLE t1;
3912
3913let $MYSQLD_DATADIR= `select @@datadir`;
3914remove_file $MYSQLD_DATADIR/test/t1.dat;
3915
3916SET TIMESTAMP= DEFAULT;
3917
3918
3919--echo #
3920--echo # BUG 26626277: BUG IN "INSERT... ON DUPLICATE KEY UPDATE" QUERY
3921--echo #
3922
3923--echo # Setup.
3924CREATE TABLE t1 (fld1 VARCHAR(64) NOT NULL,
3925fld2 INT DEFAULT 0, PRIMARY KEY (fld1));
3926
3927CREATE TABLE t2 (fld1 VARCHAR(64) NOT NULL,
3928fld2 INT(11) DEFAULT NULL, PRIMARY KEY (fld1));
3929
3930INSERT INTO t1(fld1) VALUES (1100);
3931INSERT INTO t2 VALUES (1100, 40);
3932
3933DELIMITER $$;
3934
3935CREATE TRIGGER update_after_update
3936AFTER UPDATE ON t2 FOR EACH ROW
3937BEGIN
3938  UPDATE t1 SET t1.fld2 = t1.fld2 + 1
3939  WHERE t1.fld1 = NEW.fld1;
3940END$$
3941
3942DELIMITER ;$$
3943
3944SELECT * FROM t1;
3945SELECT * FROM t2;
3946
3947--echo # Without patch, the after update trigger is not invoked.
3948INSERT INTO t2 (fld1) values (1100) ON DUPLICATE KEY UPDATE
3949fld2= 50;
3950
3951SELECT * FROM t1;
3952SELECT * FROM t2;
3953
3954--echo # Without patch, the after update trigger is not invoked.
3955INSERT INTO t2 (fld1) values (1100) ON DUPLICATE KEY UPDATE
3956fld2= 50;
3957
3958SELECT * FROM t1;
3959SELECT * FROM t2;
3960
3961--echo # Test added for coverage.
3962INSERT INTO t2 (fld1) values (1100) ON DUPLICATE KEY UPDATE
3963fld2= 60;
3964
3965SELECT * FROM t1;
3966SELECT * FROM t2;
3967
3968--echo # Tests covering before update trigger.
3969
3970--echo # Setup.
3971TRUNCATE TABLE t1;
3972DROP TRIGGER update_after_update;
3973INSERT INTO t1(fld1) VALUES (1100);
3974
3975DELIMITER $$;
3976
3977CREATE TRIGGER update_before_update
3978BEFORE UPDATE ON t2 FOR EACH ROW
3979BEGIN
3980  UPDATE t1 SET t1.fld2 = t1.fld2 + 1
3981  WHERE t1.fld1 = NEW.fld1;
3982END$$
3983
3984DELIMITER ;$$
3985
3986SELECT * FROM t1;
3987SELECT * FROM t2;
3988
3989INSERT INTO t2 (fld1) values (1100) ON DUPLICATE KEY UPDATE
3990fld2= 50;
3991
3992SELECT * FROM t1;
3993SELECT * FROM t2;
3994
3995INSERT INTO t2 (fld1) values (1100) ON DUPLICATE KEY UPDATE
3996fld2= 50;
3997
3998SELECT * FROM t1;
3999SELECT * FROM t2;
4000
4001INSERT INTO t2 (fld1) values (1100) ON DUPLICATE KEY UPDATE
4002fld2= 60;
4003
4004SELECT * FROM t1;
4005SELECT * FROM t2;
4006
4007--echo # Cleanup
4008DROP TRIGGER update_before_update;
4009DROP TABLE t1, t2;
4010
4011--echo #
4012--echo # BUG#27544152 - TRIGGERS ARE FIRED IN INCORRECT ORDE
4013--echo #
4014
4015CREATE TABLE t1 (a INT);
4016CREATE TRIGGER _AI_1 AFTER INSERT ON t1 FOR EACH ROW SET
4017@t1_var=concat(@t1_var,'_AI_1');
4018CREATE TRIGGER _AI_2 AFTER INSERT ON t1 FOR EACH ROW SET
4019@t1_var=concat(@t1_var,'_AI_2');
4020CREATE TRIGGER _BU_2 BEFORE UPDATE ON t1 FOR EACH ROW SET
4021@t1_var=concat(@t1_var,'_BU_2');
4022CREATE TRIGGER _BD_2 BEFORE DELETE ON t1 FOR EACH ROW SET
4023@t1_var=concat(@t1_var,'_BD_2');
4024CREATE TRIGGER _AI_0 AFTER INSERT ON t1 FOR EACH ROW PRECEDES _AI_1 SET
4025@t1_var=concat(@t1_var,'_AI_0');
4026CREATE TRIGGER _AI_3 AFTER INSERT ON t1 FOR EACH ROW FOLLOWS _AI_2 SET
4027@t1_var=concat(@t1_var,'_AI_3');
4028
4029SET @t1_var='Actual Result:   ';
4030INSERT INTO t1 VALUES (1);
4031SELECT @t1_var;
4032--echo # Expected result : _AI_0_AI_1_AI_2_AI_3
4033
4034DROP TABLE t1;
4035
4036
4037--echo #
4038--echo # Testing bug#28492272
4039--echo #
4040--echo # Test case 1: SET sql_mode='time_truncate_fractional';
4041CREATE TABLE t1 (i INT, j VARCHAR(32));
4042SET sql_mode='time_truncate_fractional';
4043
4044--echo # CREATE TRIGGER when sql_mode is set to time_truncate_fractional must
4045--echo # not assert
4046CREATE TRIGGER t1_before_insert BEFORE INSERT ON t1 FOR EACH ROW INSERT INTO
4047t3 VALUES (1, NEW.i, NULL, CONCAT("BI: ", NEW.j));
4048
4049SET sql_mode=default;
4050DROP TRIGGER t1_before_insert;
4051DROP TABLE t1;
4052
4053--echo # Test case 2 sql_mode=2147483648*2;
4054--echo # (This is actually equivalent to 'time_truncate_fractional', somehow)
4055CREATE TABLE t1 (i INT, j VARCHAR(32));
4056SET sql_mode=2147483648*2;
4057
4058--echo # CREATE TRIGGER when sql_mode is set to 2147483648*2 must not trigger
4059--echo # assert
4060CREATE TRIGGER t1_before_insert BEFORE INSERT ON t1 FOR EACH ROW INSERT INTO
4061t3 VALUES (1, NEW.i, NULL, CONCAT("BI: ", NEW.j));
4062
4063SET sql_mode=default;
4064DROP TRIGGER t1_before_insert;
4065DROP TABLE t1;
4066
4067--echo #
4068--echo # Bug#28122841 - CREATE EVENT/PROCEDURE/FUNCTION CRASHES WITH ACCENT INSENSITIVE NAMES.
4069--echo #                Even trigger names are case and accent insensitive. Test case to
4070--echo #                verify the same.
4071--echo #
4072SET NAMES utf8;
4073
4074CREATE TABLE t1 (f1 INT, f2 INT);
4075
4076--echo #
4077--echo # Test case to verify triggers with case and accent insensitive names.
4078--echo #
4079CREATE TRIGGER cafe BEFORE INSERT ON t1 FOR EACH ROW SET @sum= @sum + NEW.f1;
4080--echo # Following statement errors out as trigger with name 'cafe' already
4081--echo # exists on the table.
4082--error ER_TRG_ALREADY_EXISTS
4083CREATE TRIGGER café BEFORE INSERT ON t1 FOR EACH ROW SET @sum= @sum + NEW.f1;
4084--echo # Following statement is to verify operation with the upper case name.
4085--error ER_TRG_ALREADY_EXISTS
4086CREATE TRIGGER CAFE BEFORE INSERT ON t1 FOR EACH ROW SET @sum= @sum + NEW.f1;
4087--replace_column 2 # 3 # 4 # 5 # 6 # 7 #
4088SHOW CREATE TRIGGER cAfé;
4089DROP TRIGGER CaFé;
4090
4091--echo # Trigger with NAME_LEN size name.
4092CREATE TRIGGER очень_очень_очень_очень_очень_очень_очень_очень_длинная_строка_e
4093       BEFORE INSERT ON t1 FOR EACH ROW SET @sum= @sum + NEW.f1;
4094--error ER_TRG_ALREADY_EXISTS
4095CREATE TRIGGER очень_очень_очень_очень_очень_очень_очень_очень_длинная_строка_é
4096       BEFORE INSERT ON t1 FOR EACH ROW SET @sum= @sum + NEW.f1;
4097--error ER_TRG_ALREADY_EXISTS
4098CREATE TRIGGER очень_очень_очень_очень_очень_очень_очень_очень_длинная_строка_E
4099       BEFORE INSERT ON t1 FOR EACH ROW SET @sum= @sum + NEW.f1;
4100--replace_column 2 # 3 # 4 # 5 # 6 # 7 #
4101SHOW CREATE TRIGGER очень_очень_очень_очень_очень_очень_очень_очень_длинная_строка_é;
4102DROP TRIGGER очень_очень_очень_очень_очень_очень_очень_очень_длинная_строка_é;
4103
4104DROP TABLE t1;
4105SET NAMES default;
4106