> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-mintlify-8c05c8a2.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> EXCHANGE ステートメントのドキュメント

# EXCHANGE ステートメント

2 つのテーブルまたはDictionaryの名前をアトミックに入れ替えます。
この処理は、一時的な名前を使った [`RENAME`](/ja/reference/statements/rename) クエリでも実現できますが、その場合、この操作はアトミックではありません。

<Note>
  `EXCHANGE` クエリは、[`Atomic`](/ja/reference/engines/database-engines/atomic) および [`Shared`](/ja/products/cloud/features/infrastructure/shared-catalog#shared-database-engine) データベースエンジンでのみサポートされています。
</Note>

**構文**

```sql theme={null}
EXCHANGE TABLES|DICTIONARIES [db0.]name_A AND [db1.]name_B [ON CLUSTER cluster]
```

<div id="exchange-tables">
  ## EXCHANGE TABLES
</div>

2つのテーブル名を入れ替えます。

**構文**

```sql theme={null}
EXCHANGE TABLES [db0.]table_A AND [db1.]table_B [ON CLUSTER cluster]
```

<div id="exchange-multiple-tables">
  ### 複数のテーブルを EXCHANGE する
</div>

複数のテーブルペアは、カンマで区切ることで 1 つのクエリ内で EXCHANGE できます。

<Note>
  複数のテーブルペアを EXCHANGE する場合、処理は **アトミックではなく、順次** 実行されます。操作中にエラーが発生すると、一部のテーブルペアは EXCHANGE されても、ほかはされていない可能性があります。
</Note>

**例**

```sql title="Query" theme={null}
-- テーブルを作成する
CREATE TABLE a (a UInt8) ENGINE=Memory;
CREATE TABLE b (b UInt8) ENGINE=Memory;
CREATE TABLE c (c UInt8) ENGINE=Memory;
CREATE TABLE d (d UInt8) ENGINE=Memory;

-- 1つのクエリで2組のテーブルを交換する
EXCHANGE TABLES a AND b, c AND d;

SHOW TABLE a;
SHOW TABLE b;
SHOW TABLE c;
SHOW TABLE d;
```

```sql title="Response" theme={null}
-- テーブル 'a' は 'b' の構造を持ち、テーブル 'b' は 'a' の構造を持つようになった
┌─statement──────────────┐
│ CREATE TABLE default.a↴│
│↳(                     ↴│
│↳    `b` UInt8         ↴│
│↳)                     ↴│
│↳ENGINE = Memory        │
└────────────────────────┘
┌─statement──────────────┐
│ CREATE TABLE default.b↴│
│↳(                     ↴│
│↳    `a` UInt8         ↴│
│↳)                     ↴│
│↳ENGINE = Memory        │
└────────────────────────┘

-- テーブル 'c' は 'd' の構造を持ち、テーブル 'd' は 'c' の構造を持つようになった
┌─statement──────────────┐
│ CREATE TABLE default.c↴│
│↳(                     ↴│
│↳    `d` UInt8         ↴│
│↳)                     ↴│
│↳ENGINE = Memory        │
└────────────────────────┘
┌─statement──────────────┐
│ CREATE TABLE default.d↴│
│↳(                     ↴│
│↳    `c` UInt8         ↴│
│↳)                     ↴│
│↳ENGINE = Memory        │
└────────────────────────┘
```

<div id="exchange-dictionaries">
  ## EXCHANGE DICTIONARIES
</div>

2つのDictionaryの名前を入れ替えます。

**構文**

```sql theme={null}
EXCHANGE DICTIONARIES [db0.]dict_A AND [db1.]dict_B [ON CLUSTER cluster]
```

**関連項目**

* [Dictionaries](/ja/reference/statements/create/dictionary)
