Generate UUID values by default for each row on co

2019-01-19 22:34发布

In the H2 database, on a table with a column of UUID data type, how do we specify that we want H2 to generate a UUID value by default when an INSERT omits that field?

I know how to generate a UUID. I have read the Question, How to insert a specific UUID in h2 database?.

My question is about how to ask H2 to generate the UUID value on my behalf.

标签: default h2 uuid
1条回答
\"骚年 ilove
2楼-- · 2019-01-19 23:20

You can use built-in function RANDOM_UUID():

create table test(id int primary key, data uuid default random_uuid());
insert into test(id) values(1);
select * from test;

Note that using the UUID (or any other randomly generated data) as the primary key will result in performance problems if there are more than a few million rows (with relational databases in general, not just with H2).

查看更多
登录 后发表回答