1. Home
  2. Knowledge Base
  3. ClickHouse DBA
  4. ClickHouse ATTACH Statement

ClickHouse ATTACH Statement

Attaches a table or a dictionary, for example, when moving a database to another server.

The query does not create data on the disk, but assumes that data is already in the appropriate places, and just adds information about the table or the dictionary to the server. After executing the ATTACH query, the server will know about the existence of the table or the dictionary.

If a table was previously detached (DETACH query), meaning that its structure is known, you can use shorthand without defining the structure.

Attach Existing Table

ATTACH TABLE [IF NOT EXISTS] [db.]name [ON CLUSTER cluster]

This query is used when starting the server. The server stores table metadata as files with ATTACH queries, which it simply runs at launch (with the exception of some system tables, which are explicitly created on the server).

If the table was detached permanently, it won’t be reattached at the server start, so you need to use ATTACH query explicitly.

Create New Table And Attach Data

With Specified Path to Table Data

ATTACH TABLE name FROM 'path/to/data/' (col1 Type1, ...)

Example

DROP TABLE IF EXISTS test;
INSERT INTO TABLE FUNCTION file('0047_attach_data/test/data.TSV', 'TSV', 's String, n UInt8') VALUES ('test', 47);
ATTACH TABLE test FROM '0047_attach_data/test' (s String, n UInt8) ENGINE = File(TSV);
SELECT * FROM test;

Result;

┌─s────┬──n─┐
│ test │ 47 │
└──────┴────┘

With Specified Table UUID

ATTACH TABLE name UUID '<uuid>' (col1 Type1, ...)

Attach Existing Dictionary

Attaches a previously detached dictionary.

ATTACH DICTIONARY [IF NOT EXISTS] [db.]name [ON CLUSTER cluster]

 

To more information please visit officical ClickHouse docs in here.

Was this article helpful?

Related Articles

CHISTADATA IS COMMITTED TO OPEN SOURCE SOFTWARE AND BUILDING HIGH PERFORMANCE COLUMNSTORES

In the spirit of freedom, independence and innovation. ChistaDATA Corporation is not affiliated with ClickHouse Corporation 

Need Support?

Can't find the answer you're looking for?
Contact Support

ChistaDATA Inc. Knowledge base is licensed under the Apache License, Version 2.0 (the “License”)

Copyright 2022 ChistaDATA Inc

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.