ClickHouse/docs/en/sql-reference/statements/detach.md

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

77 lines
2.4 KiB
Markdown
Raw Normal View History

2020-07-11 11:05:49 +00:00
---
2022-08-28 14:53:34 +00:00
slug: /en/sql-reference/statements/detach
sidebar_position: 43
sidebar_label: DETACH
2022-08-29 16:19:50 +00:00
title: "DETACH Statement"
2020-07-11 11:05:49 +00:00
---
Makes the server "forget" about the existence of a table, a materialized view, or a dictionary.
2021-02-15 21:32:39 +00:00
2021-08-01 16:17:50 +00:00
**Syntax**
2020-07-11 11:05:49 +00:00
``` sql
DETACH TABLE|VIEW|DICTIONARY [IF EXISTS] [db.]name [ON CLUSTER cluster] [PERMANENTLY] [SYNC]
2020-07-11 11:05:49 +00:00
```
Detaching does not delete the data or metadata of a table, a materialized view or a dictionary. If an entity was not detached `PERMANENTLY`, on the next server launch the server will read the metadata and recall the table/view/dictionary again. If an entity was detached `PERMANENTLY`, there will be no automatic recall.
2021-08-01 16:17:50 +00:00
Whether a table or a dictionary was detached permanently or not, in both cases you can reattach them using the [ATTACH](../../sql-reference/statements/attach.md) query.
2021-08-01 13:02:35 +00:00
System log tables can be also attached back (e.g. `query_log`, `text_log`, etc). Other system tables can't be reattached. On the next server launch the server will recall those tables again.
2021-05-27 19:44:11 +00:00
`ATTACH MATERIALIZED VIEW` does not work with short syntax (without `SELECT`), but you can attach it using the `ATTACH TABLE` query.
2021-07-29 15:20:55 +00:00
Note that you can not detach permanently the table which is already detached (temporary). But you can attach it back and then detach permanently again.
2023-03-18 02:45:43 +00:00
Also you can not [DROP](../../sql-reference/statements/drop.md#drop-table) the detached table, or [CREATE TABLE](../../sql-reference/statements/create/table.md) with the same name as detached permanently, or replace it with the other table with [RENAME TABLE](../../sql-reference/statements/rename.md) query.
2020-07-11 11:05:49 +00:00
The `SYNC` modifier executes the action without delay.
2021-03-10 09:56:13 +00:00
**Example**
Creating a table:
2021-03-10 09:56:13 +00:00
2021-03-12 16:14:40 +00:00
Query:
2021-03-10 09:56:13 +00:00
``` sql
CREATE TABLE test ENGINE = Log AS SELECT * FROM numbers(10);
SELECT * FROM test;
2021-03-12 16:14:40 +00:00
```
Result:
``` text
┌─number─┐
│ 0 │
│ 1 │
│ 2 │
│ 3 │
│ 4 │
│ 5 │
│ 6 │
│ 7 │
│ 8 │
│ 9 │
└────────┘
```
Detaching the table:
Query:
``` sql
DETACH TABLE test;
SELECT * FROM test;
```
Result:
``` text
Received exception from server (version 21.4.1):
2021-05-27 19:44:11 +00:00
Code: 60. DB::Exception: Received from localhost:9000. DB::Exception: Table default.test does not exist.
2021-03-12 16:14:40 +00:00
```
2021-03-10 09:56:13 +00:00
2021-08-01 13:02:35 +00:00
**See Also**
- [Materialized View](../../sql-reference/statements/create/view.md#materialized)
- [Dictionaries](../../sql-reference/dictionaries/index.md)