3.2.1 INSERT

3.2.1.1 Syntax

正常情况下,INSERT只支持下面2种写法。

INSERT 
    [INTO] tbl_name
    [(col_name [, col_name] ...)]
    { {VALUES | VALUE} (value_list) [, (value_list)] ... }
    [ON DUPLICATE KEY UPDATE assignment_list]

INSERT 
    [INTO] tbl_name
    SET assignment_list
    [ON DUPLICATE KEY UPDATE assignment_list]

value:
    {expr | DEFAULT}

value_list:
    value [, value] ...


assignment:
    col_name = 
          value

assignment_list:
    assignment [, assignment] ...

特殊情况下,支持下面这种写法。特殊场景参看下文细节

INSERT 
    [INTO] tbl_name
    [(col_name [, col_name] ...)]
    { SELECT ... 
      | TABLE table_name 
    }
    [ON DUPLICATE KEY UPDATE assignment_list]

assignment:
    col_name = 
          value

assignment_list:
    assignment [, assignment] ...

3.2.1.2 与MySQL语法区别

下文中红色表示不支持的语法或关键字

INSERT 
-   [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
    [INTO] tbl_name
-   [PARTITION (partition_name [, partition_name] ...)]
    [(col_name [, col_name] ...)]
    { {VALUES | VALUE} (value_list) [, (value_list)] ... }
-   [AS row_alias[(col_alias [, col_alias] ...)]]
    [ON DUPLICATE KEY UPDATE assignment_list]

INSERT 
-   [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
    [INTO] tbl_name
-   [PARTITION (partition_name [, partition_name] ...)]
    SET assignment_list
-   [AS row_alias[(col_alias [, col_alias] ...)]]
    [ON DUPLICATE KEY UPDATE assignment_list]

INSERT 
-   [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE]
    [INTO] tbl_name
-   [PARTITION (partition_name [, partition_name] ...)]
    [(col_name [, col_name] ...)]
    { SELECT ... 
      | TABLE table_name 
-     | VALUES row_constructor_list
    }
    [ON DUPLICATE KEY UPDATE assignment_list]

value:
    {expr | DEFAULT}

value_list:
    value [, value] ...

-row_constructor_list:
-   ROW(value_list)[, ROW(value_list)][, ...]

assignment:
    col_name = 
          value
-       | [row_alias.]col_name
-       | [tbl_name.]col_name
-       | [row_alias.]col_alias

assignment_list:
    assignment [, assignment] ...

3.2.1.3 举例

insert into test (col1,col3) values(1,'cust1'),(2,'cust2');
insert into test (col1,col3) values(default,'cust3');
insert into test set col1=4,col3='cust4';
insert into test set col1=default,col3='cust5';
insert into test (col1,col3) values(default,cast(now() as char));

3.2.1.4 限制

  • 在插入ER关系的子表时,每个语句只允许插入一个ROW

  • 全局序列在插入时不允许指定值,全部由dble序列生成

  • 对于含有枚举类型的分片表,比如表结构:CREATE TABLE  `test` (`id` enum('1','2','3') DEFAULT '1'),在插入id列时,既可以使用枚举值插入,也可以使用枚举值的下标,'1'的下标是1,以此类推。若用户以枚举值进行分片,但是插入时确使用枚举值下标,因为dble不会将下标转换为枚举值,所以分片会出现问题,详细可参考issue : https://github.com/actiontech/dble/issues/816。

  • 存在特例,当insert/replace... select 语句满足以下条件时,dble会在确保数据安全性的情况下对于SQL进行下发执行

    • 当插入目标是单节点表时,要求所有数据来源的表格都有明确的路由信息路由到同一节点
    • 当插入目标是全局表时,要求所有的数据来源表格都是全局表,并且路由范围能够覆盖插入目标
    • 当插入目标是分片表时,要求分片列的数据直接来源自拥有同样分片逻辑的分片表,并且对于select子查询中间的其他表格,要求能够子查询部分路由结果能整体下发,并且逻辑上无错误

results matching ""

    No results matching ""