Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have auto increment in ClickHouse?

Tags:

sql

clickhouse

I want a column to have a unique value in every insertion. In SQL we can have this using autoincrement, in Clickhouse can we have this functionality using any type like auto increment or any other? I am new to Clickhouse so there may be a terminology mistake.

like image 1000
Ankit Juneja Avatar asked Oct 16 '18 04:10

Ankit Juneja


People also ask

Is UUID incremental?

UUID always occupies 16 bytes. For Auto Increment Integer, when stored as Long format, it occupies 8 bytes. If the table itself has only a few columns, the extra primary key space overhead will become more significant.

What is auto incremental?

Auto-increment allows a unique number to be generated automatically when a new record is inserted into a table. Often this is the primary key field that we would like to be created automatically every time a new record is inserted.

How do I add a primary key in ClickHouse?

ClickHouse does not require a unique primary key. You can insert multiple rows with the same primary key. You can use Nullable -typed expressions in the PRIMARY KEY and ORDER BY clauses but it is strongly discouraged. To allow this feature, turn on the allow_nullable_key setting.

How do I create a temporary table in ClickHouse?

From SELECT query​CREATE TABLE [IF NOT EXISTS] [db.] table_name[(name1 [type1], name2 [type2], ...)] ENGINE = engine AS SELECT ... Creates a table with a structure like the result of the SELECT query, with the engine engine, and fills it with data from SELECT .


1 Answers

There's nothing like auto increment in ClickHouse.

If you need unique value, use UUID. It works much better for distributed systems than just auto incremented value

So you can simply generate random Uint64 and convert it to UUID

SELECT toUUID(rand64());

With insert it would look similar to this

INSERT INTO t VALUES (toUUID(rand64()), ...);
like image 106
simPod Avatar answered Sep 23 '22 14:09

simPod