Project: ShardingSphere (App Definition and Development / Database)
File: create-sharding-table-rule.en.md

+++ title = "CREATE SHARDING TABLE RULE" weight = 1 +++ The `CREATE SHARDING TABLE RULE` syntax is used to add sharding table rule for the currently selected database {{< tabs >}} {{% tab name="Grammar" %}} ```sql CreateShardingTableRule ::= 'CREATE' 'SHARDING' 'TABLE' 'RULE' ifNotExists? (tableRuleDefinition | autoTableRuleDefinition) (',' (tableRuleDefinition | autoTableRuleDefinition))* ifNotExists ::= 'IF' 'NOT' 'EXISTS' tableRuleDefinition ::= ruleName '(' 'DATANODES' '(' dataNode (',' dataNode)* ')' (',' 'DATABASESTRATEGY' '(' strategyDefinition ')')? (',' 'TABLESTRATEGY' '(' strategyDefinition ')')? (',' 'KEYGENERATESTRATEGY' '(' keyGenerateStrategyDefinition ')')? (',' 'AUDIT_STRATEGY' '(' auditStrategyDefinition ')')? ')' autoTableRuleDefinition ::= ruleName '(' 'STORAGEUNITS' '(' storageUnitName (',' storageUnitName)* ')' ',' 'SHARDINGCOLUMN' '=' columnName ',' algorithmDefinition (',' 'KEYGENERATESTRATEGY' '(' keyGenerateStrategyDefinition ')')? (',' 'AUDIT_STRATEGY' '(' auditStrategyDefinition ')')? ')' strategyDefinition ::= 'TYPE' '=' strategyType ',' ('SHARDINGCOLUMN' | 'SHARDINGCOLUMNS') '=' columnName ',' algorithmDefinition keyGenerateStrategyDefinition ::= 'KEYGENERATESTRATEGY' '(' 'COLUMN' '=' columnName ',' algorithmDefinition ')' auditStrategyDefinition ::= 'AUDIT_STRATEGY' '(' algorithmDefinition (',' algorithmDefinition)* ')' algorithmDefinition ::= 'TYPE' '(' 'NAME' '=' algorithmType (',' propertiesDefinition)?')' propertiesDefinition ::= 'PROPERTIES' '(' key '=' value (',' key '=' value)* ')' key ::= string value ::= literal ruleName ::= identifier dataNode ::= string storageUnitName ::= identifier columnName ::= identifier strategyType ::= string algorithmType ::= string ``` {{% /tab %}} {{% tab name="Railroad diagram" %}} <iframe frameborder="0" name="diagram" id="diagram" width="100%" height="100%"></iframe> {{% /tab %}} {{< /tabs >}} `tableRuleDefinition` is defined for standard sharding table rule; `autoTableRuleDefinition` is defined for auto sharding table rule. For standard sharding rules and auto sharding rule, refer to ; use standard sharding table rule: `DATANODES` can only use resources that have been added to the current database, and can only use INLINE expressions to specify required resources; `DATABASESTRATEGY`, `TABLESTRATEGY` are the database sharding strategy and the table sharding strategy, which are optional, and the default strategy is used when not configured; The attribute `TYPE` in `strategyDefinition` is used to specify the type of , currently only supports `STANDARD`, `COMPLEX`. Using `COMPLEX` requires specifying multiple sharding columns with `SHARDING_COLUMNS`. use auto sharding table rule: `STORAGE_UNITS` can only use storage units that have been registered to the current database, and the required storage units can be specified by enumeration or INLINE expression; Only auto sharding algorithm can be used, please refer to . `algorithmType` is the sharding algorithm type, please refer to ; The auto-generated algorithm naming rule is `tableName` `strategyType` `shardingAlgorithmType`; The auto-generated primary key strategy naming rule is `tableName` _ `strategyType`; `KEYGENERATESTRATEGY` is used to specify the primary key generation strategy, which is optional. For the primary key generation strategy, please refer to ; `AUDIT_STRATEGY` is used to specify the sharding audit strategy, which is optional. For the sharding audit generation strategy, please refer to ; `ifNotExists` clause is used for avoid `Duplicate sharding rule` error. ```sql CREATE SHARDING TABLE RULE torderitem ( DATANODES("ds${0..1}.torderitem${0..1}"), DATABASESTRATEGY(TYPE="standard",SHARDINGCOLUMN=userid,SHARDINGALGORITHM(TYPE(NAME="inline",PROPERTIES("algorithm-expression"="ds${userid % 2}")))), TABLESTRATEGY(TYPE="standard",SHARDINGCOLUMN=orderid,SHARDINGALGORITHM(TYPE(NAME="inline",PROPERTIES("algorithm-expression"="torderitem${orderid % 2}")))), KEYGENERATESTRATEGY(COLUMN=another_id,TYPE(NAME="snowflake")), AUDITSTRATEGY (TYPE(NAME="DMLSHARDINGCONDITIONS"),ALLOWHINT_DISABLE=true) ); ``` ```sql CREATE SHARDING TABLE RULE t_order ( STORAGEUNITS(ds0,ds_1), SHARDINGCOLUMN=orderid,TYPE(NAME="hash_mod",PROPERTIES("sharding-count"="4")), KEYGENERATESTRATEGY(COLUMN=another_id,TYPE(NAME="snowflake")), AUDITSTRATEGY (TYPE(NAME="DMLSHARDINGCONDITIONS"),ALLOWHINT_DISABLE=true) ); ``` Standard sharding table rule ```sql CREATE SHARDING TABLE RULE IF NOT EXISTS torderitem ( DATANODES("ds${0..1}.torderitem${0..1}"), DATABASESTRATEGY(TYPE="standard",SHARDINGCOLUMN=userid,SHARDINGALGORITHM(TYPE(NAME="inline",PROPERTIES("algorithm-expression"="ds${userid % 2}")))), TABLESTRATEGY(TYPE="standard",SHARDINGCOLUMN=orderid,SHARDINGALGORITHM(TYPE(NAME="inline",PROPERTIES("algorithm-expression"="torderitem${orderid % 2}")))), KEYGENERATESTRATEGY(COLUMN=another_id,TYPE(NAME="snowflake")), AUDITSTRATEGY (TYPE(NAME="DMLSHARDINGCONDITIONS"),ALLOWHINT_DISABLE=true) ); ``` Auto sharding table rule ```sql CREATE SHARDING TABLE RULE IF NOT EXISTS t_order ( STORAGEUNITS(ds0,ds_1), SHARDINGCOLUMN=orderid,TYPE(NAME="hash_mod",PROPERTIES("sharding-count"="4")), KEYGENERATESTRATEGY(COLUMN=another_id,TYPE(NAME="snowflake")), AUDITSTRATEGY (TYPE(NAME="DMLSHARDINGCONDITIONS"),ALLOWHINT_DISABLE=true) ); ``` `CREATE`, `SHARDING`, `TABLE`, `RULE`, `DATANODES`, `DATABASESTRATEGY`, `TABLESTRATEGY`, `KEYGENERATESTRATEGY`, `STORAGEUNITS`, `SHARDINGCOLUMN`, `TYPE`, `SHARDINGCOLUMN`, `KEYGENERATOR`, `SHARDINGALGORITHM`, `COLUMN`, `NAME`, `PROPERTIES`, `AUDITSTRATEGY`, `AUDITORS`, `ALLOWHINTDISABLE`