

# Scaleout SQL reference

A database cluster group for Aurora adds SQL statements that belong to the group rather than to the database engine itself. You run them at the routing endpoint to manage routing functions, scaleout tables, data mappings, data movement, and sessions. Member tables and the other objects on member clusters are standard objects of the engine that you create on each member cluster. For the engine statements that work through the routing endpoint, see [Statement compatibility, drivers, ORMs, and tools](databaseclustergroups-compatibility.md).

The errors table of each statement lists the Aurora MySQL error number.

**Topics**
+ [SQL commands](#databaseclustergroups-sql-commands)
+ [Where these statements run](#databaseclustergroups-sql-where-statements-run)
+ [Privileges](#databaseclustergroups-sql-privileges)
+ [Order of creation](#databaseclustergroups-sql-order-of-creation)
+ [SQL reference conventions](#databaseclustergroups-sql-conventions)
+ [Statements that database cluster groups for Aurora don't support](#databaseclustergroups-sql-unsupported)
+ [Routing function statements](databaseclustergroups-sql-routing-functions.md)
+ [Scaleout table statements](databaseclustergroups-sql-scaleout.md)
+ [Data mapping statements](databaseclustergroups-sql-mappings.md)
+ [Data movement statements](databaseclustergroups-sql-data-movement.md)
+ [Session and EXPLAIN statements](databaseclustergroups-sql-session-move.md)

## SQL commands
<a name="databaseclustergroups-sql-commands"></a>

The following table lists every statement in this reference. The following pages group the statements: [Routing function statements](databaseclustergroups-sql-routing-functions.md), [Scaleout table statements](databaseclustergroups-sql-scaleout.md), [Data mapping statements](databaseclustergroups-sql-mappings.md), [Data movement statements](databaseclustergroups-sql-data-movement.md), and [Session and EXPLAIN statements](databaseclustergroups-sql-session-move.md). No statement in this reference runs on a member cluster directly.


| Statement | What it does | Runs on | 
| --- | --- | --- | 
| [CREATE SCALEOUT ROUTING FUNCTION](databaseclustergroups-sql-create-scaleout-routing-function.md) | Creates a routing function that maps a routing key to collocations. | Routing endpoint, unpinned session | 
| [DROP SCALEOUT ROUTING FUNCTION](databaseclustergroups-sql-drop-scaleout-routing-function.md) | Removes a routing function that no scaleout table uses. | Routing endpoint, unpinned session | 
| [SHOW CREATE SCALEOUT ROUTING FUNCTION](databaseclustergroups-sql-show-create-scaleout-routing-function.md) | Returns the definition of one routing function. | Routing endpoint, unpinned session | 
| [SHOW SCALEOUT ROUTING FUNCTIONS](databaseclustergroups-sql-show-scaleout-routing-functions.md) | Lists the routing functions in the group. | Routing endpoint, unpinned session | 
| [CREATE SCALEOUT TABLE](databaseclustergroups-sql-create-scaleout-table.md) | Creates one table name at the routing endpoint for rows spread across member tables. | Routing endpoint, unpinned session | 
| [ALTER SCALEOUT TABLE](databaseclustergroups-sql-alter-scaleout-table.md) | Switches the routing function, or adds and removes member tables. | Routing endpoint, unpinned session | 
| [DROP SCALEOUT TABLE](databaseclustergroups-sql-drop-scaleout-table.md) | Removes a scaleout table. By default, member tables and rows stay. | Routing endpoint, unpinned session | 
| [SHOW CREATE SCALEOUT TABLE](databaseclustergroups-sql-show-create-scaleout-table.md) | Returns the definition of one scaleout table. | Routing endpoint, unpinned session | 
| [SHOW SCALEOUT TABLES](databaseclustergroups-sql-show-scaleout.md) | Lists the scaleout tables in the group. | Routing endpoint, unpinned session | 
| [SELECT … FROM dbclustergroup\_sys.data\_mappings](databaseclustergroups-sql-select-data-mappings.md) | Reads the data mappings. | Routing endpoint, unpinned session | 
| [INSERT INTO dbclustergroup\_sys.data\_mappings](databaseclustergroups-sql-insert-data-mappings.md) | Adds a mapping that places a database or a table on a member cluster. | Routing endpoint, unpinned session, autocommit mode | 
| [UPDATE dbclustergroup\_sys.data\_mappings](databaseclustergroups-sql-update-data-mappings.md) | Changes the cluster name behind a router name, or repoints or renames a collocation. | Routing endpoint, unpinned session, autocommit mode | 
| [DELETE FROM dbclustergroup\_sys.data\_mappings](databaseclustergroups-sql-delete-data-mappings.md) | Removes a mapping. The object on the member cluster stays. | Routing endpoint, unpinned session, autocommit mode | 
| [EXPLAIN ALTER DBCLUSTERGROUP MOVE COLLOCATION](databaseclustergroups-sql-explain-move-collocation.md) | Checks a move of one collocation without starting it. | Routing endpoint, unpinned session | 
| [ALTER DBCLUSTERGROUP MOVE COLLOCATION](databaseclustergroups-sql-move-collocation.md) | Starts a data movement job that moves one collocation to another member cluster. | Routing endpoint, unpinned session | 
| [ALTER DBCLUSTERGROUP CUTOVER JOB](databaseclustergroups-sql-switch-job.md) | Starts the cutover of a job in the CDC Replication phase. | Routing endpoint, unpinned session | 
| [ALTER DBCLUSTERGROUP CANCEL JOB](databaseclustergroups-sql-cancel-job.md) | Cancels a data movement job. | Routing endpoint, unpinned session | 
| [SELECT … FROM dbclustergroup\_data\_movement.move\_info](databaseclustergroups-sql-move-info.md) | Reads the status of data movement jobs. There is no SHOW statement for moves. | Routing endpoint, unpinned session | 
| [SET router\_cluster\_pin](databaseclustergroups-sql-set-router-cluster-pin.md) | Pins the session to one member cluster, or clears the pin. | Routing endpoint, pinned or unpinned session | 
| [SET scaleout\_tables\_preferred\_member](databaseclustergroups-sql-set-scaleout-tables-preferred-member.md) | Names the member table that answers SHOW CREATE TABLE and DESCRIBE for a scaleout table. | Routing endpoint, unpinned session | 
| [EXPLAIN](databaseclustergroups-sql-explain.md) | Shows which member cluster a routed statement reaches, and that member cluster's plan. | Routing endpoint, unpinned session | 

## Where these statements run
<a name="databaseclustergroups-sql-where-statements-run"></a>

Run these statements at the routing endpoint, in a session that isn't pinned to a member cluster. The exception is `SET router_cluster_pin`, which also clears a pin. A pinned session passes its statements through to its member cluster unchanged, and you can't reach a scaleout table through a pinned session.

A pinned session doesn't run a statement on a `dbclustergroup_sys` table or a `SCALEOUT` statement. It forwards the statement to the member cluster, where the statement fails. The member cluster doesn't recognize these keywords, so it returns a syntax error. There is no separate error code for this case. Clear the pin to run the statement.

Run each statement in autocommit mode, outside an explicit transaction:
+ Data mapping changes aren't transactional. On Aurora MySQL, the routing layer refuses a data mapping write inside a transaction, the write changes nothing, and the transaction stays open.
+ A data mapping or routing function change reaches the routing layer within a short time. A statement routed on an earlier mapping returns Aurora MySQL error 1412; the session stays open, and a retry routes on the new mapping. Inside a transaction, a statement that needs a mapping 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).
+ On Aurora MySQL, the routing layer refuses a routing function or scaleout table statement inside a transaction, with error 1179, `… is not allowed inside a transaction (opened by BEGIN/START TRANSACTION or SET autocommit=0)`.
+ One multi-row `INSERT` into `data_mappings` applies all its rows or none.

  One statement can add, change, or remove at most 1,000 data mappings. For more information about the limit, see [Routing metadata and naming limits](databaseclustergroups-limitations.md#databaseclustergroups-limitations-routing-metadata-limits).
+ A sequence of these statements isn't atomic. If one statement fails, the statements before it stay applied. Check the result of each statement before you send the next.

## Privileges
<a name="databaseclustergroups-sql-privileges"></a>

The routing layer checks its own privileges for database cluster groups for Aurora for these statements. Each member cluster checks the data privileges of the member cluster user when a routed statement runs there. Each command page names the privilege that it needs.


| Privilege for database cluster groups for Aurora | Statements | 
| --- | --- | 
| dbclustergroup\_metadata\_admin | CREATE and DROP SCALEOUT ROUTING FUNCTION; CREATE, ALTER, and DROP SCALEOUT TABLE; SELECT, INSERT, UPDATE, and DELETE on dbclustergroup\_sys.data\_mappings | 
| dbclustergroup\_move\_admin | ALTER DBCLUSTERGROUP MOVE COLLOCATION, EXPLAIN ALTER DBCLUSTERGROUP MOVE COLLOCATION, ALTER DBCLUSTERGROUP CUTOVER JOB, ALTER DBCLUSTERGROUP CANCEL JOB | 
| dbclustergroup\_super\_admin | Every statement in this reference. It implies every other privilege for database cluster groups for Aurora. | 
| dbclustergroup\_metadata\_read\_only | Read data\_mappings, member\_cluster\_mappings, and move\_info, without write access. | 
| None | SET SESSION router\_cluster\_pin, SET scaleout\_tables\_preferred\_member, EXPLAIN of a routed statement, SHOW SCALEOUT TABLES, SHOW SCALEOUT ROUTING FUNCTIONS, SHOW CREATE SCALEOUT TABLE, and SHOW CREATE SCALEOUT ROUTING FUNCTION | 

A statement without its privilege for database cluster groups for Aurora returns an access-denied error that names the missing privilege. For the privileges that manage router users, sessions, and connections (`dbclustergroup_user_admin`, `dbclustergroup_process_admin`, and `dbclustergroup_connection_admin`), see [Router users](databaseclustergroups-router-users.md).

## Order of creation
<a name="databaseclustergroups-sql-order-of-creation"></a>

Create the objects of a scaleout table in the following order, and drop them in the reverse order. For a worked example with loading, see [Creating and loading a scaleout table](databaseclustergroups-create-scaleout-table.md).

**To create the objects of a scaleout table**

1. On each member cluster, through a direct connection or a session pinned to that member cluster, create its member tables. This step is optional if the member table already exists:

   ```sql
   SET SESSION router_cluster_pin = '{{member_cluster_alias}}';
   ```

   You can also write `'{{member_cluster_alias}}@{{database}}'`, which selects the database as `USE` does.

1. At the routing endpoint, map each member table with one table-level data mapping. Put each member table of one scaleout table in a different collocation. See [INSERT INTO dbclustergroup\_sys.data\_mappings](databaseclustergroups-sql-insert-data-mappings.md).

1. At the routing endpoint, create the routing function, which can name only collocations that a data mapping already created. See [CREATE SCALEOUT ROUTING FUNCTION](databaseclustergroups-sql-create-scaleout-routing-function.md).

1. At the routing endpoint, create the scaleout table. See [CREATE SCALEOUT TABLE](databaseclustergroups-sql-create-scaleout-table.md).

1. At the routing endpoint, insert and read one key for each collocation, and check each with [EXPLAIN](databaseclustergroups-sql-explain.md). The routing rows of each `EXPLAIN` show the member cluster that the key reaches.

To remove a scaleout table, run `DROP SCALEOUT TABLE` and then `DROP SCALEOUT ROUTING FUNCTION`. Then delete the data mappings, and drop the member tables on their member clusters. Note that `DROP SCALEOUT TABLE` doesn't remove the actual table on the member clusters. It only removes the metadata used to route queries through scaleout tables.

Keep your own record of every `CREATE SCALEOUT ROUTING FUNCTION` and `CREATE SCALEOUT TABLE` statement that you run. You read the definitions back only with the `SHOW CREATE` statements, and you can't restore the routing rules on their own. Keep the record with your export of the data mappings. See [Exporting the mappings before and after a change](databaseclustergroups-changing-mappings.md#databaseclustergroups-export-mappings).

## SQL reference conventions
<a name="databaseclustergroups-sql-conventions"></a>

The SQL reference for database cluster groups for Aurora uses the following conventions.

### Syntax notation
<a name="databaseclustergroups-sql-notation"></a>

The syntax blocks in this reference use the following notation.

`CAPS`  
Words in uppercase are keywords. Enter them as shown.

`{{name}}`  
Words in italics are placeholders. Replace them with your own value. Parameters and error texts write a placeholder as `<name>`.

`[ ]`  
Square brackets enclose an optional part. Don't enter the brackets.

`{ a | b }`  
Braces enclose a choice. Enter one of the values separated by vertical bars, without the braces.

`...`  
An ellipsis means that you can repeat the preceding item.

`…`  
In an error text or a statement in a table, an ellipsis marks text that the reference leaves out.

### Names and quoting
<a name="databaseclustergroups-sql-names"></a>

The following rules apply to names and quoting in these statements.
+ In data mapping writes, every value is a quoted string, `member_cluster_alias` included.
+ In the move statements, the collocation and the member cluster aliases are identifiers. On Aurora MySQL, enclose them in backticks, which an alias that contains a hyphen needs.
+ A job ID is a string literal in single quotes.
+ Collocation names are case-sensitive.

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

The examples use the following objects:
+ A database `db`.
+ Member cluster aliases `member-a`, `member-b`, and `member-c`.
+ A routing function `orders_mod_fn` with the collocations `coll_0` and `coll_1`, and a routing function `tenant_rf` with twelve collocations, `cg_00` to `cg_11`.
+ A scaleout table `orders`, keyed on `customer_id`, whose member tables have the router table names `orders_s1` and `orders_s2`, and a scaleout table `items` that stays with it.

Each example is a SQL statement that you run at the routing endpoint, unless the example says otherwise. Sample output follows the statement where it helps.

## Statements that database cluster groups for Aurora don't support
<a name="databaseclustergroups-sql-unsupported"></a>

The routing layer refuses the following statement forms. Use the alternative in the last column.


| Statement | Aurora MySQL | Instead | 
| --- | --- | --- | 
| ALTER SCALEOUT ROUTING FUNCTION … | [1064](databaseclustergroups-errors-routing.md#databaseclustergroups-routing-and-statement-errors) | Create a new routing function and switch the table to it. See [Changing a routing function](databaseclustergroups-changing-routing-function.md). | 
| CREATE OR REPLACE SCALEOUT ROUTING FUNCTION … | [1235](databaseclustergroups-errors-routing.md#databaseclustergroups-routing-and-statement-errors) | Create a new routing function and switch the table to it. | 
| PARTITION BY a function other than MOD, HASH, PREFIX, or COLUMN | [1305](databaseclustergroups-errors-scaleout.md#databaseclustergroups-data-mapping-and-scaleout-ddl-errors) | A MOD, HASH, PREFIX, or COLUMN function. For a scheme that doesn't map to one of these, connect with session pinning, or map each database with a data mapping. See [Routing functions](databaseclustergroups-scaleout-tables.md#databaseclustergroups-routing-function-availability). | 
| Two actions in one ALTER SCALEOUT TABLE statement | [1064](databaseclustergroups-errors-routing.md#databaseclustergroups-routing-and-statement-errors) | One action for each statement | 
| DESCRIBE SCALEOUT TABLE … | [1064](databaseclustergroups-errors-routing.md#databaseclustergroups-routing-and-statement-errors) | SHOW CREATE SCALEOUT TABLE | 
| INSERT … SELECT into a scaleout table | [1235](databaseclustergroups-errors-routing.md#databaseclustergroups-routing-and-statement-errors) | INSERT … VALUES with the routing key in each row | 
| An UPDATE that changes a routing key column | [1235](databaseclustergroups-errors-routing.md#databaseclustergroups-routing-and-statement-errors) | Choose a routing key whose value doesn't change. See [Choosing a routing key and routing function](databaseclustergroups-routing-key.md). | 
| Data definition on a scaleout table name, such as ALTER TABLE or CREATE INDEX | [1235](databaseclustergroups-errors-routing.md#databaseclustergroups-routing-and-statement-errors) | Run the same statement on each member table, through a pinned session or a direct connection. See [Running DDL through a session pin](databaseclustergroups-schema-changes.md#databaseclustergroups-pin-ddl). | 
| GRANT or REVOKE on a scaleout table | Refused | Grant the privileges on each member table, on its member cluster. | 
| LOCK TABLES on a scaleout table | [1235](databaseclustergroups-errors-routing.md#databaseclustergroups-routing-and-statement-errors) | Lock the member tables on their member clusters, through a pinned session or a direct connection. | 
| A view over a scaleout table | [1235](databaseclustergroups-errors-routing.md#databaseclustergroups-routing-and-statement-errors) | Query the scaleout table with its routing key. | 
| LOAD DATA or another bulk load into a scaleout table at the routing endpoint | [1235](databaseclustergroups-errors-routing.md#databaseclustergroups-routing-and-statement-errors) | Load each member table through a pinned session or a direct connection. See [Loading data into member clusters](databaseclustergroups-loading-data.md). | 
| SHOW SCALEOUT TABLES or SHOW SCALEOUT ROUTING FUNCTIONS with a LIKE or WHERE clause | Refused | Omit the LIKE or WHERE clause. | 