split create_table.sql usecase

This commit is contained in:
chenxiaobin19
2023-05-05 17:15:06 +08:00
parent f4a28676e9
commit ff67adb323
7 changed files with 1969 additions and 1973 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,222 @@
-- test mysql "create table as"
-- a format
create schema a_createas;
set current_schema to 'a_createas';
create table t_base(col1 int, col2 int, col3 int);
create table t1 as select * from t_base;
select * from t1;
col1 | col2 | col3
------+------+------
(0 rows)
create table t2(col) as select * from t_base;
select * from t2;
col | col2 | col3
-----+------+------
(0 rows)
-- fail
create table t3(col1,col2,col3,col4) as select * from t_base;
ERROR: CREATE TABLE AS specifies too many column names
create table t4(col int) as select * from t_base;
ERROR: define column_definition is supported only in B-format database
reset current_schema;
drop schema a_createas cascade;
NOTICE: drop cascades to 3 other objects
DETAIL: drop cascades to table a_createas.t_base
drop cascades to table a_createas.t1
drop cascades to table a_createas.t2
-- b format
create database b_createas dbcompatibility 'B';
\c b_createas
create table t_base(col1 int, col2 int, col3 int);
create table t1 as select * from t_base;
select * from t1;
col1 | col2 | col3
------+------+------
(0 rows)
create table t2(col) as select * from t_base;
select * from t2;
col | col2 | col3
-----+------+------
(0 rows)
create table t3(col int) as select * from t_base;
select * from t3;
col | col1 | col2 | col3
-----+------+------+------
(0 rows)
create table t4(col1 int) as select * from t_base;
select * from t4;
col1 | col2 | col3
------+------+------
(0 rows)
-- fail
create table t5() as select * from t_base;
ERROR: syntax error at or near ")"
LINE 1: create table t5() as select * from t_base;
^
create table t6(col1 int) as select col1,* from t_base;
ERROR: column "col1" specified more than once
LINE 1: create table t6(col1 int) as select col1,* from t_base;
^
-- duplicate key
insert into t_base values(1,1,10),(1,2,9),(2,2,8),(2,1,7),(1,1,6);
-- error
create table t7(col1 int unique) as select * from t_base;
NOTICE: CREATE TABLE / UNIQUE will create implicit index "t7_col1_key" for table "t7"
ERROR: duplicate key value violates unique constraint "t7_col1_key"
DETAIL: Key (col1)=(1) already exists.
-- ignore
create table t8(col1 int unique) ignore as select * from t_base;
NOTICE: CREATE TABLE / UNIQUE will create implicit index "t8_col1_key" for table "t8"
select * from t8;
col1 | col2 | col3
------+------+------
1 | 1 | 10
2 | 2 | 8
(2 rows)
-- replace
create table t9(col1 int unique) replace as select * from t_base;
NOTICE: CREATE TABLE / UNIQUE will create implicit index "t9_col1_key" for table "t9"
select * from t9 order by col3;
col1 | col2 | col3
------+------+------
1 | 1 | 6
2 | 1 | 7
(2 rows)
create table t10(col1 int unique, col2 int unique) replace as select * from t_base;
NOTICE: CREATE TABLE / UNIQUE will create implicit index "t10_col1_key" for table "t10"
NOTICE: CREATE TABLE / UNIQUE will create implicit index "t10_col2_key" for table "t10"
select * from t10 order by col3;
col1 | col2 | col3
------+------+------
1 | 1 | 6
(1 row)
-- foreign key
create table ftable(col int primary key);
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "ftable_pkey" for table "ftable"
create table t11(col int, foreign key(col) references ftable(col)) as select * from t_base;
ERROR: CREATE TABLE AS SELECT is not allowed with Foreign Key
create table t12(col int references ftable(col)) as select * from t_base;
ERROR: CREATE TABLE AS SELECT is not allowed with Foreign Key
create table t13(foreign key(col1) references ftable(col)) as select * from t_base;
ERROR: CREATE TABLE AS SELECT is not allowed with Foreign Key
-- table like
create table t14(like t_base) as select * from t_base;
ERROR: CREATE TABLE AS SELECT is not allowed with Tablelike Clause
-- with no data
create table t15(id int, name char(8));
insert into t15(id) select generate_series(1,10);
create table t16(col1 int, id int) as select * from t15 where id with no data;
select * from t16;
col1 | id | name
------+----+------
(0 rows)
create table t17(col1 int, id int unique) replace as select * from t15 where id with no data;
NOTICE: CREATE TABLE / UNIQUE will create implicit index "t17_id_key" for table "t17"
select * from t17;
col1 | id | name
------+----+------
(0 rows)
-- union all
create table test1(id int,name varchar(10),score numeric,date1 date,c1 bytea);
insert into test1 values(1,'aaa',97.1,'1999-12-12','0101');
insert into test1 values(5,'bbb',36.9,'1998-01-12','0110');
insert into test1 values(30,'ooo',90.1,'2023-01-30','1001');
insert into test1 values(6,'hhh',60,'2022-12-22','1010');
insert into test1 values(7,'fff',71,'2001-11-23','1011');
insert into test1 values(-1,'yaya',77.7,'2008-09-10','1100');
insert into test1 values(7,'fff',71,'2001-11-23','1011');
insert into test1 values(null,null,null,null,null);
create table test2(id int,name varchar(10),score numeric,date1 date,c1 bytea);
insert into test2 values(1,'aaa',99.1,'1998-12-12','0101');
insert into test2 values(2,'hhh',36.9,'1996-01-12','0110');
insert into test2 values(3,'ddd',89.2,'2000-03-12','0111');
insert into test2 values(7,'uuu',60.9,'1997-01-01','1000');
insert into test2 values(11,'eee',71,'2011-11-20','1011');
insert into test2 values(-1,'yaya',76.7,'2008-09-10','1100');
insert into test2 values(7,'uuu',60.9,'1997-01-01','1000');
insert into test2 values(null,null,null,null,null);
create table tb1(col1 int,id int) as select * from test1 where id<4 union all select * from test2 where score>80 order by id,score;
select * from tb1 order by id;
col1 | id | name | score | date1 | c1
------+----+------+-------+------------+------------
| -1 | yaya | 77.7 | 09-10-2008 | \x31313030
| 1 | aaa | 97.1 | 12-12-1999 | \x30313031
| 1 | aaa | 99.1 | 12-12-1998 | \x30313031
| 3 | ddd | 89.2 | 03-12-2000 | \x30313131
(4 rows)
create table tb2(col1 int,id int unique) replace as select * from test1 where id<4 union all select * from test2 where score>80 order by id,score;
NOTICE: CREATE TABLE / UNIQUE will create implicit index "tb2_id_key" for table "tb2"
select * from tb2 order by id;
col1 | id | name | score | date1 | c1
------+----+------+-------+------------+------------
| -1 | yaya | 77.7 | 09-10-2008 | \x31313030
| 1 | aaa | 99.1 | 12-12-1998 | \x30313031
| 3 | ddd | 89.2 | 03-12-2000 | \x30313131
(3 rows)
-- test update
create table tb_primary(a int primary key, b int);
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "tb_primary_pkey" for table "tb_primary"
create table tb_unique(a int unique, b int);
NOTICE: CREATE TABLE / UNIQUE will create implicit index "tb_unique_a_key" for table "tb_unique"
insert into tb_primary values(1,2),(2,4),(3,6);
insert into tb_unique values(1,2),(2,4),(3,6);
-- error
insert into tb_primary values(1,1);
ERROR: duplicate key value violates unique constraint "tb_primary_pkey"
DETAIL: Key (a)=(1) already exists.
insert into tb_unique values(1,1);
ERROR: duplicate key value violates unique constraint "tb_unique_a_key"
DETAIL: Key (a)=(1) already exists.
-- UPDATE nothing
insert into tb_primary values(1,1) ON DUPLICATE KEY UPDATE NOTHING;
insert into tb_unique values(1,1) ON DUPLICATE KEY UPDATE NOTHING;
select * from tb_primary;
a | b
---+---
1 | 2
2 | 4
3 | 6
(3 rows)
select * from tb_unique;
a | b
---+---
1 | 2
2 | 4
3 | 6
(3 rows)
-- UPDATE
insert into tb_primary values(1,1) ON DUPLICATE KEY UPDATE a = 1, b = 1;
insert into tb_unique values(1,1) ON DUPLICATE KEY UPDATE a = 1, b = 1;
select * from tb_primary;
a | b
---+---
2 | 4
3 | 6
1 | 1
(3 rows)
select * from tb_unique;
a | b
---+---
2 | 4
3 | 6
1 | 1
(3 rows)
\c postgres
drop database b_createas;

View File

@ -397,7 +397,7 @@ test: partiton_pathkey_col_plan partiton_pathkey_col_randomexec partiton_pathkey
# ---------- # ----------
#test: type_sanity #test: type_sanity
#test: create_function_1 #test: create_function_1
test: create_table test: create_table create_table_2 create_table_3
test: temp__4 test: temp__4
#test: copy# #test: copy#

View File

@ -351,14 +351,12 @@ DROP TABLE hw_create_as_test1;
-- Zero column table is not supported any more. -- Zero column table is not supported any more.
CREATE TABLE zero_column_table_test1(); CREATE TABLE zero_column_table_test1();
CREATE TABLE zero_column_table_test2();
CREATE TABLE zero_column_table_test3(a INT); CREATE TABLE zero_column_table_test3(a INT);
ALTER TABLE zero_column_table_test3 DROP COLUMN a; ALTER TABLE zero_column_table_test3 DROP COLUMN a;
DROP TABLE zero_column_table_test3; DROP TABLE zero_column_table_test3;
CREATE TABLE zero_column_table_test6() with (orientation = column); CREATE TABLE zero_column_table_test6() with (orientation = column);
CREATE TABLE zero_column_table_test7() with (orientation = column);
CREATE TABLE zero_column_table_test8(a INT) with (orientation = column); CREATE TABLE zero_column_table_test8(a INT) with (orientation = column);
ALTER TABLE zero_column_table_test8 DROP COLUMN a; ALTER TABLE zero_column_table_test8 DROP COLUMN a;
DROP TABLE zero_column_table_test8; DROP TABLE zero_column_table_test8;
@ -895,690 +893,3 @@ PARTITION BY RANGE(f1)
); );
\d+ test_p_unique \d+ test_p_unique
drop table test_p_unique; drop table test_p_unique;
-- b compatibility case
drop database if exists b;
create database b dbcompatibility 'b';
\c b
-- test primary key in M mode
-- test [index_type]
create table test_primary(f11 int, f12 varchar(20), f13 bool, constraint con_t_pri primary key using btree(f11, f12));
\d+ test_primary
drop table test_primary;
create table test_primary(f11 int, f12 varchar(20), f13 bool, constraint con_t_pri primary key using hash(f11, f12));
create table test_primary(f11 int, f12 varchar(20), f13 bool, constraint con_t_pri primary key using hash(f11, f12)) with (orientation = column);
-- test [CONSTRAINT [constraint_name]]
create table test_primary(f11 int, f12 varchar(20), f13 bool, constraint con_t_pri primary key(f11));
\d+ test_primary
drop table test_primary;
create table test_primary(f11 int, f12 varchar(20), f13 bool, primary key(f11));
\d+ test_primary
drop table test_primary;
create table test_primary(f11 int, f12 varchar(20), f13 bool, constraint primary key(f11));
\d+ test_primary
drop table test_primary;
-- test [ASC|DESC]
create table test_primary(f11 int, f12 varchar(20), f13 bool, constraint con_t_pri primary key using btree(f11 desc, f12 asc));
\d+ test_primary
drop table test_primary;
-- test expression, error
create table test_primary(f11 int, f12 varchar(20), f13 bool, primary key ((abs(f11))));
create table test_primary(f11 int, f12 varchar(20), f13 bool, primary key ((f11 * 2 + 1)));
-- test foreign key in M mode
-- test [CONSTRAINT [constraint_name]] and [index_name]
create table test_primary(f11 int, f12 varchar(20), f13 bool, primary key (f11));
create table test_foreign(f21 int, f22 timestamp, constraint con_t_foreign foreign key f_t_foreign (f21) references test_primary(f11));
\d+ test_foreign
drop table test_foreign;
create table test_foreign(f21 int, f22 timestamp, constraint con_t_foreign foreign key (f21) references test_primary(f11));
\d+ test_foreign
drop table test_foreign;
create table test_foreign(f21 int, f22 timestamp, constraint foreign key f_t_foreign (f21) references test_primary(f11));
\d+ test_foreign
drop table test_foreign;
create table test_foreign(f21 int, f22 timestamp, foreign key f_t_foreign (f21) references test_primary(f11));
\d+ test_foreign
drop table test_foreign;
create table test_foreign(f21 int, f22 timestamp, foreign key (f21) references test_primary(f11));
\d+ test_foreign
drop table test_foreign;
drop table test_primary;
-- test unique key in M mode
-- test [index_type]
create table test_unique(f31 int, f32 varchar(20), constraint con_t_unique unique using btree(f31, f32));
\d+ test_unique
drop table test_unique;
create table test_unique(f31 int, f32 varchar(20), constraint con_t_unique unique using hash(f31, f32));
create table test_unique(f31 int, f32 varchar(20), constraint con_t_unique unique using hash(f31, f32)) with (orientation = column);
-- test [CONSTRAINT [constraint_name]] and [index_name]
create table test_unique(f31 int, f32 varchar(20), constraint con_t_unique unique u_t_unique(f31, f32));
\d+ test_unique
drop table test_unique;
create table test_unique(f31 int, f32 varchar(20), unique u_t_unique(f31, f32));
\d+ test_unique
drop table test_unique;
create table test_unique(f31 int, f32 varchar(20), constraint con_t_unique unique (f31, f32));
\d+ test_unique
drop table test_unique;
create table test_unique(f31 int, f32 varchar(20), constraint unique (f31, f32));
\d+ test_unique
drop table test_unique;
create table test_unique(f31 int, f32 varchar(20), unique (f31, f32));
\d+ test_unique
drop table test_unique;
-- test [ASC|DESC]
create table test_unique(f31 int, f32 varchar(20), unique (f31 desc, f32 asc));
\d+ test_unique
drop table test_unique;
-- test expression
create table test_unique(f31 int, f32 varchar(20), unique ((abs(f31)) desc, (lower(f32)) asc));
\d+ test_unique
drop table test_unique;
create table test_unique(f31 int, f32 varchar(20), unique ((f31 * 2 + 1) desc, (lower(f32)) asc));
\d+ test_unique
drop table test_unique;
-- test unreserved_keyword index and key
-- error
create table test_unique(f31 int, f32 varchar(20), constraint con_t_unique unique key using btree(f31));
create table test_unique(f31 int, f32 varchar(20), constraint con_t_unique unique index using btree(f31));
-- partition table
-- test primary key in M mode
-- test [index_type]
CREATE TABLE test_p_primary
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
constraint con_t_pri primary key using btree(f1, f2, f3)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
\d+ test_p_primary
drop table test_p_primary;
CREATE TABLE test_p_primary
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
constraint con_t_pri primary key using hash(f1, f2, f3)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
CREATE TABLE test_p_primary
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
constraint con_t_pri primary key using hash(f1, f2, f3)
)
with (orientation = column)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
-- test [CONSTRAINT [constraint_name]]
CREATE TABLE test_p_primary
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
constraint con_t_pri primary key(f1, f2, f3)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
\d+ test_p_primary
drop table test_p_primary;
CREATE TABLE test_p_primary
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
primary key(f1, f2, f3)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
\d+ test_p_primary
drop table test_p_primary;
CREATE TABLE test_p_primary
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
constraint primary key(f1, f2, f3)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
\d+ test_p_primary
drop table test_p_primary;
-- test [ASC|DESC]
CREATE TABLE test_p_primary
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
constraint con_t_pri primary key using btree(f1 desc, f2 asc, f3)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
\d+ test_p_primary
drop table test_p_primary;
-- test expression, error
CREATE TABLE test_p_primary
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
constraint con_t_pri primary key using btree((abs(f1)) desc, (f2 * 2 + 1) asc, f3)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
-- test foreign key in M mode
-- test [CONSTRAINT [constraint_name]] and [index_name]
CREATE TABLE test_p_primary
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
constraint con_t_pri primary key(f1)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
CREATE TABLE test_p_foreign
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
constraint con_t_foreign foreign key f_t_foreign(f1) references test_p_primary(f1)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
\d+ test_p_foreign
drop table test_p_foreign;
CREATE TABLE test_p_foreign
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
constraint con_t_foreign foreign key(f1) references test_p_primary(f1)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
\d+ test_p_foreign
drop table test_p_foreign;
CREATE TABLE test_p_foreign
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
constraint foreign key f_t_foreign(f1) references test_p_primary(f1)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
\d+ test_p_foreign
drop table test_p_foreign;
CREATE TABLE test_p_foreign
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
foreign key f_t_foreign(f1) references test_p_primary(f1)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
\d+ test_p_foreign
drop table test_p_foreign;
CREATE TABLE test_p_foreign
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
foreign key(f1) references test_p_primary(f1)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
\d+ test_p_foreign
drop table test_p_foreign;
drop table test_p_primary;
-- test unique key in M mode
-- test [index_type]
CREATE TABLE test_p_unique
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
constraint con_t_unique unique using btree(f1, f2, f3)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
\d+ test_p_unique
drop table test_p_unique;
-- error
CREATE TABLE test_p_unique
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
constraint con_t_unique unique using hash(f1, f2, f3)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
CREATE TABLE test_p_unique
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
constraint con_t_unique unique using hash(f1, f2, f3)
)
with (orientation = column)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
-- test [CONSTRAINT [constraint_name]] and [index_name]
CREATE TABLE test_p_unique
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
constraint con_t_unique unique u_t_unique(f1, f2, f3)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
\d+ test_p_unique
drop table test_p_unique;
CREATE TABLE test_p_unique
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
unique u_t_unique(f1, f2, f3)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
\d+ test_p_unique
drop table test_p_unique;
CREATE TABLE test_p_unique
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
constraint con_t_unique unique(f1, f2, f3)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
\d+ test_p_unique
drop table test_p_unique;
CREATE TABLE test_p_unique
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
constraint unique(f1, f2, f3)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
\d+ test_p_unique
drop table test_p_unique;
CREATE TABLE test_p_unique
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
unique(f1, f2, f3)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
\d+ test_p_unique
drop table test_p_unique;
-- test [ASC|DESC]
CREATE TABLE test_p_unique
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
unique(f1 desc, f2 asc, f3)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
\d+ test_p_unique
drop table test_p_unique;
-- test expression
CREATE TABLE test_p_unique
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
unique((abs(f1)) desc, (f2 * 2 + 1) asc, f3)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
\d+ test_p_unique
drop table test_p_unique;
-- test unreserved_keyword index and key
-- error
CREATE TABLE test_p_unique
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
constraint con_t_unique unique key using btree(f1)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
CREATE TABLE test_p_unique
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
constraint con_t_unique unique index using btree(f1)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
create table test_unique(f31 int, f32 varchar(20), constraint con_t_unique unique using btree(f31, f32) comment 'unique index' using btree);
\d+ test_unique
drop table test_unique;
create table test_unique(f31 int, f32 varchar(20), constraint con_t_unique unique (f31, f32) comment 'unique index' using btree);
\d+ test_unique
drop table test_unique;
create table test_unique(f31 int, f32 varchar(20), constraint con_t_unique unique (f31, f32) comment 'unique index' using btree using btree);
\d+ test_unique
drop table test_unique;
create table test_primary(f11 int, f12 varchar(20), f13 bool, constraint con_t_pri primary key using btree(f11 desc, f12 asc) comment 'primary key' using btree);
\d+ test_primary
drop table test_primary;
create table test_primary(f11 int, f12 varchar(20), f13 bool, constraint con_t_pri primary key (f11 desc, f12 asc) comment 'primary key' using btree);
\d+ test_primary
drop table test_primary;
create table test_primary(f11 int, f12 varchar(20), f13 bool, constraint con_t_pri primary key using btree(f11 desc, f12 asc) comment 'primary key' using btree using btree);
\d+ test_primary
drop table test_primary;
\c postgres
-- test mysql "create table as"
-- a format
create database a_createas dbcompatibility 'A';
\c a_createas
create table t_base(col1 int, col2 int, col3 int);
create table t1 as select * from t_base;
select * from t1;
create table t2(col) as select * from t_base;
select * from t2;
-- fail
create table t3(col1,col2,col3,col4) as select * from t_base;
create table t4(col int) as select * from t_base;
-- b format
create database b_createas dbcompatibility 'B';
\c b_createas
create table t_base(col1 int, col2 int, col3 int);
create table t1 as select * from t_base;
select * from t1;
create table t2(col) as select * from t_base;
select * from t2;
create table t3(col int) as select * from t_base;
select * from t3;
create table t4(col1 int) as select * from t_base;
select * from t4;
-- fail
create table t5() as select * from t_base;
create table t6(col1 int) as select col1,* from t_base;
-- duplicate key
insert into t_base values(1,1,10),(1,2,9),(2,2,8),(2,1,7),(1,1,6);
-- error
create table t7(col1 int unique) as select * from t_base;
-- ignore
create table t8(col1 int unique) ignore as select * from t_base;
select * from t8;
-- replace
create table t9(col1 int unique) replace as select * from t_base;
select * from t9 order by col3;
create table t10(col1 int unique, col2 int unique) replace as select * from t_base;
select * from t10 order by col3;
-- foreign key
create table ftable(col int primary key);
create table t11(col int, foreign key(col) references ftable(col)) as select * from t_base;
create table t12(col int references ftable(col)) as select * from t_base;
create table t13(foreign key(col1) references ftable(col)) as select * from t_base;
-- table like
create table t14(like t_base) as select * from t_base;
-- with no data
create table t15(id int, name char(8));
insert into t15(id) select generate_series(1,10);
create table t16(col1 int, id int) as select * from t15 where id with no data;
select * from t16;
create table t17(col1 int, id int unique) replace as select * from t15 where id with no data;
select * from t17;
-- union all
create table test1(id int,name varchar(10),score numeric,date1 date,c1 bytea);
insert into test1 values(1,'aaa',97.1,'1999-12-12','0101');
insert into test1 values(5,'bbb',36.9,'1998-01-12','0110');
insert into test1 values(30,'ooo',90.1,'2023-01-30','1001');
insert into test1 values(6,'hhh',60,'2022-12-22','1010');
insert into test1 values(7,'fff',71,'2001-11-23','1011');
insert into test1 values(-1,'yaya',77.7,'2008-09-10','1100');
insert into test1 values(7,'fff',71,'2001-11-23','1011');
insert into test1 values(null,null,null,null,null);
create table test2(id int,name varchar(10),score numeric,date1 date,c1 bytea);
insert into test2 values(1,'aaa',99.1,'1998-12-12','0101');
insert into test2 values(2,'hhh',36.9,'1996-01-12','0110');
insert into test2 values(3,'ddd',89.2,'2000-03-12','0111');
insert into test2 values(7,'uuu',60.9,'1997-01-01','1000');
insert into test2 values(11,'eee',71,'2011-11-20','1011');
insert into test2 values(-1,'yaya',76.7,'2008-09-10','1100');
insert into test2 values(7,'uuu',60.9,'1997-01-01','1000');
insert into test2 values(null,null,null,null,null);
create table tb1(col1 int,id int) as select * from test1 where id<4 union all select * from test2 where score>80 order by id,score;
select * from tb1 order by id;
create table tb2(col1 int,id int unique) replace as select * from test1 where id<4 union all select * from test2 where score>80 order by id,score;
select * from tb2 order by id;
-- test update
create table tb_primary(a int primary key, b int);
create table tb_unique(a int unique, b int);
insert into tb_primary values(1,2),(2,4),(3,6);
insert into tb_unique values(1,2),(2,4),(3,6);
-- error
insert into tb_primary values(1,1);
insert into tb_unique values(1,1);
-- UPDATE nothing
insert into tb_primary values(1,1) ON DUPLICATE KEY UPDATE NOTHING;
insert into tb_unique values(1,1) ON DUPLICATE KEY UPDATE NOTHING;
select * from tb_primary;
select * from tb_unique;
-- UPDATE
insert into tb_primary values(1,1) ON DUPLICATE KEY UPDATE a = 1, b = 1;
insert into tb_unique values(1,1) ON DUPLICATE KEY UPDATE a = 1, b = 1;
select * from tb_primary;
select * from tb_unique;
\c postgres
drop database a_createas;
drop database b_createas;

View File

@ -0,0 +1,574 @@
-- b compatibility case
drop database if exists b;
create database b dbcompatibility 'b';
\c b
-- test primary key in M mode
-- test [index_type]
create table test_primary(f11 int, f12 varchar(20), f13 bool, constraint con_t_pri primary key using btree(f11, f12));
\d+ test_primary
drop table test_primary;
create table test_primary(f11 int, f12 varchar(20), f13 bool, constraint con_t_pri primary key using hash(f11, f12));
create table test_primary(f11 int, f12 varchar(20), f13 bool, constraint con_t_pri primary key using hash(f11, f12)) with (orientation = column);
-- test [CONSTRAINT [constraint_name]]
create table test_primary(f11 int, f12 varchar(20), f13 bool, constraint con_t_pri primary key(f11));
\d+ test_primary
drop table test_primary;
create table test_primary(f11 int, f12 varchar(20), f13 bool, primary key(f11));
\d+ test_primary
drop table test_primary;
create table test_primary(f11 int, f12 varchar(20), f13 bool, constraint primary key(f11));
\d+ test_primary
drop table test_primary;
-- test [ASC|DESC]
create table test_primary(f11 int, f12 varchar(20), f13 bool, constraint con_t_pri primary key using btree(f11 desc, f12 asc));
\d+ test_primary
drop table test_primary;
-- test expression, error
create table test_primary(f11 int, f12 varchar(20), f13 bool, primary key ((abs(f11))));
create table test_primary(f11 int, f12 varchar(20), f13 bool, primary key ((f11 * 2 + 1)));
-- test foreign key in M mode
-- test [CONSTRAINT [constraint_name]] and [index_name]
create table test_primary(f11 int, f12 varchar(20), f13 bool, primary key (f11));
create table test_foreign(f21 int, f22 timestamp, constraint con_t_foreign foreign key f_t_foreign (f21) references test_primary(f11));
\d+ test_foreign
drop table test_foreign;
create table test_foreign(f21 int, f22 timestamp, constraint con_t_foreign foreign key (f21) references test_primary(f11));
\d+ test_foreign
drop table test_foreign;
create table test_foreign(f21 int, f22 timestamp, constraint foreign key f_t_foreign (f21) references test_primary(f11));
\d+ test_foreign
drop table test_foreign;
create table test_foreign(f21 int, f22 timestamp, foreign key f_t_foreign (f21) references test_primary(f11));
\d+ test_foreign
drop table test_foreign;
create table test_foreign(f21 int, f22 timestamp, foreign key (f21) references test_primary(f11));
\d+ test_foreign
drop table test_foreign;
drop table test_primary;
-- test unique key in M mode
-- test [index_type]
create table test_unique(f31 int, f32 varchar(20), constraint con_t_unique unique using btree(f31, f32));
\d+ test_unique
drop table test_unique;
create table test_unique(f31 int, f32 varchar(20), constraint con_t_unique unique using hash(f31, f32));
create table test_unique(f31 int, f32 varchar(20), constraint con_t_unique unique using hash(f31, f32)) with (orientation = column);
-- test [CONSTRAINT [constraint_name]] and [index_name]
create table test_unique(f31 int, f32 varchar(20), constraint con_t_unique unique u_t_unique(f31, f32));
\d+ test_unique
drop table test_unique;
create table test_unique(f31 int, f32 varchar(20), unique u_t_unique(f31, f32));
\d+ test_unique
drop table test_unique;
create table test_unique(f31 int, f32 varchar(20), constraint con_t_unique unique (f31, f32));
\d+ test_unique
drop table test_unique;
create table test_unique(f31 int, f32 varchar(20), constraint unique (f31, f32));
\d+ test_unique
drop table test_unique;
create table test_unique(f31 int, f32 varchar(20), unique (f31, f32));
\d+ test_unique
drop table test_unique;
-- test [ASC|DESC]
create table test_unique(f31 int, f32 varchar(20), unique (f31 desc, f32 asc));
\d+ test_unique
drop table test_unique;
-- test expression
create table test_unique(f31 int, f32 varchar(20), unique ((abs(f31)) desc, (lower(f32)) asc));
\d+ test_unique
drop table test_unique;
create table test_unique(f31 int, f32 varchar(20), unique ((f31 * 2 + 1) desc, (lower(f32)) asc));
\d+ test_unique
drop table test_unique;
-- test unreserved_keyword index and key
-- error
create table test_unique(f31 int, f32 varchar(20), constraint con_t_unique unique key using btree(f31));
create table test_unique(f31 int, f32 varchar(20), constraint con_t_unique unique index using btree(f31));
-- partition table
-- test primary key in M mode
-- test [index_type]
CREATE TABLE test_p_primary
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
constraint con_t_pri primary key using btree(f1, f2, f3)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
\d+ test_p_primary
drop table test_p_primary;
CREATE TABLE test_p_primary
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
constraint con_t_pri primary key using hash(f1, f2, f3)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
CREATE TABLE test_p_primary
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
constraint con_t_pri primary key using hash(f1, f2, f3)
)
with (orientation = column)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
-- test [CONSTRAINT [constraint_name]]
CREATE TABLE test_p_primary
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
constraint con_t_pri primary key(f1, f2, f3)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
\d+ test_p_primary
drop table test_p_primary;
CREATE TABLE test_p_primary
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
primary key(f1, f2, f3)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
\d+ test_p_primary
drop table test_p_primary;
CREATE TABLE test_p_primary
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
constraint primary key(f1, f2, f3)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
\d+ test_p_primary
drop table test_p_primary;
-- test [ASC|DESC]
CREATE TABLE test_p_primary
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
constraint con_t_pri primary key using btree(f1 desc, f2 asc, f3)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
\d+ test_p_primary
drop table test_p_primary;
-- test expression, error
CREATE TABLE test_p_primary
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
constraint con_t_pri primary key using btree((abs(f1)) desc, (f2 * 2 + 1) asc, f3)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
-- test foreign key in M mode
-- test [CONSTRAINT [constraint_name]] and [index_name]
CREATE TABLE test_p_primary
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
constraint con_t_pri primary key(f1)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
CREATE TABLE test_p_foreign
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
constraint con_t_foreign foreign key f_t_foreign(f1) references test_p_primary(f1)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
\d+ test_p_foreign
drop table test_p_foreign;
CREATE TABLE test_p_foreign
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
constraint con_t_foreign foreign key(f1) references test_p_primary(f1)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
\d+ test_p_foreign
drop table test_p_foreign;
CREATE TABLE test_p_foreign
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
constraint foreign key f_t_foreign(f1) references test_p_primary(f1)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
\d+ test_p_foreign
drop table test_p_foreign;
CREATE TABLE test_p_foreign
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
foreign key f_t_foreign(f1) references test_p_primary(f1)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
\d+ test_p_foreign
drop table test_p_foreign;
CREATE TABLE test_p_foreign
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
foreign key(f1) references test_p_primary(f1)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
\d+ test_p_foreign
drop table test_p_foreign;
drop table test_p_primary;
-- test unique key in M mode
-- test [index_type]
CREATE TABLE test_p_unique
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
constraint con_t_unique unique using btree(f1, f2, f3)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
\d+ test_p_unique
drop table test_p_unique;
-- error
CREATE TABLE test_p_unique
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
constraint con_t_unique unique using hash(f1, f2, f3)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
CREATE TABLE test_p_unique
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
constraint con_t_unique unique using hash(f1, f2, f3)
)
with (orientation = column)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
-- test [CONSTRAINT [constraint_name]] and [index_name]
CREATE TABLE test_p_unique
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
constraint con_t_unique unique u_t_unique(f1, f2, f3)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
\d+ test_p_unique
drop table test_p_unique;
CREATE TABLE test_p_unique
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
unique u_t_unique(f1, f2, f3)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
\d+ test_p_unique
drop table test_p_unique;
CREATE TABLE test_p_unique
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
constraint con_t_unique unique(f1, f2, f3)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
\d+ test_p_unique
drop table test_p_unique;
CREATE TABLE test_p_unique
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
constraint unique(f1, f2, f3)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
\d+ test_p_unique
drop table test_p_unique;
CREATE TABLE test_p_unique
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
unique(f1, f2, f3)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
\d+ test_p_unique
drop table test_p_unique;
-- test [ASC|DESC]
CREATE TABLE test_p_unique
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
unique(f1 desc, f2 asc, f3)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
\d+ test_p_unique
drop table test_p_unique;
-- test expression
CREATE TABLE test_p_unique
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
unique((abs(f1)) desc, (f2 * 2 + 1) asc, f3)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
\d+ test_p_unique
drop table test_p_unique;
-- test unreserved_keyword index and key
-- error
CREATE TABLE test_p_unique
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
constraint con_t_unique unique key using btree(f1)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
CREATE TABLE test_p_unique
(
f1 INTEGER,
f2 INTEGER,
f3 INTEGER,
constraint con_t_unique unique index using btree(f1)
)
PARTITION BY RANGE(f1)
(
PARTITION P1 VALUES LESS THAN(2450815),
PARTITION P2 VALUES LESS THAN(2451179),
PARTITION P3 VALUES LESS THAN(2451544),
PARTITION P4 VALUES LESS THAN(MAXVALUE)
);
create table test_unique(f31 int, f32 varchar(20), constraint con_t_unique unique using btree(f31, f32) comment 'unique index' using btree);
\d+ test_unique
drop table test_unique;
create table test_unique(f31 int, f32 varchar(20), constraint con_t_unique unique (f31, f32) comment 'unique index' using btree);
\d+ test_unique
drop table test_unique;
create table test_unique(f31 int, f32 varchar(20), constraint con_t_unique unique (f31, f32) comment 'unique index' using btree using btree);
\d+ test_unique
drop table test_unique;
create table test_primary(f11 int, f12 varchar(20), f13 bool, constraint con_t_pri primary key using btree(f11 desc, f12 asc) comment 'primary key' using btree);
\d+ test_primary
drop table test_primary;
create table test_primary(f11 int, f12 varchar(20), f13 bool, constraint con_t_pri primary key (f11 desc, f12 asc) comment 'primary key' using btree);
\d+ test_primary
drop table test_primary;
create table test_primary(f11 int, f12 varchar(20), f13 bool, constraint con_t_pri primary key using btree(f11 desc, f12 asc) comment 'primary key' using btree using btree);
\d+ test_primary
drop table test_primary;

View File

@ -0,0 +1,111 @@
-- test mysql "create table as"
-- a format
create schema a_createas;
set current_schema to 'a_createas';
create table t_base(col1 int, col2 int, col3 int);
create table t1 as select * from t_base;
select * from t1;
create table t2(col) as select * from t_base;
select * from t2;
-- fail
create table t3(col1,col2,col3,col4) as select * from t_base;
create table t4(col int) as select * from t_base;
reset current_schema;
drop schema a_createas cascade;
-- b format
create database b_createas dbcompatibility 'B';
\c b_createas
create table t_base(col1 int, col2 int, col3 int);
create table t1 as select * from t_base;
select * from t1;
create table t2(col) as select * from t_base;
select * from t2;
create table t3(col int) as select * from t_base;
select * from t3;
create table t4(col1 int) as select * from t_base;
select * from t4;
-- fail
create table t5() as select * from t_base;
create table t6(col1 int) as select col1,* from t_base;
-- duplicate key
insert into t_base values(1,1,10),(1,2,9),(2,2,8),(2,1,7),(1,1,6);
-- error
create table t7(col1 int unique) as select * from t_base;
-- ignore
create table t8(col1 int unique) ignore as select * from t_base;
select * from t8;
-- replace
create table t9(col1 int unique) replace as select * from t_base;
select * from t9 order by col3;
create table t10(col1 int unique, col2 int unique) replace as select * from t_base;
select * from t10 order by col3;
-- foreign key
create table ftable(col int primary key);
create table t11(col int, foreign key(col) references ftable(col)) as select * from t_base;
create table t12(col int references ftable(col)) as select * from t_base;
create table t13(foreign key(col1) references ftable(col)) as select * from t_base;
-- table like
create table t14(like t_base) as select * from t_base;
-- with no data
create table t15(id int, name char(8));
insert into t15(id) select generate_series(1,10);
create table t16(col1 int, id int) as select * from t15 where id with no data;
select * from t16;
create table t17(col1 int, id int unique) replace as select * from t15 where id with no data;
select * from t17;
-- union all
create table test1(id int,name varchar(10),score numeric,date1 date,c1 bytea);
insert into test1 values(1,'aaa',97.1,'1999-12-12','0101');
insert into test1 values(5,'bbb',36.9,'1998-01-12','0110');
insert into test1 values(30,'ooo',90.1,'2023-01-30','1001');
insert into test1 values(6,'hhh',60,'2022-12-22','1010');
insert into test1 values(7,'fff',71,'2001-11-23','1011');
insert into test1 values(-1,'yaya',77.7,'2008-09-10','1100');
insert into test1 values(7,'fff',71,'2001-11-23','1011');
insert into test1 values(null,null,null,null,null);
create table test2(id int,name varchar(10),score numeric,date1 date,c1 bytea);
insert into test2 values(1,'aaa',99.1,'1998-12-12','0101');
insert into test2 values(2,'hhh',36.9,'1996-01-12','0110');
insert into test2 values(3,'ddd',89.2,'2000-03-12','0111');
insert into test2 values(7,'uuu',60.9,'1997-01-01','1000');
insert into test2 values(11,'eee',71,'2011-11-20','1011');
insert into test2 values(-1,'yaya',76.7,'2008-09-10','1100');
insert into test2 values(7,'uuu',60.9,'1997-01-01','1000');
insert into test2 values(null,null,null,null,null);
create table tb1(col1 int,id int) as select * from test1 where id<4 union all select * from test2 where score>80 order by id,score;
select * from tb1 order by id;
create table tb2(col1 int,id int unique) replace as select * from test1 where id<4 union all select * from test2 where score>80 order by id,score;
select * from tb2 order by id;
-- test update
create table tb_primary(a int primary key, b int);
create table tb_unique(a int unique, b int);
insert into tb_primary values(1,2),(2,4),(3,6);
insert into tb_unique values(1,2),(2,4),(3,6);
-- error
insert into tb_primary values(1,1);
insert into tb_unique values(1,1);
-- UPDATE nothing
insert into tb_primary values(1,1) ON DUPLICATE KEY UPDATE NOTHING;
insert into tb_unique values(1,1) ON DUPLICATE KEY UPDATE NOTHING;
select * from tb_primary;
select * from tb_unique;
-- UPDATE
insert into tb_primary values(1,1) ON DUPLICATE KEY UPDATE a = 1, b = 1;
insert into tb_unique values(1,1) ON DUPLICATE KEY UPDATE a = 1, b = 1;
select * from tb_primary;
select * from tb_unique;
\c postgres
drop database b_createas;