2020-04-03 13:23:32 +00:00
---
2022-08-28 14:53:34 +00:00
slug: /en/engines/table-engines/integrations/odbc
2022-04-09 13:29:05 +00:00
sidebar_position: 2
sidebar_label: ODBC
2020-04-03 13:23:32 +00:00
---
2022-06-02 10:55:18 +00:00
# ODBC
2019-05-15 10:44:55 +00:00
Allows ClickHouse to connect to external databases via [ODBC ](https://en.wikipedia.org/wiki/Open_Database_Connectivity ).
2019-06-17 09:06:58 +00:00
To safely implement ODBC connections, ClickHouse uses a separate program `clickhouse-odbc-bridge` . If the ODBC driver is loaded directly from `clickhouse-server` , driver problems can crash the ClickHouse server. ClickHouse automatically starts `clickhouse-odbc-bridge` when it is required. The ODBC bridge program is installed from the same package as the `clickhouse-server` .
2019-05-15 10:44:55 +00:00
2020-04-30 18:19:18 +00:00
This engine supports the [Nullable ](../../../sql-reference/data-types/nullable.md ) data type.
2019-05-15 10:44:55 +00:00
2020-03-20 10:10:48 +00:00
## Creating a Table {#creating-a-table}
2019-05-15 10:44:55 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2019-05-20 09:50:37 +00:00
CREATE TABLE [IF NOT EXISTS] [db.]table_name [ON CLUSTER cluster]
(
2019-05-25 13:21:17 +00:00
name1 [type1],
name2 [type2],
2019-05-20 09:50:37 +00:00
...
)
ENGINE = ODBC(connection_settings, external_database, external_table)
2019-05-15 10:44:55 +00:00
```
2020-07-09 15:10:35 +00:00
See a detailed description of the [CREATE TABLE ](../../../sql-reference/statements/create/table.md#create-table-query ) query.
2019-05-20 09:50:37 +00:00
2019-06-17 09:06:58 +00:00
The table structure can differ from the source table structure:
2019-05-20 09:50:37 +00:00
2023-04-19 15:55:29 +00:00
- Column names should be the same as in the source table, but you can use just some of these columns and in any order.
- Column types may differ from those in the source table. ClickHouse tries to [cast ](../../../sql-reference/functions/type-conversion-functions.md#type_conversion_function-cast ) values to the ClickHouse data types.
- The [external_table_functions_use_nulls ](../../../operations/settings/settings.md#external-table-functions-use-nulls ) setting defines how to handle Nullable columns. Default value: 1. If 0, the table function does not make Nullable columns and inserts default values instead of nulls. This is also applicable for NULL values inside arrays.
2019-05-20 09:50:37 +00:00
2019-05-15 10:44:55 +00:00
**Engine Parameters**
2023-04-19 15:55:29 +00:00
- `connection_settings` — Name of the section with connection settings in the `odbc.ini` file.
- `external_database` — Name of a database in an external DBMS.
- `external_table` — Name of a table in the `external_database` .
2019-05-15 10:44:55 +00:00
2020-03-20 10:10:48 +00:00
## Usage Example {#usage-example}
2019-05-15 10:44:55 +00:00
2019-06-17 09:06:58 +00:00
**Retrieving data from the local MySQL installation via ODBC**
2019-05-20 09:50:37 +00:00
2019-06-17 09:06:58 +00:00
This example is checked for Ubuntu Linux 18.04 and MySQL server 5.7.
2019-05-20 09:50:37 +00:00
2019-06-17 09:06:58 +00:00
Ensure that unixODBC and MySQL Connector are installed.
2019-05-20 09:50:37 +00:00
2019-06-17 09:06:58 +00:00
By default (if installed from packages), ClickHouse starts as user `clickhouse` . Thus, you need to create and configure this user in the MySQL server.
2019-05-20 09:50:37 +00:00
2020-03-20 10:10:48 +00:00
``` bash
2019-09-23 15:31:46 +00:00
$ sudo mysql
2019-05-20 09:50:37 +00:00
```
2020-03-20 10:10:48 +00:00
``` sql
2019-05-20 09:50:37 +00:00
mysql> CREATE USER 'clickhouse'@'localhost' IDENTIFIED BY 'clickhouse';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'clickhouse'@'clickhouse' WITH GRANT OPTION;
```
Then configure the connection in `/etc/odbc.ini` .
2020-03-20 10:10:48 +00:00
``` bash
2019-05-20 09:50:37 +00:00
$ cat /etc/odbc.ini
[mysqlconn]
DRIVER = /usr/local/lib/libmyodbc5w.so
SERVER = 127.0.0.1
PORT = 3306
DATABASE = test
USERNAME = clickhouse
PASSWORD = clickhouse
```
2019-06-17 09:06:58 +00:00
You can check the connection using the `isql` utility from the unixODBC installation.
2019-05-20 09:50:37 +00:00
2020-03-20 10:10:48 +00:00
``` bash
2019-09-23 15:31:46 +00:00
$ isql -v mysqlconn
2020-04-03 13:23:32 +00:00
+-------------------------+
2019-05-20 09:50:37 +00:00
| Connected! |
| |
...
```
Table in MySQL:
2020-03-20 10:10:48 +00:00
``` text
2019-05-20 09:50:37 +00:00
mysql> CREATE TABLE `test` .`test` (
-> `int_id` INT NOT NULL AUTO_INCREMENT,
-> `int_nullable` INT NULL DEFAULT NULL,
-> `float` FLOAT NOT NULL,
-> `float_nullable` FLOAT NULL DEFAULT NULL,
-> PRIMARY KEY (`int_id`));
Query OK, 0 rows affected (0,09 sec)
mysql> insert into test (`int_id`, `float` ) VALUES (1,2);
Query OK, 1 row affected (0,00 sec)
mysql> select * from test;
2020-04-03 13:23:32 +00:00
+------+----------+-----+----------+
2019-05-20 09:50:37 +00:00
| int_id | int_nullable | float | float_nullable |
2020-04-03 13:23:32 +00:00
+------+----------+-----+----------+
2019-05-20 09:50:37 +00:00
| 1 | NULL | 2 | NULL |
2020-04-03 13:23:32 +00:00
+------+----------+-----+----------+
2019-05-20 09:50:37 +00:00
1 row in set (0,00 sec)
```
2019-06-17 09:06:58 +00:00
Table in ClickHouse, retrieving data from the MySQL table:
2019-05-20 09:50:37 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2019-05-20 09:50:37 +00:00
CREATE TABLE odbc_t
(
`int_id` Int32,
`float_nullable` Nullable(Float32)
)
ENGINE = ODBC('DSN=mysqlconn', 'test', 'test')
```
2020-03-20 10:10:48 +00:00
``` sql
2019-05-20 09:50:37 +00:00
SELECT * FROM odbc_t
```
2020-03-20 10:10:48 +00:00
``` text
2019-05-20 09:50:37 +00:00
┌─int_id─┬─float_nullable─┐
│ 1 │ ᴺᵁᴸᴸ │
└────────┴────────────────┘
```
2020-03-20 10:10:48 +00:00
## See Also {#see-also}
2019-05-15 10:44:55 +00:00
2023-04-19 15:55:29 +00:00
- [ODBC dictionaries ](../../../sql-reference/dictionaries/index.md#dictionary-sources#dicts-external_dicts_dict_sources-odbc )
- [ODBC table function ](../../../sql-reference/table-functions/odbc.md )