复古 护眼 海天 深邃 暗黑 默认

CREATE DEFAULT SHARDING STRATEGY

描述

CREATE DEFAULT SHARDING STRATEGY 语法用于创建默认的分片策略。

语法定义

CreateDefaultShardingStrategy ::=
  'CREATE' 'DEFAULT' 'SHARDING' ('DATABASE' | 'TABLE') 'STRATEGY' ifNotExists? '(' shardingStrategy ')'

ifNotExists ::=
  'IF' 'NOT' 'EXISTS'

shardingStrategy ::=
  'TYPE' '=' strategyType ',' ('SHARDING_COLUMN' '=' columnName | 'SHARDING_COLUMNS' '=' columnNames) ',' 'SHARDING_ALGORITHM' '=' algorithmDefinition

strategyType ::=
  string

algorithmDefinition ::=
  'TYPE' '(' 'NAME' '=' algorithmType ',' propertiesDefinition ')'  

columnNames ::=
  columnName (',' columnName)+

columnName ::=
  identifier

algorithmType ::=
  string

propertiesDefinition ::=
  'PROPERTIES' '(' key '=' value (',' key '=' value)* ')'

key ::=
  string

value ::=
  literal

补充说明

  • 当使用复合分片算法时,需要通过 SHARDING_COLUMNS 指定多个分片键;
  • algorithmType 为分片算法类型,详细的分片算法类型信息请参考分片算法
  • ifNotExists 子句用于避免出现 Duplicate default sharding strategy 错误。

示例

  • 创建默认分表策略
CREATE DEFAULT SHARDING TABLE STRATEGY (
    TYPE="standard", SHARDING_COLUMN=user_id, SHARDING_ALGORITHM(TYPE(NAME=inline, PROPERTIES("algorithm-expression"="t_order_${user_id % 2}")))
);
  • 使用 ifNotExists 创建默认分表策略
CREATE DEFAULT SHARDING TABLE STRATEGY IF NOT EXISTS (
    TYPE="standard", SHARDING_COLUMN=user_id, SHARDING_ALGORITHM(TYPE(NAME=inline, PROPERTIES("algorithm-expression"="t_order_${user_id % 2}")))
);

保留字

CREATEDEFAULTSHARDINGDATABASETABLESTRATEGYTYPESHARDING_COLUMNSHARDING_COLUMNSSHARDING_ALGORITHMNAMEPROPERTIES

相关链接