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

> Documentation for creating and configuring dictionaries

# CREATE DICTIONARY

export const CloudSupportedBadge = () => <div className="cloudBadge">
    <div className="cloudIcon">
      <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path fillRule="evenodd" clipRule="evenodd" d="M5.33395 12.6667H12.3739C13.6593 12.6667 14.7073 11.6187 14.7073 10.3334C14.7073 9.04804 13.6593 8.00004 12.3739 8.00004H12.0839V7.33337C12.0839 5.12671 10.2906 3.33337 8.08395 3.33337C6.09928 3.33337 4.45395 4.78537 4.14195 6.68204C2.55728 6.76271 1.29395 8.06204 1.29395 9.66671C1.29395 11.3234 2.63728 12.6667 4.29395 12.6667H5.33395Z" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" />
      </svg>
    </div>
    {"Supported in ClickHouse Cloud"}
  </div>;

export const CloudNotSupportedBadge = () => {
  return <div className="cloudNotSupportedBadge">
            <div className="cloudNotSupportedIcon">
            <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path strokeWidth="1.5" d="M6.33366 12.6666L12.3739 12.6667C13.6593 12.6667 14.7073 11.6187 14.7073 10.3334C14.7073 9.04804 13.6593 8.00003 12.3739 8.00003C12.3739 8.00003 12.3337 7.66659 12.0003 7.33325M10.667 5.33322C8.00033 2.33325 4.45395 4.78537 4.14195 6.68203C2.55728 6.7627 1.29395 8.06203 1.29395 9.6667C1.29395 11.3234 2.66699 12.6666 4.00033 12.6666" stroke="currentColor" strokeLinecap="round" strokeLinejoin="round" />
                <path strokeWidth="1.5" d="M2.66699 14L12.0003 4.66663" stroke="currentColor" strokeLinecap="round" strokeLinejoin="round" />
            </svg>

        </div>
            Not supported in ClickHouse Cloud
        </div>;
};

A dictionary is a mapping (`key -> attributes`) that is convenient for various types of reference lists.
ClickHouse supports special functions for working with dictionaries that can be used in queries. It is easier and more efficient to use dictionaries with functions than a `JOIN` with reference tables.

Dictionaries can be created in two ways:

* [With a DDL query](#creating-a-dictionary-with-a-ddl-query) (recommended)
* [With a configuration file](#creating-a-dictionary-with-a-configuration-file)

<h2 id="creating-a-dictionary-with-a-ddl-query">
  Creating a dictionary with a DDL query
</h2>

<CloudSupportedBadge />

Dictionaries can be created with DDL queries.
This is the recommended method because with DDL created dictionaries:

* No additional records are added to server configuration files.
* Dictionaries can be used like first-class entities such as tables or views.
* Data can be read directly, using familiar `SELECT` syntax rather than dictionary table functions. Note that when accessing a dictionary directly via a `SELECT` statement, cached dictionary will return only cached data, while for a non-cached dictionary it will return all the data that it stores.
* Dictionaries can be easily renamed.

<h3 id="syntax">
  Syntax
</h3>

```sql theme={null}
CREATE [OR REPLACE] DICTIONARY [IF NOT EXISTS] [db.]dictionary_name [ON CLUSTER cluster]
(
    key1  type1  [DEFAULT | EXPRESSION expr1] [IS_OBJECT_ID],
    key2  type2  [DEFAULT | EXPRESSION expr2],
    attr1 type2  [DEFAULT | EXPRESSION expr3] [HIERARCHICAL|INJECTIVE],
    attr2 type2  [DEFAULT | EXPRESSION expr4] [HIERARCHICAL|INJECTIVE]
)
PRIMARY KEY key1, key2
SOURCE(SOURCE_NAME([param1 value1 ... paramN valueN]))
LAYOUT(LAYOUT_NAME([param_name param_value]))
LIFETIME({MIN min_val MAX max_val | max_val})
SETTINGS(setting_name = setting_value, setting_name = setting_value, ...)
COMMENT 'Comment'
```

| Clause                                                               | Description                                                                                                                                 |
| -------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
| [Attributes](/reference/statements/create/dictionary/attributes)     | Dictionary attributes are specified similarly to table columns. The only required property is the type, all others may have default values. |
| PRIMARY KEY                                                          | Defines the key column(s) for dictionary lookups. Depending on the layout, one or more attributes can be specified as keys.                 |
| [`SOURCE`](/reference/statements/create/dictionary/sources/overview) | Defines the data source for the dictionary (e.g. ClickHouse table, HTTP, PostgreSQL).                                                       |
| [`LAYOUT`](/reference/statements/create/dictionary/layouts/overview) | Controls how the dictionary is stored in memory (e.g. `FLAT`, `HASHED`, `CACHE`).                                                           |
| [`LIFETIME`](/reference/statements/create/dictionary/lifetime)       | Sets the refresh interval for the dictionary.                                                                                               |
| [`ON CLUSTER`](/reference/statements/distributed-ddl)                | Creates the dictionary on a cluster. Optional.                                                                                              |
| `SETTINGS`                                                           | Additional dictionary settings. Optional.                                                                                                   |
| `COMMENT`                                                            | Adds a text comment to the dictionary. Optional.                                                                                            |

<h2 id="creating-a-dictionary-with-a-configuration-file">
  Creating a dictionary with a configuration file
</h2>

<Note>
  Creating a dictionary with a configuration file is not applicable to ClickHouse Cloud. Please use DDL (see above), and create your dictionary as the `default` user.
</Note>

The dictionary configuration file has the following format:

```xml theme={null}
<clickhouse>
    <comment>An optional element with any content. Ignored by the ClickHouse server.</comment>

    <!--Optional element. File name with substitutions-->
    <include_from>/etc/metrika.xml</include_from>

    <dictionary>
        <!-- Dictionary configuration. -->
        <!-- There can be any number of dictionary sections in a configuration file. -->
    </dictionary>

</clickhouse>
```

You can configure any number of dictionaries in the same file.

<h2 id="related-content">
  Related content
</h2>

* [Layouts](/reference/statements/create/dictionary/layouts/overview) — How dictionaries are stored in memory
* [Sources](/reference/statements/create/dictionary/sources/overview) — Connecting to data sources
* [Lifetime](/reference/statements/create/dictionary/lifetime) — Automatic refresh configuration
* [Attributes](/reference/statements/create/dictionary/attributes) — Key and attribute configuration
* [Embedded Dictionaries](/reference/statements/create/dictionary/embedded) — Built-in geobase dictionaries
* [system.dictionaries](/reference/system-tables/dictionaries) — System table with dictionary information
