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

> Documentação da instrução EXCHANGE

# Instrução EXCHANGE

Troca, de forma atômica, os nomes de duas tabelas ou dicionários.
Essa tarefa também pode ser realizada com uma consulta [`RENAME`](/pt-BR/reference/statements/rename) usando um nome temporário, mas, nesse caso, a operação não é atômica.

<Note>
  A consulta `EXCHANGE` é compatível apenas com os motores de banco de dados [`Atomic`](/pt-BR/reference/engines/database-engines/atomic) e [`Shared`](/pt-BR/products/cloud/features/infrastructure/shared-catalog#shared-database-engine).
</Note>

**Sintaxe**

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

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

Troca os nomes de duas tabelas.

**Sintaxe**

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

<div id="exchange-multiple-tables">
  ### EXCHANGE MÚLTIPLAS TABELAS
</div>

Você pode trocar vários pares de tabelas em uma única consulta, separando-os por vírgulas.

<Note>
  Ao trocar vários pares de tabelas, as trocas são realizadas **sequencialmente, e não de forma atômica**. Se ocorrer um erro durante a operação, alguns pares de tabelas podem já ter sido trocados, enquanto outros não.
</Note>

**Exemplo**

```sql title="Query" theme={null}
-- Criar tabelas
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;

-- Trocar dois pares de tabelas em uma única consulta
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}
-- Agora a tabela 'a' tem a estrutura de 'b', e a tabela 'b' tem a estrutura de 'a'
┌─statement──────────────┐
│ CREATE TABLE default.a↴│
│↳(                     ↴│
│↳    `b` UInt8         ↴│
│↳)                     ↴│
│↳ENGINE = Memory        │
└────────────────────────┘
┌─statement──────────────┐
│ CREATE TABLE default.b↴│
│↳(                     ↴│
│↳    `a` UInt8         ↴│
│↳)                     ↴│
│↳ENGINE = Memory        │
└────────────────────────┘

-- Agora a tabela 'c' tem a estrutura de 'd', e a tabela 'd' tem a estrutura de '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>

Troca os nomes de dois dicionários.

**Sintaxe**

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

**Veja também**

* [Dicionários](/pt-BR/reference/statements/create/dictionary)
