DDL操作支持
Data Definition Language,数据库定义语言,用于定义和管理 SQL 数据库中的所有对象的语言。GBase 8a Cluster MPP的DDL语言包括对DATABASE、TABLE、VIEW、INDEX等对象的CREATE、ALTER、DROP和TRUNCATE操作。
对象 |
对象含义 |
支持的DDL操作 |
|
DATABSE |
数据库 |
CREATE、DROP |
|
TABLE |
表 |
CREATE、ALTER、DROP、TRUNCATE |
|
VIEW |
视图 |
CREATE、ALTER、DROP |
|
INDEX |
索引 |
CREATE、ALTER、DROP |
|
DDL操作 |
DDL操作功能 |
||
CREATE |
创建对象 |
||
ALTER |
修改对象属性 |
||
DROP |
删除对象 |
||
TRUNCATE |
删除对象内记录 |
DML操作支持
GBase 8a MPP Cluster支持标准的DML语句。
INSERT
功能说明
INSERT将新行插入到一个已存在的表中。INSERT...VALUES形式的语句基于明确的值插入记录行。INSERT ... SELECT形式的语句从另一个或多个表中选取出值,并将其插入。
语法格式
INSERT [INTO] [vc_name.][database_name.]table_name [(col_name,...)] VALUES ({expr | DEFAULT},...),(...),... |
或
INSERT [INTO] [vc_name.][database_name.]table_name [(col_name,...)] SELECT ... FROM [vc_name.][database_name.]table_name ... |
表 724 参数说明
参数名称 |
说 明 |
vc_name |
虚拟集群名,可选项。 |
database_name |
数据库名,可选项。 |
table_name |
表名,是要被插入数据的表。 |
col_name |
指出语句指定的值赋给哪个列。如果在INSERT...VALUES或INSERT...SELECT中没有指定column列,那么所有列的值必须在VALUES()列表中或由SELECT提供。如果用户不知道表的列的次序,可以使用DESC table_name来查看。 |
expr |
指定一个表达式expr来提供列值。例如插入一个INT型的数据表达式可以写为INSERT INTO t(a) VALUES(3+5)。 |
DEFAULT |
使用关键词DEFAULT,明确地把列设置为默认值。使用DEFAULT可以避免编写出不完整的、未包含全部列值的VALUES清单。可以使用DEFAULT (col_name)作为设置列默认值的一个更通用的形式。如果不使用DEFAULT,用户必须注明每一个列的名称,与VALUES中的每个值对应。 |
说明 l 如果column列表和VALUES列表都为空,INSERT将创建一个行,它的每一列都设置为它的默认值。 |
示例
Ø 示例1:INSERT INTO... 。
gbase> CREATE TABLE t0(id int) REPLICATED; Query OK, 0 rows affected gbase> INSERT INTO t0 VALUES(1),(2),(3),(4),(5),(6),(2),(3),(1); Query OK, 9 rows affected Records: 9 Duplicates: 0 Warnings: 0 gbase> SELECT * FROM t0; +------+ | id | +------+ | 1 | | 2 | | 3 | | 4 | | 5 | | 6 | | 2 | | 3 | | 1 | +------+ 9 rows in set |
Ø 示例2:INSERT INTO...SELECT...。
gbase> CREATE TABLE t0(id int) REPLICATED; Query OK, 0 rows affected gbase> INSERT INTO t0(id) SELECT DISTINCT lo_custkey FROM ssbm.lineorder LIMIT 15; Query OK, 15 rows affected Records: 15 Duplicates: 0 Warnings: 0 gbase> SELECT * FROM t0; +-------+ | id | +-------+ | 25738 | | 18238 | | 20612 | | 5393 | | 5954 | | 11728 | | 17302 | | 14578 | | 2210 | | 27362 | | 1642 | | 29255 | | 10745 | | 7180 | | 16276 | +-------+ 15 rows in set |
Ø 示例3:INSERT INTO ... VALUES(DEFAULT)。
gbase> CREATE TABLE t0(id int DEFAULT 1) REPLICATED; Query OK, 0 rows affected gbase> INSERT INTO t0 (id) VALUES(DEFAULT); Query OK, 1 row affected -- 也可以这样指定默认值进行插入 gbase> INSERT INTO t0 (id) VALUES(DEFAULT(id)); Query OK, 1 row affected gbase> SELECT * FROM t0; +------+ | id | +------+ | 1 | | 1 | +------+ 2 rows in set |
Ø 示例4:INSERT时,自动更新TIMESTAMP列。使用INSERT INTO t1 VALUES(),()语法时,此时INSERT完成后数据行对应的TIMESTAMP列的值自动更新为一个时间戳。b=6和b=7对应的TIMESTAMP列自动更新为一个时间戳
DROP TABLE IF EXISTS t1; CREATE TABLE t1(a timestamp , b int) DISTRIBUTED BY ('b');
gbase> INSERT INTO t1(b) VALUES(1); Query OK, 1 row affected
gbase> INSERT INTO t1(b) VALUES (2); Query OK, 1 row affected
gbase> INSERT INTO t1(b) VALUES (3); Query OK, 1 row affected
gbase> INSERT INTO t1(b) VALUES (4); Query OK, 1 row affected
gbase> INSERT INTO t1(b) VALUES (5); Query OK, 1 row affected
gbase> SELECT * FROM t1 ORDER BY a; +---------------------+------+ | a | b | +---------------------+------+ | 2013-12-17 14:04:43 | 1 | | 2013-12-17 14:04:47 | 2 | | 2013-12-17 14:04:52 | 3 | | 2013-12-17 14:04:59 | 4 | | 2013-12-17 14:05:04 | 5 | +---------------------+------+ 5 rows in set gbase> INSERT INTO t1(b) VALUES (6),(7); Query OK, 1 row affected gbase> SELECT * FROM t1 ORDER BY a; +---------------------+------+ | a | b | +---------------------+------+ | 2013-12-17 15:44:29 | 1 | | 2013-12-17 15:44:30 | 2 | | 2013-12-17 15:44:31 | 3 | | 2013-12-17 15:44:32 | 4 | | 2013-12-17 15:44:33 | 5 | | 2013-12-17 15:44:48 | 7 | | 2013-12-17 15:44:48 | 6 | +---------------------+------+ 7 rows in set |
UPDATE
功能说明
当更新列的值是一个合法的表达式时,也可以进行正确的更新赋值操作。
注意 l UPDATE操作不支持更新DISTRIBUTED BY列的值、自增列的值、分区表中分区字段的值。 |
语法格式
UPDATE [vc_name.][database_name.]table_name SET col_name1=expr1 [, col_name2=expr2 ...] [WHERE where_definition] |
表 725 参数说明
参数名称 |
说 明 |
vc_name |
虚拟集群名,可选项。 |
database_name |
数据库名,可选项。 |
table_name |
表名,是要被插入数据的表。 |
col_name |
指出更新哪个列。 |
expr |
指定一个表达式expr来提供列值。 |
示例
Ø 首先创建表t0和t2,并插入数据。
gbase> CREATE TABLE t0(id int) REPLICATED; Query OK, 0 rows affected gbase> CREATE TABLE t2(id int); Query OK, 0 rows affected gbase> INSERT INTO t0(id) VALUES(1),(2),(3),(4),(5),(6),(2),(3),(1); Query OK, 9 rows affected Records: 9 Duplicates: 0 Warnings: 0 gbase> INSERT INTO t2(id) VALUES(1),(2),(4); Query OK, 3 rows affected Records: 3 Duplicates: 0 Warnings: 0 |
Ø 示例1:更新t0表的数据。
gbase> SELECT * FROM t0; +------+ | id | +------+ | 1 | | 2 | | 3 | | 4 | | 5 | | 6 | | 2 | | 3 | | 1 | +------+ 9 rows in set gbase> UPDATE t0 SET t0.id = t0.id+1 WHERE t0.id > 1; Query OK, 7 rows affected Rows matched: 7 Changed: 7 Warnings: 0 gbase> SELECT * FROM t0; +------+ | id | +------+ | 1 | | 3 | | 4 | | 5 | | 6 | | 7 | | 3 | | 4 | | 1 | +------+ 9 rows in set |
Ø 示例2:使用IN的多表查询更新。
gbase> SELECT * FROM t0; +------+ | id | +------+ | 2 | | 3 | | 5 | | 5 | | 6 | | 7 | | 3 | | 5 | | 2 | +------+ 9 rows in set gbase> UPDATE t0 SET t0.id = 10 WHERE t0.id IN (SELECT t2.id FROM t2); Query OK, 2 rows affected Rows matched: 2 Changed: 2 Warnings: 0 gbase> SELECT * FROM t0; +------+ | id | +------+ | 10 | | 3 | | 5 | | 5 | | 6 | | 7 | | 3 | | 5 | | 10 | +------+ 9 rows in set |
Ø 示例3:子查询中包含更新列,更新成功。
gbase> CREATE TABLE t0(id int) REPLICATED; Query OK, 0 rows affected gbase> INSERT INTO t0(id) VALUES(1),(2),(3),(4),(5),(6),(2),(3),(1); Query OK, 9 rows affected Records: 9 Duplicates: 0 Warnings: 0 gbase> SELECT * FROM t0; +------+ | id | +------+ | 1 | | 2 | | 3 | | 4 | | 5 | | 6 | | 2 | | 3 | | 1 | +------+ 9 rows in set gbase> UPDATE t0 SET t0.id = (SELECT id FROM t0 WHERE id = 6); Query OK, 9 rows affected Rows matched: 9 Changed: 9 Warnings: 0 |
Ø 示例4:不允许更新DISTRIBUTED BY列的值。
gbase> CREATE TABLE student (stu_no int, stu_name varchar(200),stu_sex int) DISTRIBUTED BY('stu_no'); Query OK, 0 rows affected gbase> INSERT INTO student (stu_no,stu_name,stu_sex) VALUES (1,'Tom',0), (2,'Jim',0),(3,'Rose',1); Query OK, 3 rows affected Records: 3 Duplicates: 0 Warnings: 0 gbase> SELECT * FROM student; +--------+----------+---------+ | stu_no | stu_name | stu_sex | +--------+----------+---------+ | 1 | Tom | 0 | | 2 | Jim | 0 | | 3 | Rose | 1 | +--------+----------+---------+ 3 rows in set gbase> UPDATE student SET stu_no = 4 WHERE stu_no = 2; ERROR 1722 (HY000): (GBA-02DD-0006) Can't update distributed column 'stu_no' |
Ø 示例5:UPDATE时,更新数据行中的TIMESTAMP列的数值自动更新。
DROP TABLE IF EXISTS t2; CREATE TABLE t2(a timestamp,b decimal(30,4),c int) DISTRIBUTED BY ('c'); INSERT INTO t2(b,c) VALUES(534.536,1); INSERT INTO t2(b,c) VALUES(3534.56,11); INSERT INTO t2(b,c) VALUES(33534.576,111); INSERT INTO t2(b,c) VALUES(1334.56,1111); INSERT INTO t2(b,c) VALUES(334.565,11111); INSERT INTO t2(b,c) VALUES(34.5,111111); INSERT INTO t2(b,c) VALUES(35.56,10009); INSERT INTO t2(b,c) VALUES(3222.56,1897); INSERT INTO t2(b,c) VALUES(3255.56,123); INSERT INTO t2(b,c) VALUES(325.56,2); gbase> SELECT * FROM t2 ORDER BY a; +---------------------+------------+--------+ | a | b | c | +---------------------+------------+--------+ | 2013-12-17 14:11:16 | 3534.5600 | 11 | | 2013-12-17 14:11:16 | 33534.5760 | 111 | | 2013-12-17 14:11:16 | 1334.5600 | 1111 | | 2013-12-17 14:11:16 | 334.5650 | 11111 | | 2013-12-17 14:11:16 | 3222.5600 | 1897 | | 2013-12-17 14:11:16 | 3255.5600 | 123 | | 2013-12-17 14:11:16 | 534.5360 | 1 | | 2013-12-17 14:11:16 | 34.5000 | 111111 | | 2013-12-17 14:11:16 | 35.5600 | 10009 | | 2013-12-17 14:11:16 | 325.5600 | 2 | +---------------------+------------+--------+ 10 rows in set gbase> UPDATE t2 SET b=89.3 where c <1000; Query OK, 5 rows affected Rows matched: 5 Changed: 5 Warnings: 0 Ø -- 查看没有被更新的数据行对应的TIMESTAMP列,TIMESTAMP列的值保持不变。 gbase> SELECT * FROM t2 WHERE c <1000; +---------------------+---------+------+ | a | b | c | +---------------------+---------+------+ | 2013-12-17 14:17:57 | 89.3000 | 11 | | 2013-12-17 14:17:57 | 89.3000 | 111 | | 2013-12-17 14:17:57 | 89.3000 | 123 | | 2013-12-17 14:17:57 | 89.3000 | 1 | | 2013-12-17 14:17:57 | 89.3000 | 2 | +---------------------+---------+------+ 5 rows in set Ø -- 查看没有被更新的数据行对应的TIMESTAMP列,TIMESTAMP列的值保持不变。 gbase> SELECT * FROM t2 WHERE c >=1000; +---------------------+-----------+--------+ | a | b | c | +---------------------+-----------+--------+ | 2013-12-17 14:11:16 | 1334.5600 | 1111 | | 2013-12-17 14:11:16 | 334.5650 | 11111 | | 2013-12-17 14:11:16 | 3222.5600 | 1897 | | 2013-12-17 14:11:16 | 34.5000 | 111111 | | 2013-12-17 14:11:16 | 35.5600 | 10009 | +---------------------+-----------+--------+ 5 rows in set |
快速UPDATE模式
功能说明
快速UPDATE模式,即先删除符合更新条件的数据,然后再向表的末尾插入需要更新的新数据。
相对于传统的行存储数据库来说,列存储的数据中UPDATE更新少量行时,操作效率相对来说是耗时的,因此,GBase 8a MPP Cluster针对此特点,专门设计了快速UPDATE模式,用以提高数据更新操作。
快速UPDATE模式目前只支持针对表对象的操作。
要使用快速UPDATE模式,必须在客户端使用SET gbase_fast_update =1;的命令打开快速UPDATE模式。更新批量数据的时候建议使用默认UPDATE模式,更新少量数据的时候建议使用快速UPDATE模式。
SET gbase_fast_update =0;表示关闭快速UPDATE模式。
SET gbase_fast_update =1;表示开启快速UPDATE模式。
示例
Ø 示例1:开启快速UPDATE模式。
gbase> CREATE TABLE t1 (f_1 int); Query OK, 0 rows affected gbase> INSERT INTO t1 values(1),(2),(3); Query OK, 3 rows affected Records: 3 Duplicates: 0 Warnings: 0 gbase> SELECT * FROM t1; +------+ | f_1 | +------+ | 1 | | 2 | | 3 | +------+ 3 rows in set gbase> SET gbase_fast_update = 1; Query OK, 0 rows affected gbase> UPDATE t1 SET f_1 = 10 WHERE f_1= 1; Query OK, 1 row affected Rows matched: 1 Changed: 1 Warnings: 0 gbase> SELECT * FROM t1; +------+ | f_1 | +------+ | 2 | | 3 | | 10 | +------+ 3 rows in set gbase> SET gbase_fast_update = 0; Query OK, 0 rows affected |
DELETE
语法格式
DELETE [FROM] [vc_name.][database_name.]table_name [tbl_alias] [WHERE where_definition] |
表 726 参数说明
参数名称 |
说 明 |
vc_name |
虚拟集群名,可选项。 |
database_name |
数据库名,可选项。 |
table_name |
表名,是要被插入数据的表。 |
tbl_alias |
别名 |
说明 l l当DELETE语句中包含别名时,可以省略FROM关键字。 |
Ø
示例
Ø 示例中用到的表及数据:
CREATE TABLE t0 (id int); INSERT INTO t0 values(1),(2),(3),(4),(5),(6),(7),(8); |
Ø 示例1:删除表中id大于6的数据。
gbase> DELETE FROM t0 WHERE t0.id > 6; Query OK, 2 rows affected |
Ø 示例2:使用IN,删除id值为1,2,3的数据。
gbase> DELETE FROM t0 WHERE t0.id IN ( 1,2,3); Query OK, 3 rows affected |
Ø 示例3:删除全表数据。
gbase> DELETE FROM t0; Query OK, 3 rows affected |
Ø 示例4:DELETE FROM...WHERE...IN (SELECT...FROM)。
gbase> INSERT INTO t0 values(1),(2),(3),(4),(5),(6),(7),(8); Query OK, 8 rows affected Records: 8 Duplicates: 0 Warnings: 0 gbase> DELETE FROM t0 WHERE t0.ID IN (SELECT id FROM t0); Query OK, 8 rows affected |
Ø 示例5:DELETE语法中包含表的别名,可以省略FROM关键字。
gbase> INSERT INTO t0 values(1),(2),(3),(4),(5),(6),(7),(8); Query OK, 8 rows affected Records: 8 Duplicates: 0 Warnings: 0 gbase> DELETE FROM t0 tt WHERE tt.id=8; Query OK, 1 row affected gbase> DELETE t0 tt WHERE tt.id=1; Query OK, 1 row affected gbase> SELECT * FROM t0; +------+ | id | +------+ | 2 | | 3 | | 4 | | 5 | | 6 | | 7 | +------+ 6 rows in set |
Ø 示例6:DELETE ...WHERE...
gbase> DELETE t0 WHERE id = 2; Query OK, 1 row affected |
MERGE
语法格式
MERGE [INTO] [vc_name.][database_name.]table_name USING table_reference ON conditional_exp [WHEN MATCHED THEN UPDATE SET col_name1=expr1 [, col_name2=expr2] ... [WHEN NOT MATCHED THEN INSERT [(col_name3,...)] VALUES (expr3,...)] |
使用MERGE语法示例性语句如下:
MERGE INTO a USING table_x b ON (a.hash_col = b.hash_col) WHEN MATCHED THEN UPDATE SET a.column = b.column,… WHEN NOT MATCHED THEN INSERT (a.column,…) VALUES (b.column,…) |
表 727 参数说明
参数名称 |
说 明 |
vc_name |
vc名,可选项。 |
database_name |
数据库名,可选项。 |
table_name |
表名,必须是表,不可以是视图,可以使用别名。 |
table_reference |
table_reference只允许是表,可以使用别名。 |
conditional_exp |
关联条件 |
column |
列名 |
注意 l UPDATE部分不支持DELETE子句。 l UPDATE部分和INSERT部分不支持WHERE子句。 l UPDATE部分和INSERT部分可以省略,但不可以同时省略,否则报语法错误。 l UPDATE部分和INSERT部分位置不可以颠倒。 l INSERT的VALUES部分不允许使用MERGE表。 l UPDATE或INSERT中的列如果出现多次,不会报错,后指定的列生效,但建议不要依赖于该行为,避免这样使用。 l 不允许一对多更新:如果MERGE表中的一行与USING表中的多行符合连接条件,则报错。 l MERGE INTO a语句中,a表必须是哈希分布表。 l USING table_x b ON (a.hash_col = b.hash_col)这部分中包含JOIN条件语句,JOIN条件中必须存在a表哈希分布列的等值关联条件,且该关联条件下,哈希分布列必须是物理列的关联,不能是表达式、函数。 l 例如: l a.hash_col = b.hash_col(合法) l ABS(a.hash_col) = b.hash_col(不合法) l a.hash_col = ABS(b.hash_col)(不合法),这个不合法是因为当前集群哈希重分布的限制,JOIN时均是以物理列进行哈希重分布。 l WHEN MATCHED THEN UPDATE SET a.column = b.column, …语句中, set列(a.column)不能是hash分布列。 l WHEN NOT MATCHED THEN INSERT (a.column,…) VALUES (b.column,…)语句中,INSERT的字段列表中,a.column必须出现哈希分布列,且VALUES中对应的列中,b.column必须是哈希分布列,或者是经过动态哈希重分布后的哈希分布列。 l 如果a表和b表之间JOIN关系不是静态哈希分布JOIN关系,则gcluster_hash_redistribute_join_optimize参数不能被关闭。 l 参与MERGE操作的表,如果有表的主分片处于locked状态,则无法使用哈希重分布,因此当上例中的a与b不是静态哈希JOIN关系时,会导致MERGE无法执行。 |
示例
Ø 示例1:对t1表进行MERGE操作。
DROP TABLE IF EXISTS t1; CREATE TABLE t1(i int,vc varchar(20),d date,dc decimal(20,3)) DISTRIBUTED BY ('i'); INSERT INTO t1 VALUES(1,'one','2013-02-03',11.21); INSERT INTO t1 VALUES (2,'two','2013-04-03',12.21); INSERT INTO t1 VALUES (3,'one2','2013-03-03',31.21); INSERT INTO t1 VALUES (11,'one3','2013-08-03',41.21); INSERT INTO t1 VALUES (14,'three','2013-07-22',161.218); INSERT INTO t1 VALUES (33,'third','2013-09-04',11.216); INSERT INTO t1 VALUES (5,'wto','2013-02-03',110.210); INSERT INTO t1 VALUES (null,'first','2013-02-03',311.91); INSERT INTO t1 VALUES (8,'five','2013-02-03',811.201);
DROP TABLE IF EXISTS t2; CREATE TABLE t2(i int,vc varchar(20),d date,dc decimal(30,3)) DISTRIBUTED BY ('i'); INSERT INTO t2 VALUES (1,'one','2013-02-03',11.20); INSERT INTO t2 VALUES (2,'two','2013-08-03',12.81); INSERT INTO t2 VALUES (13,'one2','2013-09-03',31.01); INSERT INTO t2 VALUES (110,'one3','2013-08-03',41.21); INSERT INTO t2 VALUES (14,'three','2013-06-22',161.218); INSERT INTO t2 VALUES (30,'third','2013-09-04',11.216);
gbase> SELECT * FROM t1 ORDER BY i; +------+-------+------------+---------+ | i | vc | d | dc | +------+-------+------------+---------+ | 1 | one | 2013-02-03 | 11.210 | | 2 | two | 2013-04-03 | 12.210 | | 3 | one2 | 2013-03-03 | 31.210 | | 5 | wto | 2013-02-03 | 110.210 | | 8 | five | 2013-02-03 | 811.201 | | 11 | one3 | 2013-08-03 | 41.210 | | 14 | three | 2013-07-22 | 161.218 | | 33 | third | 2013-09-04 | 11.216 | | NULL | first | 2013-02-03 | 311.910 | +------+-------+------------+---------+ 9 rows in set gbase> SELECT * FROM t2 ORDER BY i; +------+-------+------------+---------+ | i | vc | d | dc | +------+-------+------------+---------+ | 1 | one | 2013-02-03 | 11.200 | | 2 | two | 2013-08-03 | 12.810 | | 13 | one2 | 2013-09-03 | 31.010 | | 14 | three | 2013-06-22 | 161.218 | | 30 | third | 2013-09-04 | 11.216 | | 110 | one3 | 2013-08-03 | 41.210 | +------+-------+------------+---------+ 6 rows in set gbase> MERGE INTO t1 USING t2 ON t1.i=t2.i WHEN MATCHED THEN UPDATE SET t1.vc=t2.vc WHEN NOT MATCHED THEN INSERT(t1.i,t1.vc) VALUES(t2.i,t2.vc); Query OK, 6 rows affected Rows matched: 6 Changed: 6 Warnings: 0 gbase> SELECT * FROM t1 ORDER BY i; +------+-------+------------+---------+ | i | vc | d | dc | +------+-------+------------+---------+ | 1 | one | 2013-02-03 | 11.210 | | 2 | two | 2013-04-03 | 12.210 | | 3 | one2 | 2013-03-03 | 31.210 | | 5 | wto | 2013-02-03 | 110.210 | | 8 | five | 2013-02-03 | 811.201 | | 11 | one3 | 2013-08-03 | 41.210 | | 13 | one2 | NULL | NULL | | 14 | three | 2013-07-22 | 161.218 | | 30 | third | NULL | NULL | | 33 | third | 2013-09-04 | 11.216 | | 110 | one3 | NULL | NULL | | NULL | first | 2013-02-03 | 311.910 | +------+-------+------------+---------+ 12 rows in set |
Ø 示例2:t1表使用别名t,然后进行MERGE操作。
DROP TABLE IF EXISTS t1; CREATE TABLE t1(i int,vc varchar(20),d date,dc decimal(20,3)) DISTRIBUTED BY ('i'); INSERT INTO t1 VALUES(1,'one','2013-02-03',11.21); INSERT INTO t1 VALUES (2,'two','2013-04-03',12.21); INSERT INTO t1 VALUES (3,'one2','2013-03-03',31.21); INSERT INTO t1 VALUES (11,'one3','2013-08-03',41.21); INSERT INTO t1 VALUES (14,'three','2013-07-22',161.218); INSERT INTO t1 VALUES (33,'third','2013-09-04',11.216); INSERT INTO t1 VALUES (5,'wto','2013-02-03',110.210); INSERT INTO t1 VALUES (null,'first','2013-02-03',311.91); INSERT INTO t1 VALUES (8,'five','2013-02-03',811.201);
DROP TABLE IF EXISTS t2; CREATE TABLE t2(i int,vc varchar(20),d date,dc decimal(30,3)) DISTRIBUTED BY ('i'); INSERT INTO t2 VALUES (1,'one','2013-02-03',11.20); INSERT INTO t2 VALUES (2,'two','2013-08-03',12.81); INSERT INTO t2 VALUES (13,'one2','2013-09-03',31.01); INSERT INTO t2 VALUES (110,'one3','2013-08-03',41.21); INSERT INTO t2 VALUES (14,'three','2013-06-22',161.218); INSERT INTO t2 VALUES (30,'third','2013-09-04',11.216); gbase> SELECT * FROM t1 ORDER BY i; +------+-------+------------+---------+ | i | vc | d | dc | +------+-------+------------+---------+ | 1 | one | 2013-02-03 | 11.210 | | 2 | two | 2013-04-03 | 12.210 | | 3 | one2 | 2013-03-03 | 31.210 | | 5 | wto | 2013-02-03 | 110.210 | | 8 | five | 2013-02-03 | 811.201 | | 11 | one3 | 2013-08-03 | 41.210 | | 14 | three | 2013-07-22 | 161.218 | | 33 | third | 2013-09-04 | 11.216 | | NULL | first | 2013-02-03 | 311.910 | +------+-------+------------+---------+ 9 rows in set gbase> SELECT * FROM t2 ORDER BY i; +------+-------+------------+---------+ | i | vc | d | dc | +------+-------+------------+---------+ | 1 | one | 2013-02-03 | 11.200 | | 2 | two | 2013-08-03 | 12.810 | | 13 | one2 | 2013-09-03 | 31.010 | | 14 | three | 2013-06-22 | 161.218 | | 30 | third | 2013-09-04 | 11.216 | | 110 | one3 | 2013-08-03 | 41.210 | +------+-------+------------+---------+ 6 rows in set gbase> MERGE INTO t1 t USING t2 ON t.i=t2.i WHEN MATCHED THEN UPDATE SET t.vc=t2.vc WHEN NOT MATCHED THEN INSERT (t.i,t.vc) VALUES (t2.i,t2.vc); Query OK, 6 rows affected Rows matched: 6 Changed: 6 Warnings: 0 gbase> SELECT * FROM t1 ORDER BY i; +------+-------+------------+---------+ | i | vc | d | dc | +------+-------+------------+---------+ | 1 | one | 2013-02-03 | 11.210 | | 2 | two | 2013-04-03 | 12.210 | | 3 | one2 | 2013-03-03 | 31.210 | | 5 | wto | 2013-02-03 | 110.210 | | 8 | five | 2013-02-03 | 811.201 | | 11 | one3 | 2013-08-03 | 41.210 | | 13 | one2 | NULL | NULL | | 14 | three | 2013-07-22 | 161.218 | | 30 | third | NULL | NULL | | 33 | third | 2013-09-04 | 11.216 | | 110 | one3 | NULL | NULL | | NULL | first | 2013-02-03 | 311.910 | +------+-------+------------+---------+ 12 rows in set |
Ø 示例3:MERGE操作后,同步更新TIMESTAMP列的值。
DROP TABLE IF EXISTS t1; CREATE TABLE t1(a timestamp ,b int) DISTRIBUTED BY('b'); INSERT INTO t1(b) VALUES(1); INSERT INTO t1(b) VALUES(2); INSERT INTO t1(b) VALUES(6); INSERT INTO t1(b) VALUES(8); INSERT INTO t1(b) VALUES(107); INSERT INTO t1(b) VALUES(105);
gbase> SELECT * FROM t1 ORDER BY a; +---------------------+------+ | a | b | +---------------------+------+ | 2013-12-17 14:40:03 | 6 | | 2013-12-17 14:40:03 | 8 | | 2013-12-17 14:40:03 | 105 | | 2013-12-17 14:40:03 | 1 | | 2013-12-17 14:40:03 | 2 | | 2013-12-17 14:40:03 | 107 | +---------------------+------+ 6 rows in set gbase> SELECT * FROM tt ORDER BY a; +---------------------+------+------+ | a | b | c | +---------------------+------+------+ | 2013-12-17 14:40:44 | 105 | a | +---------------------+------+------+ 1 rows in set gbase> MERGE INTO tt USING t1 ON t1.b=tt.b WHEN MATCHED THEN UPDATE SET tt.c='b' WHEN NOT MATCHED THEN INSERT (tt.b) VALUES(t1.b); Query OK, 6 rows affected Rows matched: 6 Changed: 6 Warnings: 0 --查看t1表中的数据,TIMESTAMP列没有同步更新。 gbase> SELECT * FROM t1 ORDER BY a; +---------------------+------+ | a | b | +---------------------+------+ | 2013-12-17 14:40:03 | 6 | | 2013-12-17 14:40:03 | 8 | | 2013-12-17 14:40:03 | 105 | | 2013-12-17 14:40:03 | 1 | | 2013-12-17 14:40:03 | 2 | | 2013-12-17 14:40:03 | 107 | +---------------------+------+ 6 rows in set -- 查看tt表中的数据,TIMESTAMP列同步更新。 gbase> SELECT * FROM tt ORDER BY a; +---------------------+------+------+ | a | b | c | +---------------------+------+------+ | 2013-12-17 14:40:20 | 8 | NULL | | 2013-12-17 14:40:20 | 1 | NULL | | 2013-12-17 14:40:20 | 105 | b | | 2013-12-17 14:40:20 | 6 | NULL | | 2013-12-17 14:40:20 | 2 | NULL | | 2013-12-17 14:40:20 | 107 | NULL | +---------------------+------+------+ 6 rows in set |