1drop table if exists t1,t2,t3;
2CREATE TABLE t1 (  `id` int(11) NOT NULL default '0', `id2` int(11) NOT NULL default '0', `id3` int(11) NOT NULL default '0', `dummy1` char(30) default NULL, PRIMARY KEY  (`id`,`id2`), KEY `index_id3` (`id3`)) ENGINE=MyISAM;
3insert into t1 (id,id2) values (1,1),(1,2),(1,3);
4LOCK TABLE t1 WRITE;
5select dummy1,count(distinct id) from t1 group by dummy1;
6dummy1	count(distinct id)
7NULL	1
8update t1 set id=-1 where id=1;
9LOCK TABLE t1 READ;
10update t1 set id=1 where id=1;
11ERROR HY000: Table 't1' was locked with a READ lock and can't be updated
12create table t2 SELECT * from t1;
13ERROR HY000: Table 't2' was not locked with LOCK TABLES
14create temporary table t2 SELECT * from t1;
15drop table if exists t2;
16unlock tables;
17create table t2 SELECT * from t1;
18LOCK TABLE t1 WRITE,t2 write;
19insert into t2 SELECT * from t1;
20update t1 set id=1 where id=-1;
21drop table t1,t2;
22CREATE TABLE t1 (
23index1 smallint(6) default NULL,
24nr smallint(6) default NULL,
25KEY index1(index1)
26) ENGINE=MyISAM;
27CREATE TABLE t2 (
28nr smallint(6) default NULL,
29name varchar(20) default NULL
30) ENGINE=MyISAM;
31INSERT INTO t2 VALUES (1,'item1');
32INSERT INTO t2 VALUES (2,'item2');
33lock tables t1 write, t2 read;
34insert into t1 select 1,nr from t2 where name='item1';
35insert into t1 select 2,nr from t2 where name='item2';
36unlock tables;
37check table t1;
38Table	Op	Msg_type	Msg_text
39test.t1	check	status	OK
40lock tables t1 write;
41check table t2;
42Table	Op	Msg_type	Msg_text
43test.t2	check	Error	Table 't2' was not locked with LOCK TABLES
44test.t2	check	status	Operation failed
45insert into t1 select index1,nr from t1;
46ERROR HY000: Table 't1' was not locked with LOCK TABLES
47unlock tables;
48lock tables t1 write, t1 as t1_alias read;
49insert into t1 select index1,nr from t1 as t1_alias;
50drop table t1,t2;
51ERROR HY000: Table 't2' was not locked with LOCK TABLES
52unlock tables;
53drop table t1,t2;
54create table t1 (c1 int);
55create table t2 (c1 int);
56create table t3 (c1 int);
57lock tables t1 write, t2 write, t3 write;
58drop table t2, t3, t1;
59create table t1 (c1 int);
60create table t2 (c1 int);
61create table t3 (c1 int);
62lock tables t1 write, t2 write, t3 write, t1 as t4 read;
63alter table t2 add column c2 int;
64drop table t1, t2, t3;
65create table t1 ( a int(11) not null auto_increment, primary key(a));
66create table t2 ( a int(11) not null auto_increment, primary key(a));
67lock tables t1 write, t2 read;
68delete from t1 using t1,t2 where t1.a=t2.a;
69delete t1 from t1,t2 where t1.a=t2.a;
70delete from t2 using t1,t2 where t1.a=t2.a;
71ERROR HY000: Table 't2' was locked with a READ lock and can't be updated
72delete t2 from t1,t2 where t1.a=t2.a;
73ERROR HY000: Table 't2' was locked with a READ lock and can't be updated
74drop table t1,t2;
75ERROR HY000: Table 't2' was locked with a READ lock and can't be updated
76unlock tables;
77drop table t2,t1;
78End of 4.1 tests.
79drop table if exists t1;
80create table t1 (a int);
81lock table t1 write;
82flush tables with read lock;
83ERROR HY000: Can't execute the given command because you have active locked tables or an active transaction
84unlock tables;
85drop table t1;
86DROP TABLE IF EXISTS t1;
87CREATE TABLE t1 (i INT);
88LOCK TABLES mysql.time_zone READ, mysql.proc READ, t1 READ;
89UNLOCK TABLES;
90LOCK TABLES mysql.time_zone READ, mysql.proc READ, t1 WRITE;
91UNLOCK TABLES;
92LOCK TABLES mysql.time_zone READ, mysql.proc READ;
93UNLOCK TABLES;
94LOCK TABLES mysql.time_zone WRITE, mysql.proc WRITE;
95UNLOCK TABLES;
96LOCK TABLES mysql.time_zone READ, mysql.proc WRITE, t1 READ;
97ERROR HY000: You can't combine write-locking of system tables with other tables or lock types
98LOCK TABLES mysql.time_zone WRITE, mysql.proc WRITE, t1 READ;
99ERROR HY000: You can't combine write-locking of system tables with other tables or lock types
100LOCK TABLES mysql.time_zone WRITE, mysql.proc WRITE, t1 WRITE;
101ERROR HY000: You can't combine write-locking of system tables with other tables or lock types
102LOCK TABLES mysql.time_zone READ, mysql.proc WRITE;
103ERROR HY000: You can't combine write-locking of system tables with other tables or lock types
104DROP TABLE t1;
105
106Bug#5719 impossible to lock VIEW
107
108Just covering existing behaviour with tests.
109Consistency has not been found here.
110
111drop view if exists v_bug5719;
112drop table if exists t1, t2, t3;
113create table t1 (a int);
114create temporary table t2 (a int);
115create table t3 (a int);
116create view v_bug5719 as select 1;
117lock table v_bug5719 write;
118select * from t1;
119ERROR HY000: Table 't1' was not locked with LOCK TABLES
120
121Allowed to select from a temporary talbe under LOCK TABLES
122
123select * from t2;
124a
125select * from t3;
126ERROR HY000: Table 't3' was not locked with LOCK TABLES
127select * from v_bug5719;
1281
1291
130drop view v_bug5719;
131ERROR HY000: Can't execute the given command because you have active locked tables or an active transaction
132
133sic: did not left LOCK TABLES mode automatically
134
135select * from t1;
136ERROR HY000: Table 't1' was not locked with LOCK TABLES
137unlock tables;
138create or replace view v_bug5719 as select * from t1;
139lock tables v_bug5719 read;
140select * from v_bug5719;
141a
142
143Allowed to use an underlying table under LOCK TABLES <view>
144
145select * from t1;
146a
147
148Allowed to select from a temporary table under LOCK TABLES
149
150select * from t2;
151a
152select * from t3;
153ERROR HY000: Table 't3' was not locked with LOCK TABLES
154Dropping of implicitly locked table is disallowed.
155drop table t1;
156ERROR HY000: Table 't1' was locked with a READ lock and can't be updated
157unlock tables;
158Now let us also lock table explicitly and drop it.
159lock tables t1 write, v_bug5719 write;
160drop table t1;
161
162sic: left LOCK TABLES mode
163
164select * from t3;
165a
166select * from v_bug5719;
167ERROR HY000: View 'test.v_bug5719' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
168unlock tables;
169drop view v_bug5719;
170
171When limitation to use temporary tables in views is removed, please
172add a test that shows what happens under LOCK TABLES when a view
173references a temporary table, is locked, and the underlying table
174is dropped.
175
176create view v_bug5719 as select * from t2;
177ERROR HY000: View's SELECT refers to a temporary table 't2'
178
179Cleanup.
180
181drop table t2, t3;
182#
183# Bug#39843 DELETE requires write access to table in subquery in where clause
184#
185DROP TABLE IF EXISTS t1,t2;
186CREATE TABLE t1 (
187table1_rowid SMALLINT NOT NULL
188);
189CREATE TABLE t2 (
190table2_rowid SMALLINT NOT NULL
191);
192INSERT INTO t1 VALUES (1);
193INSERT INTO t2 VALUES (1);
194LOCK TABLES t1 WRITE, t2 READ;
195# Sub-select should not try to aquire a write lock.
196DELETE FROM t1
197WHERE EXISTS
198(
199SELECT 'x'
200FROM t2
201WHERE t1.table1_rowid = t2.table2_rowid
202) ;
203# While implementing the patch we didn't break old behavior;
204# The following sub-select should still requires a write lock:
205SELECT * FROM t1 WHERE 1 IN (SELECT * FROM t2 FOR UPDATE);
206ERROR HY000: Table 't2' was locked with a READ lock and can't be updated
207UNLOCK TABLES;
208DROP TABLE t1,t2;
209End of 5.1 tests.
210#
211# Ensure that FLUSH TABLES doesn't substitute a base locked table
212# with a temporary one.
213#
214drop table if exists t1, t2;
215create table t1 (a int);
216create table t2 (a int);
217lock table t1 write, t2 write;
218create temporary table t1 (a int);
219flush table t1;
220drop temporary table t1;
221select * from t1;
222a
223unlock tables;
224drop table t1, t2;
225#
226# Ensure that REPAIR .. USE_FRM works under LOCK TABLES.
227#
228drop table if exists t1, t2;
229create table t1 (a int);
230create table t2 (a int);
231lock table t1 write, t2 write;
232repair table t1 use_frm;
233Table	Op	Msg_type	Msg_text
234test.t1	repair	status	OK
235repair table t1 use_frm;
236Table	Op	Msg_type	Msg_text
237test.t1	repair	status	OK
238select * from t1;
239a
240select * from t2;
241a
242repair table t2 use_frm;
243Table	Op	Msg_type	Msg_text
244test.t2	repair	status	OK
245repair table t2 use_frm;
246Table	Op	Msg_type	Msg_text
247test.t2	repair	status	OK
248select * from t1;
249a
250unlock tables;
251drop table t1, t2;
252#
253# Ensure that mi_copy_status is called for two instances
254# of the same table when it is reopened after a flush.
255#
256drop table if exists t1;
257drop view if exists v1;
258create table t1 (c1 int);
259create view v1 as select * from t1;
260lock tables t1 write, v1 write;
261flush table t1;
262insert into t1 values (33);
263flush table t1;
264select * from t1;
265c1
26633
267unlock tables;
268drop table t1;
269drop view v1;
270#
271# WL#4284: Transactional DDL locking
272#
273drop table if exists t1;
274create table t1 (a int);
275connect con1,localhost,root,,;
276set autocommit= 0;
277insert into t1 values (1);
278lock table t1 write;
279# Ensure that metadata locks will be released if there is an open
280# transaction (autocommit=off) in conjunction with lock tables.
281disconnect con1;
282connection default;
283drop table t1;
284# Same problem but now for BEGIN
285drop table if exists t1;
286create table t1 (a int);
287connect con1,localhost,root,,;
288begin;
289insert into t1 values (1);
290# Ensure that metadata locks held by the transaction are released.
291disconnect con1;
292connection default;
293drop table t1;
294#
295# Coverage for situations when we try to execute DDL on tables
296# which are locked by LOCK TABLES only implicitly.
297#
298drop tables if exists t1, t2;
299drop view if exists v1;
300drop function if exists f1;
301create table t1 (i int);
302create table t2 (j int);
303#
304# Try to perform DDL on table which is locked through view.
305create view v1 as select * from t2;
306lock tables t1 write, v1 read;
307flush table t2;
308ERROR HY000: Table 't2' was locked with a READ lock and can't be updated
309drop table t2;
310ERROR HY000: Table 't2' was locked with a READ lock and can't be updated
311alter table t2 add column k int;
312ERROR HY000: Table 't2' was locked with a READ lock and can't be updated
313create trigger t2_bi before insert on t2 for each row set @a:=1;
314ERROR HY000: Table 't2' was locked with a READ lock and can't be updated
315# Repair produces error as part of its result set.
316repair table t2;
317Table	Op	Msg_type	Msg_text
318test.t2	repair	Error	Table 't2' was locked with a READ lock and can't be updated
319test.t2	repair	status	Operation failed
320unlock tables;
321drop view v1;
322#
323# Now, try  DDL on table which is locked through routine.
324create function f1 () returns int
325begin
326insert into t2 values (1);
327return 0;
328end|
329create view v1 as select f1() from t1;
330lock tables v1 read;
331flush table t2;
332ERROR HY000: Table 't2' was locked with a READ lock and can't be updated
333drop table t2;
334ERROR HY000: Table 't2' was locked with a READ lock and can't be updated
335alter table t2 add column k int;
336ERROR HY000: Table 't2' was locked with a READ lock and can't be updated
337create trigger t2_bi before insert on t2 for each row set @a:=1;
338ERROR HY000: Table 't2' was locked with a READ lock and can't be updated
339# Repair produces error as part of its result set.
340repair table t2;
341Table	Op	Msg_type	Msg_text
342test.t2	repair	Error	Table 't2' was locked with a READ lock and can't be updated
343test.t2	repair	status	Operation failed
344unlock tables;
345drop view v1;
346drop function f1;
347#
348# Finally, try DDL on table which is locked thanks to trigger.
349create trigger t1_ai after insert on t1 for each row insert into t2 values (1);
350lock tables t1 write;
351flush table t2;
352ERROR HY000: Table 't2' was locked with a READ lock and can't be updated
353drop table t2;
354ERROR HY000: Table 't2' was locked with a READ lock and can't be updated
355alter table t2 add column k int;
356ERROR HY000: Table 't2' was locked with a READ lock and can't be updated
357create trigger t2_bi before insert on t2 for each row set @a:=1;
358ERROR HY000: Table 't2' was locked with a READ lock and can't be updated
359# Repair produces error as part of its result set.
360repair table t2;
361Table	Op	Msg_type	Msg_text
362test.t2	repair	Error	Table 't2' was locked with a READ lock and can't be updated
363test.t2	repair	status	Operation failed
364unlock tables;
365drop trigger t1_ai;
366drop tables t1, t2;
367#
368# Bug#45035 " Altering table under LOCK TABLES results in
369# "Error 1213 Deadlock found..."
370#
371# When reopening tables under LOCK TABLES after ALTER TABLE,
372# 6.0 used to be taking thr_lock locks one by one, and
373# that would lead to a lock conflict.
374# Check that taking all locks at once works.
375#
376drop table if exists t1;
377create table t1 (i int);
378lock tables t1 write, t1 as a read, t1 as b read;
379alter table t1 add column j int;
380unlock tables;
381drop table t1;
382create temporary table t1 (i int);
383lock tables t1 write, t1 as a read, t1 as b read;
384alter table t1 add column j int;
385unlock tables;
386drop table t1;
387#
388# Separate case for partitioned tables is important
389# because each partition has an own thr_lock object.
390#
391create table t1 (i int) partition by list (i)
392(partition p0 values in (1),
393partition p1 values in (2,3),
394partition p2 values in (4,5));
395lock tables t1 write, t1 as a read, t1 as b read;
396alter table t1 add column j int;
397unlock tables;
398drop table t1;
399#
400# Bug #43272 HANDLER SQL command does not work under LOCK TABLES
401#
402DROP TABLE IF EXISTS t1;
403CREATE TABLE t1 (a INT);
404LOCK TABLE t1 WRITE;
405# HANDLER commands are not allowed in LOCK TABLES mode
406HANDLER t1 OPEN;
407ERROR HY000: Can't execute the given command because you have active locked tables or an active transaction
408HANDLER t1 READ FIRST;
409Got one of the listed errors
410HANDLER t1 CLOSE;
411ERROR HY000: Can't execute the given command because you have active locked tables or an active transaction
412UNLOCK TABLES;
413DROP TABLE t1;
414#
415# Bug#45066 FLUSH TABLES WITH READ LOCK deadlocks against
416#           LOCK TABLE
417#
418DROP TABLE IF EXISTS t1;
419CREATE TABLE t1(a INT);
420LOCK TABLE t1 READ;
421FLUSH TABLES;
422ERROR HY000: Table 't1' was locked with a READ lock and can't be updated
423LOCK TABLE t1 WRITE;
424FLUSH TABLES;
425#
426# If you allow the next combination, you reintroduce bug Bug#45066
427#
428LOCK TABLE t1 READ;
429FLUSH TABLES WITH READ LOCK;
430ERROR HY000: Can't execute the given command because you have active locked tables or an active transaction
431LOCK TABLE t1 WRITE;
432FLUSH TABLES WITH READ LOCK;
433ERROR HY000: Can't execute the given command because you have active locked tables or an active transaction
434UNLOCK TABLES;
435DROP TABLE t1;
436#
437# Simplified test for bug #48538 "Assertion in thr_lock() on LOAD DATA
438# CONCURRENT INFILE".
439#
440DROP TABLE IF EXISTS t1;
441CREATE TABLE t1 (f1 INT, f2 INT) ENGINE = MEMORY;
442CREATE TRIGGER t1_ai AFTER INSERT ON t1 FOR EACH ROW
443UPDATE LOW_PRIORITY t1 SET f2 = 7;
444# Statement below should fail with ER_CANT_UPDATE_USED_TABLE_IN_SF_OR_TRG
445# error instead of failing on assertion in table-level locking subsystem.
446INSERT INTO t1(f1) VALUES(0);
447ERROR HY000: Can't update table 't1' in stored function/trigger because it is already used by statement which invoked this stored function/trigger
448DROP TABLE t1;
449#
450# Bug#43685 Lock table affects other non-related tables
451#
452DROP TABLE IF EXISTS t1, t2;
453connect  con2, localhost, root;
454CREATE TABLE t1 (id INT);
455CREATE TABLE t2 (id INT);
456connection default;
457LOCK TABLE t1 WRITE;
458ANALYZE TABLE t1;
459Table	Op	Msg_type	Msg_text
460test.t1	analyze	status	Engine-independent statistics collected
461test.t1	analyze	status	Table is already up to date
462connection con2;
463LOCK TABLE t2 WRITE;
464# This used to hang until the first connection
465# unlocked t1.
466FLUSH TABLE t2;
467UNLOCK TABLES;
468connection default;
469UNLOCK TABLES;
470DROP TABLE t1, t2;
471disconnect con2;
472#
473# End of 6.0 tests.
474#
475create table t1 (a int) engine=myisam;
476lock tables t1 write concurrent, t1 as t2 read;
477connect  con1,localhost,root,,;
478connection con1;
479lock tables t1 read local;
480unlock tables;
481connection default;
482unlock tables;
483connection con1;
484lock tables t1 read local;
485connection default;
486lock tables t1 write concurrent, t1 as t2 read;
487unlock tables;
488connection con1;
489unlock tables;
490disconnect con1;
491connection default;
492drop table t1;
493#
494# MDEV-15769: Assertion `!is_set() || (m_status == DA_OK_BULK && is_bulk_op())' failed in Diagnostics_area::set_ok_status
495#
496CREATE TABLE t1 (a INT);
497CREATE TABLE t2 (b INT);
498LOCK TABLE t1 READ;
499connect  con1,localhost,root,,test;
500LOCK TABLE t2 WRITE;
501SET lock_wait_timeout= 1;
502FLUSH TABLES;
503FLUSH TABLES t2;
504UNLOCK TABLES;
505disconnect con1;
506connection default;
507UNLOCK TABLES;
508DROP TABLE t1, t2;
509#
510# MDEV-21398 Deadlock (server hang) or assertion failure in
511# Diagnostics_area::set_error_status upon ALTER under lock
512#
513CREATE TABLE t1 (a INT) ENGINE=MyISAM;
514LOCK TABLE t1 WRITE, t1 AS t1a READ;
515ALTER TABLE t1 CHANGE COLUMN IF EXISTS x xx INT;
516Warnings:
517Note	1054	Unknown column 'x' in 't1'
518UNLOCK TABLES;
519DROP TABLE t1;
520#
521# End of 10.2 tests
522#
523#
524# MDEV-24382 Assertion `thd->mdl_context.is_lock_owner(MDL_key::TABLE, db, table_name, MDL_EXCLUSIVE)'
525# failed in tdc_remove_table
526#
527CREATE TABLE t (c INT);
528LOCK TABLES t READ LOCAL;
529CREATE TEMPORARY TABLE t (a INT) SELECT 1 AS a;
530DROP SEQUENCE t;
531ERROR 42S02: Unknown SEQUENCE: 'test.t'
532DROP TEMPORARY TABLE t;
533UNLOCK TABLES;
534DROP TABLE t;
535#
536# End of 10.5 tests
537#
538