1#
2# Check CREATE OR REPLACE TABLE
3#
4
5--source include/have_innodb.inc
6--source include/have_metadata_lock_info.inc
7--disable_warnings
8drop table if exists t1,t2,t3;
9--enable_warnings
10
11#
12# Create help table
13#
14
15CREATE TABLE t2 (a int);
16INSERT INTO t2 VALUES(1),(2),(3);
17
18--echo #
19--echo # Check first syntax and wrong usage
20--echo #
21
22--error ER_WRONG_USAGE
23CREATE OR REPLACE TABLE IF NOT EXISTS t1 (a int);
24
25# check that we don't try to create a log table in use
26--error ER_BAD_LOG_STATEMENT
27create or replace table mysql.general_log (a int);
28--error ER_BAD_LOG_STATEMENT
29create or replace table mysql.slow_log (a int);
30
31--echo #
32--echo # Usage when table doesn't exist
33--echo #
34
35CREATE OR REPLACE TABLE t1 (a int);
36--error ER_TABLE_EXISTS_ERROR
37CREATE TABLE t1 (a int);
38DROP TABLE t1;
39CREATE OR REPLACE TEMPORARY TABLE t1 (a int);
40--error ER_TABLE_EXISTS_ERROR
41CREATE TEMPORARY TABLE t1 (a int, b int, c int);
42DROP TEMPORARY TABLE t1;
43
44--echo #
45--echo # Testing with temporary tables
46--echo #
47
48CREATE OR REPLACE TABLE t1 (a int);
49CREATE OR REPLACE TEMPORARY TABLE t1 (a int);
50CREATE OR REPLACE TEMPORARY TABLE t1 (a int, b int);
51SHOW CREATE TABLE t1;
52DROP TEMPORARY TABLE t1;
53SHOW CREATE TABLE t1;
54DROP TABLE t1;
55
56# Test also with InnoDB
57create temporary table t1 (i int) engine=InnoDB;
58create or replace temporary table t1 (a int, b int) engine=InnoDB;
59create or replace temporary table t1 (j int);
60show create table t1;
61drop table t1;
62
63# Using lock tables on normal tables with create or replace on temp tables
64CREATE OR REPLACE TABLE t1 (a int);
65LOCK TABLES t1 write;
66CREATE OR REPLACE TEMPORARY TABLE t1 (a int);
67CREATE OR REPLACE TEMPORARY TABLE t1 (a int, b int);
68CREATE OR REPLACE TEMPORARY TABLE t1 (a int, b int) engine= innodb;
69CREATE OR REPLACE TEMPORARY TABLE t1 (a int) engine= innodb;
70CREATE OR REPLACE TEMPORARY TABLE t1 (a int, b int) engine=myisam;
71SHOW CREATE TABLE t1;
72DROP TEMPORARY TABLE t1;
73SHOW CREATE TABLE t1;
74# Verify that table is still locked
75--error ER_TABLE_NOT_LOCKED
76CREATE OR REPLACE TABLE t2 (a int);
77DROP TABLE t1;
78UNLOCK TABLES;
79
80#
81# Using CREATE SELECT
82#
83
84CREATE OR REPLACE TEMPORARY TABLE t1 (a int) SELECT * from t2;
85SELECT * FROM t1;
86CREATE OR REPLACE TEMPORARY TABLE t1 (b int) SELECT * from t2;
87SELECT * FROM t1;
88SHOW CREATE TABLE t1;
89DROP TABLE t1;
90CREATE TEMPORARY TABLE t1 AS SELECT a FROM t2;
91CREATE TEMPORARY TABLE IF NOT EXISTS t1(a int, b int) SELECT 1,2 FROM t2;
92DROP TABLE t1;
93
94CREATE TABLE t1 (a int);
95CREATE OR REPLACE TABLE t1 AS SELECT 1;
96SHOW CREATE TABLE t1;
97DROP TABLE t1;
98
99create table t1 (a int);
100--error ER_UPDATE_TABLE_USED
101create or replace table t1 as select * from t1;
102--error ER_UPDATE_TABLE_USED
103create or replace table t1 as select a from (select a from t1) as t3;
104--error ER_UPDATE_TABLE_USED
105create or replace table t1 as select a from t2 where t2.a in (select a from t1);
106drop table t1;
107
108--echo #
109--echo # Testing with normal tables
110--echo #
111
112CREATE OR REPLACE TABLE t1 (a int);
113CREATE OR REPLACE TABLE t1 (a int, b int);
114SHOW CREATE TABLE t1;
115DROP TABLE t1;
116
117CREATE TABLE t1 (a int) SELECT * from t2;
118SELECT * FROM t1;
119TRUNCATE TABLE t1;
120CREATE TABLE IF NOT EXISTS t1 (a int) SELECT * from t2;
121SELECT * FROM t1;
122DROP TABLE t1;
123
124CREATE TABLE t1 (i int);
125CREATE OR REPLACE TABLE t1 AS SELECT 1;
126SHOW CREATE TABLE t1;
127DROP TABLE t1;
128
129# Using lock tables with CREATE OR REPLACE
130CREATE OR REPLACE TABLE t1 (a int);
131LOCK TABLES t1 write,t2 write;
132CREATE OR REPLACE TABLE t1 (a int, b int);
133# Verify if table is still locked
134SELECT * FROM t1;
135INSERT INTO t1 values(1,1);
136CREATE OR REPLACE TABLE t1 (a int, b int, c int);
137INSERT INTO t1 values(1,1,1);
138--error ER_TABLE_NOT_LOCKED
139CREATE OR REPLACE TABLE t3 (a int);
140UNLOCK TABLES;
141DROP TABLE t1;
142
143# Using lock tables with CREATE OR REPLACE ... SELECT
144CREATE OR REPLACE TABLE t1 (a int);
145LOCK TABLES t1 write,t2 write;
146CREATE OR REPLACE TABLE t1 (a int, b int) select a,1 from t2;
147# Verify if table is still locked
148SELECT * FROM t2;
149SELECT * FROM t1;
150SELECT * FROM t1;
151INSERT INTO t1 values(1,1,1);
152CREATE OR REPLACE TABLE t1 (a int, b int, c int, d int);
153INSERT INTO t1 values(1,1,1,1);
154--error ER_TABLE_NOT_LOCKED
155CREATE OR REPLACE TABLE t3 (a int);
156UNLOCK TABLES;
157DROP TABLE t1;
158
159CREATE OR REPLACE TABLE t1 (a int);
160LOCK TABLES t1 write,t2 write, t1 as t1_read read;
161CREATE OR REPLACE TABLE t1 (a int, b int) select a,1 from t2;
162SELECT * FROM t1;
163SELECT * FROM t2;
164--error ER_TABLE_NOT_LOCKED
165SELECT * FROM t1 as t1_read;
166DROP TABLE t1;
167UNLOCK TABLES;
168
169CREATE OR REPLACE TABLE t1 (a int);
170LOCK TABLE t1 WRITE;
171CREATE OR REPLACE TABLE t1 AS SELECT 1;
172SELECT * from t1;
173--error ER_TABLE_NOT_LOCKED
174SELECT * from t2;
175DROP TABLE t1;
176
177--echo #
178--echo # Test also with InnoDB (transactional engine)
179--echo #
180
181create table t1 (i int) engine=innodb;
182lock table t1 write;
183create or replace table t1 (j int);
184unlock tables;
185show create table t1;
186drop table t1;
187
188create table t1 (i int) engine=InnoDB;
189lock table t1 write, t2 write;
190create or replace table t1 (j int) engine=innodb;
191unlock tables;
192drop table t1;
193
194create table t1 (i int) engine=InnoDB;
195create table t3 (i int) engine=InnoDB;
196insert into t3 values(1),(2),(3);
197create table t4 (i int) engine=InnoDB;
198insert into t4 values(1);
199lock table t1 write, t2 write, t3 write, t4 write;
200create or replace table t1 (a int, i int) engine=innodb select t2.a,t3.i from t2,t3;
201select * from t4;
202unlock tables;
203select * from t1 order by a,i;
204drop table t1,t3,t4;
205
206--echo #
207--echo # Test the meta data locks are freed properly
208--echo #
209
210create database mysqltest2;
211
212drop table if exists test.t1,mysqltest2.t2;
213create table test.t1 (i int) engine=myisam;
214create table mysqltest2.t2 like test.t1;
215lock table test.t1 write, mysqltest2.t2 write;
216--replace_column 1 #
217--sorted_result
218select * from information_schema.metadata_lock_info;
219--error ER_TABLE_MUST_HAVE_COLUMNS
220create or replace table test.t1;
221show tables;
222--replace_column 1 #
223--sorted_result
224select * from information_schema.metadata_lock_info;
225--error ER_TABLE_MUST_HAVE_COLUMNS
226create or replace table mysqltest2.t2;
227--replace_column 1 #
228--sorted_result
229select * from information_schema.metadata_lock_info;
230create table t1 (i int);
231drop table t1;
232
233create table test.t1 (i int);
234create table mysqltest2.t2 like test.t1;
235lock table test.t1 write, mysqltest2.t2 write;
236--replace_column 1 #
237--sorted_result
238select * from information_schema.metadata_lock_info;
239--error ER_DUP_FIELDNAME
240create or replace table test.t1 (a int) select 1 as 'a', 2 as 'a';
241show tables;
242--replace_column 1 #
243--sorted_result
244select * from information_schema.metadata_lock_info;
245--error ER_DUP_FIELDNAME
246create or replace table mysqltest2.t2 (a int) select 1 as 'a', 2 as 'a';
247--replace_column 1 #
248--sorted_result
249select * from information_schema.metadata_lock_info;
250create table t1 (i int);
251drop table t1;
252
253create table test.t1 (i int) engine=innodb;
254create table mysqltest2.t2 like test.t1;
255lock table test.t1 write, mysqltest2.t2 write;
256--replace_column 1 #
257--sorted_result
258select * from information_schema.metadata_lock_info;
259unlock tables;
260drop table test.t1,mysqltest2.t2;
261
262create table test.t1 (i int) engine=aria transactional=1 checksum=1;
263create table mysqltest2.t2 like test.t1;
264lock table test.t1 write, mysqltest2.t2 write;
265--replace_column 1 #
266--sorted_result
267select * from information_schema.metadata_lock_info;
268unlock tables;
269drop table t1;
270
271create table test.t1 (i int);
272drop database mysqltest2;
273drop table test.t1;
274
275
276--echo #
277--echo # MDEV-23391 Server crash in close_thread_table or assertion, upon CREATE OR REPLACE TABLE under lock
278--echo #
279create table t1 (i int);
280lock table t1 write;
281--replace_column 1 #
282--sorted_result
283select * from information_schema.metadata_lock_info;
284--error ER_DATA_TOO_LONG
285create or replace table t1 (a char(1)) engine=Innodb select 'foo' as a;
286show tables;
287--replace_column 1 #
288--sorted_result
289select * from information_schema.metadata_lock_info;
290create table t1 (i int);
291drop table t1;
292
293
294--echo #
295--echo # Testing CREATE .. LIKE
296--echo #
297
298create or replace table t1 like t2;
299create or replace table t1 like t2;
300show create table t1;
301drop table t1;
302create table t1 (b int);
303lock tables t1 write, t2 read;
304create or replace table t1 like t2;
305SELECT * FROM t1;
306INSERT INTO t1 values(1);
307CREATE OR REPLACE TABLE t1 like t2;
308INSERT INTO t1 values(2);
309unlock tables;
310show create table t1;
311drop table t1;
312
313create or replace table t1 like t2;
314--error ER_NONUNIQ_TABLE
315create or replace table t1 like t1;
316drop table t1;
317
318CREATE TEMPORARY TABLE t1 like t2;
319--error ER_NONUNIQ_TABLE
320CREATE OR REPLACE TABLE t1 like t1;
321--error ER_NONUNIQ_TABLE
322CREATE OR REPLACE TABLE t1 like t1;
323drop table t1;
324
325CREATE TEMPORARY TABLE t1 like t2;
326CREATE OR REPLACE TEMPORARY TABLE t3 like t1;
327--error ER_NONUNIQ_TABLE
328CREATE OR REPLACE TEMPORARY TABLE t3 like t3;
329drop table t1,t3;
330
331--echo #
332--echo # Test with prepared statements
333--echo #
334
335prepare stmt1 from 'create or replace table t1 select * from t2';
336execute stmt1;
337select * from t1;
338execute stmt1;
339select * from t1;
340drop table t1;
341execute stmt1;
342select * from t1;
343deallocate prepare stmt1;
344drop table t1;
345
346--echo #
347--echo # Test with views
348--echo #
349
350create view t1 as select 1;
351create table if not exists t1 (a int);
352--error ER_IT_IS_A_VIEW
353create or replace table t1 (a int);
354--error ER_IT_IS_A_VIEW
355drop table t1;
356drop view t1;
357
358--echo #
359--echo # MDEV-5602 CREATE OR REPLACE obtains stricter locks than the
360--echo # connection had before
361--echo #
362
363create table t1 (a int);
364lock table t1 write, t2 read;
365--replace_column 1 #
366--sorted_result
367select * from information_schema.metadata_lock_info;
368create or replace table t1 (i int);
369--replace_column 1 #
370--sorted_result
371select * from information_schema.metadata_lock_info;
372create or replace table t1 like t2;
373--replace_column 1 #
374--sorted_result
375select * from information_schema.metadata_lock_info;
376create or replace table t1 select 1 as f1;
377--replace_column 1 #
378--sorted_result
379select * from information_schema.metadata_lock_info;
380drop table t1;
381unlock tables;
382
383--echo #
384--echo # MDEV-6560
385--echo # Assertion `! is_set() ' failed in Diagnostics_area::set_ok_status
386--echo #
387
388CREATE TABLE t1 (col_int_nokey INT)  ENGINE=InnoDB;
389
390CREATE OR REPLACE TEMPORARY TABLE tmp LIKE t1;
391LOCK TABLE t1 WRITE;
392
393--connect (con1,localhost,root,,test)
394--let $con_id = `SELECT CONNECTION_ID()`
395--send CREATE OR REPLACE TABLE t1 LIKE tmp
396--connection default
397let $wait_condition= SELECT COUNT(*)=1 FROM information_schema.processlist
398  WHERE state= 'Waiting for table metadata lock';
399--source include/wait_condition.inc
400--replace_result $con_id con_id
401--eval KILL QUERY $con_id
402
403--connection con1
404--error ER_QUERY_INTERRUPTED
405--reap
406--send CREATE OR REPLACE TABLE t1 (a int)
407
408--connection default
409let $wait_condition= SELECT COUNT(*)=1 FROM information_schema.processlist
410  WHERE state= 'Waiting for table metadata lock';
411--source include/wait_condition.inc
412--replace_result $con_id con_id
413--eval KILL QUERY $con_id
414
415--connection con1
416--error ER_QUERY_INTERRUPTED
417--reap
418--disconnect con1
419--connection default
420
421drop table t1;
422
423#
424# Cleanup
425#
426DROP TABLE t2;
427
428--echo #
429--echo # MDEV-10824 - Crash in CREATE OR REPLACE TABLE t1 AS SELECT spfunc()
430--echo #
431CREATE TABLE t1(a INT);
432CREATE FUNCTION f1() RETURNS VARCHAR(16383) RETURN 'test';
433CREATE OR REPLACE TABLE t1 AS SELECT f1();
434LOCK TABLE t1 WRITE;
435CREATE OR REPLACE TABLE t1 AS SELECT f1();
436UNLOCK TABLES;
437DROP FUNCTION f1;
438DROP TABLE t1;
439
440--echo #
441--echo # MDEV-11129
442--echo # CREATE OR REPLACE TABLE t1 AS SELECT spfunc() crashes if spfunc()
443--echo # references t1
444--echo #
445
446CREATE OR REPLACE TABLE t1(a INT);
447DELIMITER $$;
448CREATE FUNCTION f1() RETURNS VARCHAR(16383)
449BEGIN
450  INSERT INTO t1 VALUES(1);
451  RETURN 'test';
452END;
453$$
454DELIMITER ;$$
455--error ER_UPDATE_TABLE_USED
456CREATE OR REPLACE TABLE t1 AS SELECT f1();
457LOCK TABLE t1 WRITE;
458--error ER_TABLE_NOT_LOCKED
459CREATE OR REPLACE TABLE t1 AS SELECT f1();
460UNLOCK TABLES;
461
462DROP FUNCTION f1;
463DROP TABLE t1;
464
465
466--echo #
467--echo # MDEV-14410 - Assertion `table->pos_in_locked_tables == __null ||
468--echo #              table->pos_in_locked_tables->table == table' failed in
469--echo #              mark_used_tables_as_free_for_reuse
470--echo #
471CREATE TABLE t1 (a INT);
472CREATE TABLE t2 (b INT);
473CREATE TABLE t3 (c INT);
474
475CREATE TRIGGER tr1 BEFORE INSERT ON t3 FOR EACH ROW INSERT INTO t1 VALUES ();
476CREATE TRIGGER tr2 BEFORE INSERT ON t2 FOR EACH ROW INSERT INTO t3 SELECT * FROM t1;
477
478LOCK TABLE t1 WRITE, t2 WRITE;
479CREATE OR REPLACE TABLE t1 (i INT);
480UNLOCK TABLES;
481INSERT INTO t2 VALUES (1);
482
483# Cleanup
484DROP TABLE t1, t2, t3;
485
486--echo #
487--echo # MDEV-11071 - Assertion `thd->transaction.stmt.is_empty()' failed in
488--echo #              Locked_tables_list::unlock_locked_tables
489--echo #
490CREATE TEMPORARY TABLE t1(a INT) ENGINE=InnoDB;
491CREATE TEMPORARY TABLE t2(a INT);
492CREATE TABLE t3(a INT);
493LOCK TABLE t2 WRITE;
494SELECT * FROM t2;
495# drops t2
496--error ER_INVALID_DEFAULT
497CREATE OR REPLACE TEMPORARY TABLE t1(c INT DEFAULT '');
498# make sure we didn't leave locked tables mode
499--error ER_TABLE_NOT_LOCKED
500SELECT * FROM t3;
501# drops t1
502--error ER_INVALID_DEFAULT
503CREATE OR REPLACE TEMPORARY TABLE t2(c INT DEFAULT '');
504# make sure we didn't leave locked tables mode
505--error ER_TABLE_NOT_LOCKED
506SELECT * FROM t3;
507UNLOCK TABLES;
508DROP TABLE t3;
509
510--echo # End of 10.4 tests
511