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.
Asked
Active
Viewed 1.8k times
10
-
3There are no foreign keys in Clickhouse. It is not a traditional relational database. – Tigran Saluev Oct 16 '18 at 04:52
2 Answers
9
There is no server-provided auto-increment in ClickHouse.
As stated by other answers, UUID's is the way to go.
Instead, use generateUUIDv4() (see documentation here)
Sample output
SELECT generateUUIDv4();
Use during insert
INSERT INTO t VALUES (generateUUIDv4(), ...);

Silas Hansen
- 1,669
- 2
- 17
- 23
7
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()), ...);

simPod
- 11,498
- 17
- 86
- 139
-
it is not working. Error gives: DB::Exception: Element of set in IN or VALUES is not a constant expression: toUUID – Sakezzz Jan 08 '21 at 08:32
-
-
-
Tested in latest version to Oct 2018 (not sure what it was) and in v20.12 now – simPod Jan 08 '21 at 13:03