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

+++ title = "ALTER SHARDING TABLE RULE" weight = 2 +++ The `ALTER SHARDING TABLE RULE` syntax is used to alter sharding table rule for the currently selected database {{< tabs >}} {{% tab name="Grammar" %}} ```sql AlterShardingTableRule ::= 'ALTER' 'SHARDING' 'TABLE' 'RULE' (tableRuleDefinition | autoTableRuleDefinition) (',' (tableRuleDefinition | autoTableRuleDefinition))* 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 . ```SQL ALTER SHARDING TABLE RULE torderitem ( DATANODES("ds${0..3}.torder_item${0..3}"), DATABASESTRATEGY(TYPE="standard",SHARDINGCOLUMN=userid,SHARDINGALGORITHM(TYPE(NAME="inline",PROPERTIES("algorithm-expression"="ds${userid % 4}")))), TABLESTRATEGY(TYPE="standard",SHARDINGCOLUMN=orderid,SHARDINGALGORITHM(TYPE(NAME="inline",PROPERTIES("algorithm-expression"="torderitem${orderid % 4}")))), KEYGENERATESTRATEGY(COLUMN=another_id,TYPE(NAME="snowflake")), AUDITSTRATEGY(TYPE(NAME="dmlshardingconditions"),ALLOWHINT_DISABLE=true) ); ``` ```sql ALTER SHARDING TABLE RULE t_order ( STORAGEUNITS(ds0,ds1,ds2,ds_3), SHARDINGCOLUMN=orderid,TYPE(NAME="hash_mod",PROPERTIES("sharding-count"="16")), KEYGENERATESTRATEGY(COLUMN=another_id,TYPE(NAME="snowflake")), AUDITSTRATEGY(TYPE(NAME="dmlshardingconditions"),ALLOWHINT_DISABLE=true) ); ``` `ALTER`, `SHARDING`, `TABLE`, `RULE`, `DATANODES`, `DATABASESTRATEGY`, `TABLESTRATEGY`, `KEYGENERATESTRATEGY`, `STORAGEUNITS`, `SHARDINGCOLUMN`, `TYPE`, `SHARDINGCOLUMN`, `KEYGENERATOR`, `SHARDINGALGORITHM`, `COLUMN`, `NAME`, `PROPERTIES`, `AUDITSTRATEGY`, `AUDITORS`, `ALLOWHINTDISABLE`