2020-07-09 15:10:35 +00:00
---
2022-08-28 14:53:34 +00:00
slug: /en/sql-reference/statements/create/dictionary
2022-04-09 13:29:05 +00:00
sidebar_position: 38
sidebar_label: DICTIONARY
2022-08-29 16:19:50 +00:00
title: "CREATE DICTIONARY"
2020-07-09 15:10:35 +00:00
---
2023-03-18 02:45:43 +00:00
Creates a new [dictionary ](../../../sql-reference/dictionaries/index.md ) with given [structure ](../../../sql-reference/dictionaries/index.md#dictionary-key-and-fields ), [source ](../../../sql-reference/dictionaries/index.md#dictionary-sources ), [layout ](../../../sql-reference/dictionaries/index.md#storig-dictionaries-in-memory ) and [lifetime ](../../../sql-reference/dictionaries/index.md#dictionary-updates ).
2020-07-09 15:10:35 +00:00
2022-11-02 19:47:11 +00:00
## Syntax
2020-07-09 15:10:35 +00:00
``` sql
2021-12-30 22:43:54 +00:00
CREATE [OR REPLACE] DICTIONARY [IF NOT EXISTS] [db.]dictionary_name [ON CLUSTER cluster]
2020-07-09 15:10:35 +00:00
(
2021-10-06 20:33:36 +00:00
key1 type1 [DEFAULT|EXPRESSION expr1] [IS_OBJECT_ID],
2021-11-06 07:52:28 +00:00
key2 type2 [DEFAULT|EXPRESSION expr2],
2021-10-06 20:33:36 +00:00
attr1 type2 [DEFAULT|EXPRESSION expr3] [HIERARCHICAL|INJECTIVE],
attr2 type2 [DEFAULT|EXPRESSION expr4] [HIERARCHICAL|INJECTIVE]
2020-07-09 15:10:35 +00:00
)
PRIMARY KEY key1, key2
SOURCE(SOURCE_NAME([param1 value1 ... paramN valueN]))
LAYOUT(LAYOUT_NAME([param_name param_value]))
2020-11-14 13:45:42 +00:00
LIFETIME({MIN min_val MAX max_val | max_val})
2021-11-12 17:00:34 +00:00
SETTINGS(setting_name = setting_value, setting_name = setting_value, ...)
2021-11-04 16:08:42 +00:00
COMMENT 'Comment'
2020-07-09 15:10:35 +00:00
```
2022-11-02 19:47:11 +00:00
The dictionary structure consists of attributes. Dictionary attributes are specified similarly to table columns. The only required attribute property is its type, all other properties may have default values.
2020-07-09 15:10:35 +00:00
`ON CLUSTER` clause allows creating dictionary on a cluster, see [Distributed DDL ](../../../sql-reference/distributed-ddl.md ).
2023-03-18 02:45:43 +00:00
Depending on dictionary [layout ](../../../sql-reference/dictionaries/index.md#storig-dictionaries-in-memory ) one or more attributes can be specified as dictionary keys.
2020-07-09 15:10:35 +00:00
2022-11-02 20:58:07 +00:00
## SOURCE
2022-11-02 19:47:11 +00:00
The source for a dictionary can be a:
- table in the current ClickHouse service
- table in a remote ClickHouse service
- file available by HTTP(S)
- another database
2022-11-02 20:58:07 +00:00
### Create a dictionary from a table in the current ClickHouse service
2021-11-04 16:08:42 +00:00
Input table `source_table` :
``` text
┌─id─┬─value──┐
│ 1 │ First │
│ 2 │ Second │
└────┴────────┘
```
2021-11-06 07:52:28 +00:00
Creating the dictionary:
2021-11-04 16:08:42 +00:00
``` sql
2022-11-02 19:47:11 +00:00
CREATE DICTIONARY id_value_dictionary
2021-11-04 16:08:42 +00:00
(
id UInt64,
value String
)
PRIMARY KEY id
2022-11-02 19:47:11 +00:00
SOURCE(CLICKHOUSE(TABLE 'source_table'))
2021-11-04 16:08:42 +00:00
LAYOUT(FLAT())
LIFETIME(MIN 0 MAX 1000)
```
Output the dictionary:
``` sql
2022-11-02 19:47:11 +00:00
SHOW CREATE DICTIONARY id_value_dictionary;
2021-11-04 16:08:42 +00:00
```
2022-11-02 20:58:07 +00:00
```response
CREATE DICTIONARY default.id_value_dictionary
2021-11-04 16:08:42 +00:00
(
`id` UInt64,
`value` String
)
PRIMARY KEY id
2022-11-02 20:58:07 +00:00
SOURCE(CLICKHOUSE(TABLE 'source_table'))
2021-11-04 16:08:42 +00:00
LIFETIME(MIN 0 MAX 1000)
2022-11-02 20:58:07 +00:00
LAYOUT(FLAT())
```
2023-06-20 16:00:03 +00:00
:::note
2023-06-20 16:48:40 +00:00
When using the SQL console in [ClickHouse Cloud ](https://clickhouse.com ), you must specify a user (`default` or any other user with the role `default_role` ) and password when creating a dictionary.
2023-06-20 16:00:03 +00:00
:::note
```sql
2023-06-20 16:50:49 +00:00
CREATE USER IF NOT EXISTS clickhouse_admin
IDENTIFIED WITH sha256_password BY 'passworD43$x';
GRANT default_role TO clickhouse_admin;
2023-06-20 16:49:48 +00:00
CREATE DATABASE foo_db;
2023-06-20 16:00:03 +00:00
CREATE TABLE foo_db.source_table (
id UInt64,
value String
) ENGINE = MergeTree
PRIMARY KEY id;
CREATE DICTIONARY foo_db.id_value_dictionary
(
id UInt64,
value String
)
PRIMARY KEY id
2023-06-20 16:51:11 +00:00
SOURCE(CLICKHOUSE(TABLE 'source_table' USER 'clickhouse_admin' PASSWORD 'passworD43$x' DB 'foo_db' ))
2023-06-20 16:00:03 +00:00
LAYOUT(FLAT())
LIFETIME(MIN 0 MAX 1000);
```
2022-11-02 20:58:07 +00:00
### Create a dictionary from a table in a remote ClickHouse service
Input table (in the remote ClickHouse service) `source_table` :
``` text
┌─id─┬─value──┐
│ 1 │ First │
│ 2 │ Second │
└────┴────────┘
2021-11-04 16:08:42 +00:00
```
2022-11-02 20:58:07 +00:00
2022-11-02 19:47:11 +00:00
Creating the dictionary:
``` sql
CREATE DICTIONARY id_value_dictionary
(
id UInt64,
value String
)
PRIMARY KEY id
2022-11-02 20:58:07 +00:00
SOURCE(CLICKHOUSE(HOST 'HOSTNAME' PORT 9000 USER 'default' PASSWORD 'PASSWORD' TABLE 'source_table' DB 'default'))
2022-11-02 19:47:11 +00:00
LAYOUT(FLAT())
LIFETIME(MIN 0 MAX 1000)
```
2022-11-02 20:58:07 +00:00
### Create a dictionary from a file available by HTTP(S)
2022-11-02 19:47:11 +00:00
```sql
2023-01-30 09:56:17 +00:00
CREATE DICTIONARY default.taxi_zone_dictionary
2022-11-02 19:47:11 +00:00
(
`LocationID` UInt16 DEFAULT 0,
`Borough` String,
`Zone` String,
`service_zone` String
)
PRIMARY KEY LocationID
SOURCE(HTTP(URL 'https://datasets-documentation.s3.eu-west-3.amazonaws.com/nyc-taxi/taxi_zone_lookup.csv' FORMAT 'CSVWithNames'))
LIFETIME(MIN 0 MAX 0)
LAYOUT(HASHED())
```
2022-11-02 20:58:07 +00:00
### Create a dictionary from another database
2022-11-02 19:47:11 +00:00
2023-03-18 02:45:43 +00:00
Please see the details in [Dictionary sources ](/docs/en/sql-reference/dictionaries/index.md#dictionary-sources/#dbms ).
2021-11-04 16:08:42 +00:00
**See Also**
2023-03-18 02:45:43 +00:00
- For more information, see the [Dictionaries ](../../../sql-reference/dictionaries/index.md ) section.
- [system.dictionaries ](../../../operations/system-tables/dictionaries.md ) — This table contains information about [Dictionaries ](../../../sql-reference/dictionaries/index.md ).