

# INSERT INTO dbclustergroup\_sys.data\_mappings

Use `INSERT INTO dbclustergroup_sys.data_mappings` to place a database or a table on a member cluster of a database cluster group for Aurora.

## Description
<a name="databaseclustergroups-sql-insert-data-mappings-description"></a>

Adds data mappings: rows that place a database or a table on a member cluster, under a router name, in a collocation. A mapping write moves no data. It changes only where the routing layer sends statements for that object.

For the columns of the table, see [data\_mappings](databaseclustergroups-sysviews-data-mappings.md). For the concepts, see [Data mappings and collocations](databaseclustergroups-data-mappings.md). For the task, see [Changing data mappings](databaseclustergroups-changing-mappings.md).

## Syntax
<a name="databaseclustergroups-sql-insert-data-mappings-syntax"></a>

The columns that you name differ, as the parameters describe.

```sql
INSERT INTO dbclustergroup_sys.data_mappings
  ({{column}} [, {{column}} ...])
VALUES ('{{value}}' [, '{{value}}' ...]) [, ('{{value}}' [, ...]) ...];
```

## Parameters and options
<a name="databaseclustergroups-sql-insert-data-mappings-parameters"></a>

Each parameter is a column of `data_mappings`. The column list is required, and every value is a single-quoted string.

cluster\_db\_name  
Required. The database name on the member cluster (the cluster database name).

cluster\_table\_name  
The table name on the member cluster. Omit it for a database-level mapping.

router\_db\_name  
The database name that applications use through the routing endpoint. Defaults to `cluster_db_name`.

router\_table\_name  
The table name that applications use through the routing endpoint (the router table name). Defaults to `cluster_table_name`. For a member table of a scaleout table, it's the name that the `MEMBERS` clause of `CREATE SCALEOUT TABLE` names.

member\_cluster\_alias  
Required. A `member_cluster_alias` value that `dbclustergroup_sys.member_cluster_mappings` lists. Copy it from the listing, and don't derive it from a DB cluster identifier. The routing layer refuses a member cluster alias that the listing doesn't show and writes no row. The listing shows a member cluster that is still joining only after it can route.

router\_collocation\_group  
The collocation of the object. Name a new collocation, or a collocation that is already on the same member cluster. If you omit it, the mapping takes the `member_cluster_alias` value as its collocation, so every object mapped that way on one member cluster moves together. Name a collocation where you want finer control.

db\_cluster\_identifier  
Read-only. Leave it out.

## Where it runs and required privileges
<a name="databaseclustergroups-sql-insert-data-mappings-privileges"></a>

Run the statement at the routing endpoint, in a session in query routing mode that isn't pinned, and outside a transaction. A pinned session passes the statement through to its member cluster, where it fails. On Aurora MySQL, the routing layer refuses a mapping write inside a transaction, and a session with `autocommit=0` counts as an open transaction. Run `SET autocommit = 1`, or commit, before the write.

The routing layer accepts a read of `data_mappings` inside a transaction.

The router user needs `dbclustergroup_metadata_admin` or `dbclustergroup_super_admin`. Reading `data_mappings` with `SELECT` needs `dbclustergroup_metadata_read_only`, `dbclustergroup_metadata_admin`, or `dbclustergroup_super_admin`. Without one, the read fails with error 1227. For all privileges for database cluster groups for Aurora, see [Privileges](databaseclustergroups-sql-reference.md#databaseclustergroups-sql-privileges).

## Usage notes
<a name="databaseclustergroups-sql-insert-data-mappings-usage"></a>

Keep the following in mind when you use the statement.
+ A mapping change reaches the routing layer within a short time. A statement that the routing layer routed on an older mapping returns a retryable error, and a retry routes on the new mapping. For what an open session and an open transaction see, see [Applying data mapping changes](databaseclustergroups-data-mappings.md#databaseclustergroups-when-a-data-mapping-change-takes-effect).
+ Mapping writes aren't transactional. A multi-row `INSERT` applies all its rows or none, but `BEGIN` and `COMMIT` don't group separate statements, and a failed statement doesn't undo the statements before it. Put each set of mappings that must appear together in one multi-row `INSERT`, so that each intermediate state still routes.
+ One `INSERT` can add at most 1,000 mappings. A larger statement fails with error 1235 and adds nothing, for example `put of 1500 entries exceeds the per-request limit of 1000; split the write into smaller requests`. For more information about the limit, see [Routing metadata and naming limits](databaseclustergroups-limitations.md#databaseclustergroups-limitations-routing-metadata-limits).
+ Create the object on the member cluster first. The routing layer doesn't create it, and a mapping with a wrong cluster name registers without an error. Check each pair of cluster names against the member cluster that the member cluster alias points at.
+ A database has one mapping level. While a database has a database-level mapping, the routing layer refuses a table-level mapping in it. See [Using database-level and table-level mappings](databaseclustergroups-mapping-levels.md).
+ Every fully qualified router name (database and table) is unique across the group. An object on a member cluster has one mapping. To change its router name, update the row. See [UPDATE dbclustergroup\_sys.data\_mappings](databaseclustergroups-sql-update-data-mappings.md).
+ A mapping names explicit values, and the routing layer reads `%` and `*` as ordinary characters, not as patterns. To place every table of a database, map the database.
+ Database and table names can't be empty and can't contain a period, white space, a control character, a character above U\+FFFF, or any of ```, `"`, `'`, `\`, `,`, `(`, `/*`, and `*/`. A collocation name accepts letters, digits, `_`, `$`, and `-`. The routing layer reserves the names of its system tables, in any letter case and in any name column: `dbclustergroup_sys`, `data_mappings`, `member_cluster_mappings`, `router_user_definition`, `dbclustergroup_data_movement`, `move_info`, and `router_processlist`. A `router_collocation_group` value can use them.
+ Export the mappings before and after every change. See [Exporting the mappings before and after a change](databaseclustergroups-changing-mappings.md#databaseclustergroups-export-mappings).

A `router_collocation_group` value is stored as you write it and compared case-sensitively, so `C1` and `c1` are two collocations. It can contain letters, digits, `_`, `$`, and `-`.

While a data movement job moves a collocation, the routing layer refuses a mapping write that touches the collocation or any object in it, with error 1205 (`Metadata for ... is being changed by another operation`).

A mapping change needs an available writer instance on every member cluster that it affects. This includes both the old and the new member cluster when you repoint a mapping. If one has no available writer, the change fails and nothing changes. The member clusters don't need to be reachable through the routing endpoint, because the routing layer reaches them directly.

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

The following examples map two tables of the database `app_db` on one member cluster, each in its own collocation, in one multi-row `INSERT`, and read the rows back. Filter a read with one equality or one tuple equality, and sort in your client. For the read, see [SELECT FROM dbclustergroup\_sys.data\_mappings](databaseclustergroups-sql-select-data-mappings.md).

```sql
INSERT INTO dbclustergroup_sys.data_mappings
  (cluster_db_name, cluster_table_name, router_db_name, router_table_name,
   member_cluster_alias, router_collocation_group)
VALUES ('app_db', 'orders', 'app_db', 'orders', 'member-a', 'cg_orders'),
       ('app_db', 'customers', 'app_db', 'customers', 'member-a', 'cg_customers');

SELECT * FROM dbclustergroup_sys.data_mappings
 WHERE (router_db_name, router_table_name) = ('app_db', 'orders');
```

The following example maps the whole database `global` on `member-a`.

```sql
INSERT INTO dbclustergroup_sys.data_mappings
  (cluster_db_name, router_db_name, member_cluster_alias, router_collocation_group)
VALUES ('global', 'global', 'member-a', 'cg_global');
```

## Errors
<a name="databaseclustergroups-sql-insert-data-mappings-errors"></a>

The statement can return the following errors. A refused statement writes no row. For the error texts and all errors from the routing layer, see [Routing and data mapping errors](databaseclustergroups-errors-routing.md).


| Aurora MySQL | Meaning | 
| --- | --- | 
| [1227](databaseclustergroups-errors-routing.md) (42000) | The router user lacks dbclustergroup\_metadata\_admin. | 
| [1235](databaseclustergroups-errors-routing.md) (42000) | The write is inside a transaction. Commit or roll back, and send it again outside a transaction. | 
| [64004](databaseclustergroups-errors-routing.md) (42000) | Member cluster '<alias>' doesn't exist: the member\_cluster\_alias value isn't in member\_cluster\_mappings. | 
| [64060](databaseclustergroups-errors-routing.md) (42000) | The database already has a mapping of the other level. One database holds database-level or table-level mappings, not both. | 
| [64062](databaseclustergroups-errors-routing.md) (42000) | The collocation is on another member cluster. | 
| [64021](databaseclustergroups-errors-routing.md) (42000) | Table '<db>.<table>' already has a data mapping (Database '<db>' ... for a database-level mapping): the router name is already mapped. | 
| [1235](databaseclustergroups-errors-routing.md) (42000) | physical table '<db>.<table>' is already mapped on member cluster '<alias>' as '<router name>' (physical database '<db>' ... for a database-level mapping), or physical table '<db>.<table>' on member cluster '<alias>' overlaps the database mapping '<router name>' -> '<db>': the object on that member cluster already has a mapping, under the router name in the message. | 
| [1235](databaseclustergroups-errors-routing.md) (42000) | Column 'cluster\_db\_name' cannot be empty, or Column 'member\_cluster\_alias' cannot be NULL: a required column is missing. | 
| [1235](databaseclustergroups-errors-routing.md) (42000) | Column list is required for INSERT, or Only simple INSERT INTO ... VALUES (...) is supported: the statement has no column list, or uses REPLACE, INSERT IGNORE, ON DUPLICATE KEY UPDATE, or INSERT ... SET. To change a mapping, use UPDATE. | 
| [1235](databaseclustergroups-errors-routing.md) (42000) | Reserved identifier: '<name>' cannot be used as a name: a name is reserved. | 
| [1235](databaseclustergroups-errors-routing.md) (42000) | put of <n> entries exceeds the per-request limit of 1000; split the write into smaller requests: your statement adds more than 1,000 mappings, so it adds nothing. Split the rows across several statements. | 
| [1235](databaseclustergroups-errors-routing.md) (42000) | db\_cluster\_identifier is read-only: the statement sets db\_cluster\_identifier. | 
| [1059](databaseclustergroups-errors-routing.md) (42000) | A name is longer than 64 characters. | 
| [1412](databaseclustergroups-errors-routing.md) (HY000) | After a mapping change, a statement that the routing layer routed on the older mapping returns this error. On Aurora MySQL, the statement ends and an open transaction stays open, so retry the statement or the transaction. | 

## Related statements
<a name="databaseclustergroups-sql-insert-data-mappings-related"></a>

For more information, see the following:
+ [SELECT FROM dbclustergroup\_sys.data\_mappings](databaseclustergroups-sql-select-data-mappings.md)
+ [UPDATE dbclustergroup\_sys.data\_mappings](databaseclustergroups-sql-update-data-mappings.md)
+ [DELETE FROM dbclustergroup\_sys.data\_mappings](databaseclustergroups-sql-delete-data-mappings.md)
+ [CREATE SCALEOUT ROUTING FUNCTION](databaseclustergroups-sql-create-scaleout-routing-function.md)
+ [CREATE SCALEOUT TABLE](databaseclustergroups-sql-create-scaleout-table.md)
+ [ALTER DBCLUSTERGROUP MOVE COLLOCATION](databaseclustergroups-sql-move-collocation.md)