CREATE DEFAULT SHARDING STRATEGY

Description

The CREATE DEFAULT SHARDING STRATEGY syntax is used to create a default sharding strategy

Syntax

Grammar Railroad diagram

  1. CreateDefaultShardingStrategy ::=
  2. 'CREATE' 'DEFAULT' 'SHARDING' ('DATABASE' | 'TABLE') 'STRATEGY' ifNotExists? '(' shardingStrategy ')'
  3. ifNotExists ::=
  4. 'IF' 'NOT' 'EXISTS'
  5. shardingStrategy ::=
  6. 'TYPE' '=' strategyType ',' ('SHARDING_COLUMN' '=' columnName | 'SHARDING_COLUMNS' '=' columnNames) ',' 'SHARDING_ALGORITHM' '=' algorithmDefinition
  7. strategyType ::=
  8. string
  9. algorithmDefinition ::=
  10. 'TYPE' '(' 'NAME' '=' algorithmType ',' propertiesDefinition ')'
  11. columnNames ::=
  12. columnName (',' columnName)+
  13. columnName ::=
  14. identifier
  15. algorithmType ::=
  16. string
  17. propertiesDefinition ::=
  18. 'PROPERTIES' '(' key '=' value (',' key '=' value)* ')'
  19. key ::=
  20. string
  21. value ::=
  22. literal

Supplement

  • When using the complex sharding algorithm, multiple sharding columns need to be specified using SHARDING_COLUMNS;
  • algorithmType is the sharding algorithm type. For detailed sharding algorithm type information, please refer to Sharding Algorithm;
  • ifNotExists clause is used for avoid Duplicate default sharding strategy error.

Example

  • create a default sharding table strategy
  1. -- create a default sharding table strategy
  2. CREATE DEFAULT SHARDING TABLE STRATEGY (
  3. TYPE="standard", SHARDING_COLUMN=user_id, SHARDING_ALGORITHM(TYPE(NAME="inline", PROPERTIES("algorithm-expression"="t_order_${user_id % 2}")))
  4. );
  • create a default sharding table strategy with ifNotExists clause
  1. CREATE DEFAULT SHARDING TABLE STRATEGY IF NOT EXISTS (
  2. TYPE="standard", SHARDING_COLUMN=user_id, SHARDING_ALGORITHM(TYPE(NAME="inline", PROPERTIES("algorithm-expression"="t_order_${user_id % 2}")))
  3. );

Reserved word

CREATE, DEFAULT, SHARDING, DATABASE, TABLE, STRATEGY, TYPE, SHARDING_COLUMN, SHARDING_COLUMNS, SHARDING_ALGORITHM, NAME, PROPERTIES