

# ALTER SCALEOUT TABLE

Use `ALTER SCALEOUT TABLE` to switch a scaleout table to another routing function, or add and remove member tables in a database cluster group for Aurora.

## Description and syntax
<a name="databaseclustergroups-sql-alter-scaleout-table-syntax"></a>

Changes the routing information of one scaleout table: its routing key and routing function, or its member tables. The statement moves no rows and changes no member table. Each statement takes one action. Switching a table to a new routing function is how you change a routing function, because a routing function has no `ALTER` statement.

```sql
ALTER SCALEOUT TABLE {{table_name}}
  ROUTE BY KEY({{column}} POSITION({{n}}) [, ...]) [USING ROUTING FUNCTION {{function_name}}];

ALTER SCALEOUT TABLE {{table_name}} ADD MEMBER {{member_table}};

ALTER SCALEOUT TABLE {{table_name}} DROP MEMBER {{member_table}};

ALTER SCALEOUT TABLE {{table_name}} DROP ROUTING;

ALTER SCALEOUT TABLE {{table_name}} ADD ROUTING FUNCTION {{function_name}};
```

## Parameters and options
<a name="databaseclustergroups-sql-alter-scaleout-table-parameters"></a>

The statement takes the following parameters.

{{table\_name}}  
The scaleout table to change.

ROUTE BY KEY({{column}} POSITION({{n}}) [, ...]) [USING ROUTING FUNCTION {{function\_name}}]  
The routing key, and optionally a new routing function. Without `USING`, the table keeps its current function. The new function also applies to tables created `OF` this one, so each of those tables also needs a member table in every collocation of the new function. Tables that name the old function directly keep it.

ADD MEMBER {{member\_table}}  
Adds one member table, mapped in a collocation that the table doesn't use, without changing routing. The routing layer accepts a member table whose collocation isn't in the current routing function. It receives rows after a switch to a function that targets its collocation.  
On Aurora MySQL, write {{db}}.{{member\_table}}.

DROP MEMBER {{member\_table}}  
Removes one member table whose collocation the function no longer targets. Then delete its data mapping; the routing layer refuses to delete the mapping of a member table that a scaleout table still names. The member table name takes the same form as in `ADD MEMBER`.

DROP ROUTING, ADD ROUTING FUNCTION {{function\_name}}  
`DROP ROUTING` removes the routing key and the routing function, so you can't query the table. `ADD ROUTING FUNCTION` binds a function to a table that already has routing key columns. You can't clear the routing of a parent that other tables use with `OF`.

## Where it runs and required privileges
<a name="databaseclustergroups-sql-alter-scaleout-table-privileges"></a>

Run the statement at the routing endpoint, in a session that isn't pinned, outside a transaction. On Aurora MySQL, inside a transaction the routing layer refuses the statement with error 1179. For the details, see [Where these statements run](databaseclustergroups-sql-reference.md#databaseclustergroups-sql-where-statements-run).

The router user needs `dbclustergroup_metadata_admin` or `dbclustergroup_super_admin`. For all privileges for database cluster groups for Aurora, see [Privileges](databaseclustergroups-sql-reference.md#databaseclustergroups-sql-privileges).

## Usage notes
<a name="databaseclustergroups-sql-alter-scaleout-table-usage"></a>

Keep the following in mind when you use the statement.
+ Switch the routing function only of an empty scaleout table. The switch moves no rows, and the routing layer accepts it on a table that holds rows. After the switch, you can't read a row by its routing key if the new function sends that key to another collocation. For the procedure, see [Changing a routing function](databaseclustergroups-changing-routing-function.md).
+ The routing layer can't count the rows of a scaleout table, because the count carries no routing key. To check that a table is empty, count the rows of each member table on its member cluster, through a pinned session or a direct connection.
+ The routing layer checks a switch against this table only: the table needs one member table in each collocation that the new function targets. It doesn't compare the new function with the functions of other scaleout tables in the same collocations, and it accepts a state in which they differ. Switch each table that names the old function directly. Don't query those tables between the switches, because they route the same key to different collocations until every switch is done.
+ A routing function change reaches the routing layer within a short time. A statement routed on the earlier definition returns Aurora MySQL error 1412; the session stays open, and a retry routes on the new definition. Inside a transaction, a statement that needs a definition changed after the transaction's first routed statement fails with error 63928, and the routing layer rolls back the transaction. For the retry rule, see [Transaction errors and retries](databaseclustergroups-sessions.md#databaseclustergroups-when-a-mapping-change-reaches-an-open-transaction). For more information, see [Data mappings and collocations](databaseclustergroups-data-mappings.md).
+ The routing layer doesn't check that a member table you add with `ADD MEMBER` matches the other member tables. Create it with the same columns, in the same order, with the same data types. See [Member table definitions](databaseclustergroups-scaleout-tables.md#databaseclustergroups-scaleout-tables-definition).
+ Include a single action in each statement, for example a single `ADD MEMBER`.
+ To rename a scaleout table, run `ALTER SCALEOUT TABLE {{table_name}} RENAME TO {{new_name}}` at the routing endpoint. The new name must not be in use in the group.

## Examples
<a name="databaseclustergroups-sql-alter-scaleout-table-examples"></a>

The following example adds a member table mapped in the new collocation `coll_2`, switches `orders` to the routing function `orders_mod_fn_v2`, and then removes a member table that the new function no longer targets.

```sql
ALTER SCALEOUT TABLE orders ADD MEMBER db.orders_s3;

ALTER SCALEOUT TABLE orders
  ROUTE BY KEY(customer_id POSITION(1)) USING ROUTING FUNCTION orders_mod_fn_v2;

ALTER SCALEOUT TABLE orders DROP MEMBER db.orders_s1;
```

## Errors
<a name="databaseclustergroups-sql-alter-scaleout-table-errors"></a>

The statement can return the following errors. For all errors from the routing layer, see [Error codes for database cluster groups for Aurora](databaseclustergroups-error-reference.md).


| Aurora MySQL | Meaning | 
| --- | --- | 
| [64024](databaseclustergroups-errors-scaleout.md#databaseclustergroups-data-mapping-and-scaleout-ddl-errors) Scaleout table '<table>' already has a member in collocation group '<collocation>' (42000) | ADD MEMBER of a second member table in one collocation. Map the new member table in a new collocation. | 
| [64066](databaseclustergroups-errors-scaleout.md#databaseclustergroups-data-mapping-and-scaleout-ddl-errors) Scaleout table '<table>' has no member in collocation group '<collocation>', which its routing function targets (42000) With more than one group, the message reads collocation groups '<a>', '<b>'. | A switch to a function that targets a collocation with no member table, or DROP MEMBER of a targeted collocation. | 
| [64040](databaseclustergroups-errors-scaleout.md#databaseclustergroups-data-mapping-and-scaleout-ddl-errors) Member '<member>' already belongs to a scaleout table (42000) | The member table belongs to another scaleout table. | 
| [64000](databaseclustergroups-errors-scaleout.md#databaseclustergroups-data-mapping-and-scaleout-ddl-errors) Member '<member>' has no data mapping. Create a data mapping for it. (42000) | ADD MEMBER of a member table that has no table-level data mapping, that only a database-level mapping covers, or that the statement names by its cluster name instead of its router table name. The scaleout table's member tables stay unchanged. Map the member table at table level with INSERT INTO dbclustergroup\_sys.data\_mappings, and then run the statement again. | 
| [1235](databaseclustergroups-errors-scaleout.md#databaseclustergroups-data-mapping-and-scaleout-ddl-errors) cannot clear routing on scaleout table '<name>': other scaleout tables inherit its routing function (42000) | DROP ROUTING or DROP ROUTING FUNCTION on a parent that other tables use with OF. | 
| [1064](databaseclustergroups-errors-routing.md#databaseclustergroups-routing-and-statement-errors) | Syntax error: a form outside the grammar, such as two actions in one statement. | 
| [1227](databaseclustergroups-errors-connection.md) Access denied; you need the '<PRIVILEGE>' privilege for this operation | The router user lacks the privilege for database cluster groups for Aurora. | 
| [1412](databaseclustergroups-errors-routing.md) | The routing layer routed a statement on the definition from before the change. On Aurora MySQL, the routing layer resends a text-protocol statement up to 3 times first; the session stays open. Retry the statement. | 
| [63928](databaseclustergroups-errors-routing.md) | Inside a transaction, a statement needed a definition that changed after the transaction's first routed statement, and the routing layer rolled the whole transaction back. Retry the whole transaction from its first statement. | 

## Related statements
<a name="databaseclustergroups-sql-alter-scaleout-table-related"></a>

For more information, see the following:
+ [CREATE SCALEOUT TABLE](databaseclustergroups-sql-create-scaleout-table.md)
+ [CREATE SCALEOUT ROUTING FUNCTION](databaseclustergroups-sql-create-scaleout-routing-function.md)
+ [DROP SCALEOUT ROUTING FUNCTION](databaseclustergroups-sql-drop-scaleout-routing-function.md)
+ [SHOW CREATE SCALEOUT TABLE](databaseclustergroups-sql-show-create-scaleout-table.md)
+ [INSERT INTO dbclustergroup\_sys.data\_mappings](databaseclustergroups-sql-insert-data-mappings.md)
+ [DELETE FROM dbclustergroup\_sys.data\_mappings](databaseclustergroups-sql-delete-data-mappings.md)