mariadb/mysql-test/suite/sql_sequence/default.test
Monty 9cc7789e90 MDEV 13679 Enabled sequences to be used in DEFAULT
Other changes done to get this to work:
- Added 'internal_tables' to TABLE object to list which sequence tables
  is needed to use the table.
- Mark any expression using DEFAULT() with LEX->default_used.
  This is needed when deciding if we should open internal sequence
  tables when a table is opened (we don't need to open sequence tables
  if the main table is only used with SELECT).
- Create_and_open_temporary_table() can now also open all internal
  sequence tables.
- Added option MYSQL_LOCK_USE_MALLOC to mysql_lock_tables()
  to force memory allocation to be used with malloc instead of
  memroot.
- Added flag to MYSQL_LOCK to remember if allocation was done with
  malloc or memroot (makes code simpler and safer).
- init_one_table_for_prelocking() now takes argument for what lock to
  use instead of it's a routine or something else.
- Renamed prelocking placeholders to make them more understandable as
  they are now used in more code.
- Changed test in check_lock_and_start_stmt() if found table has correct
  locks. The old test didn't work for tables that has lock
  TL_WRITE_ALLOW_WRITE, which is what sequence tables are using.
- Added VCOL_NOT_VIRTUAL option to ensure that sequence functions can't
  be used with virtual columns
- More sequence tests
2017-12-22 14:56:58 +02:00

111 lines
2.5 KiB
Text

#
# Testing sequence in DEFAULT clause
#
--source include/have_sequence.inc
drop table if exists t1,s1,s2;
drop view if exists v1;
--echo #
--echo # Test DEFAULT
--echo #
CREATE SEQUENCE s1 nocache engine=myisam;
CREATE table t1 (a int default next value for s1, b int);
show create table t1;
insert into t1 SET b=1;
insert into t1 SET b=2;
insert into t1 (b) values (3),(4);
select * from t1;
update t1 set b=5 where a=1;
delete from t1 where b=1;
select * from t1;
--echo #
--echo # Executing DEFAULT function
--echo #
INSERT into t1 values(default(a),10);
INSERT into t1 values(default(a),default(a));
update t1 set a=default(a), b=12 where b=2;
select * from t1;
select default(a), a, b from t1;
select * from s1;
select * from t1 where default(a) > 0;
select * from s1;
--echo #
--echo # View
--echo #
create view v1 as select * from t1;
insert into v1 set b=20;
select * from v1;
drop view v1;
--echo #
--echo # Alter table
--echo #
CREATE SEQUENCE s2 nocache engine=myisam;
alter table t1 add column c int default next value for s2, add column d int default previous value for s2;
show create table t1;
select * from t1;
drop sequence s2;
show create table t1;
drop table t1;
drop sequence s1;
--echo #
--echo # LOCK TABLES
--echo #
CREATE SEQUENCE s1 nocache engine=myisam;
CREATE table t1 (a int default next value for s1, b int);
insert into t1 (b) values (3),(4);
LOCK TABLE t1 WRITE;
--error ER_TABLE_NOT_LOCKED
insert into t1 (b) values (5),(6);
UNLOCK TABLES;
LOCK TABLE t1 WRITE, s1 WRITE;
insert into t1 (b) values (5),(6);
select default(a) from t1;
UNLOCK TABLES;
LOCK TABLE t1 READ;
--error ER_TABLE_NOT_LOCKED
insert into t1 (b) values (5),(6);
--error ER_TABLE_NOT_LOCKED
select default(a) from t1;
UNLOCK TABLES;
LOCK TABLE t1 READ, s1 read;
--error ER_TABLE_NOT_LOCKED_FOR_WRITE
insert into t1 (b) values (5),(6);
--error ER_TABLE_NOT_LOCKED_FOR_WRITE
select default(a) from t1;
UNLOCK TABLES;
drop table t1;
drop sequence s1;
--echo #
--echo # Wrong usage of default
--echo #
--error ER_NO_SUCH_TABLE
CREATE table t1 (a int default next value for s1, b int);
CREATE SEQUENCE s1 nocache engine=myisam;
CREATE table t1 (a int default next value for s1, b int);
DROP SEQUENCE s1;
--error ER_NO_SUCH_TABLE
insert into t1 (b) values (5),(6);
--error ER_NO_SUCH_TABLE
ALTER TABLE t1 add column c int;
CREATE SEQUENCE s1 nocache engine=myisam;
ALTER TABLE t1 add column c int;
--error ER_NO_SUCH_TABLE
ALTER TABLE t1 add column d int default next value for s_not_exits;
drop table t1;
drop sequence s1;