如何在H2数据库中插入特定的UUID? [英] How to insert a specific UUID in h2 database?

查看:412
本文介绍了如何在H2数据库中插入特定的UUID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在数据库中插入一些默认数据.我正在将Spring Boot与Flyway集成一起使用.为了进行测试,我使用H2.在生产中将使用MySQL.

I need to insert some default data in my database. I am using Spring Boot with the Flyway integration. For testing, I use H2. For production MySQL will be used.

我制作了单独的Flyway迁移脚本,因此我可以使用数据库特定的东西作为默认数据(创建表是在通用脚本中完成的).

I have made separate Flyway migration scripts so I can use database specific stuff for the default data (Creating the tables is done in a common script).

对于MySQL,我有这样的东西:

For MySQL, I have something like this:

INSERT INTO survey_definition (id, name, period) 
VALUES (0x2D1EBC5B7D2741979CF0E84451C5BBB1, 'disease-activity', 'P1M');

我该如何对H2做同样的事情?

How can I do the same for H2?

我只找到了有效的RANDOM_UUID()函数,但是我需要使用一个已知的UUID,因为我将其用作其他语句中的外键.

I only found the RANDOM_UUID() function, which works, but I need use a known UUID because I am using it as foreign keys in further statements.

推荐答案

最好使用适用于所有数据库的语法.我认为大多数数据库不支持0x语法.对于H2,这将起作用:

Best if you use a syntax that works for all databases. I think most databases don't support the 0x syntax. For H2, this would work:

INSERT INTO survey_definition (id, name, period) 
VALUES ('2D1EBC5B7D2741979CF0E84451C5BBB1', 'disease-activity', 'P1M');

但是要获得跨数据库语法,您可能需要创建一个用户定义的函数(例如uuid),然后使用:

But to get a cross-database syntax, you may need to create a user defined function (for example uuid) and then use:

INSERT INTO survey_definition (id, name, period) 
VALUES (uuid('2D1EBC5B7D2741979CF0E84451C5BBB1'), 'disease-activity', 'P1M');

这篇关于如何在H2数据库中插入特定的UUID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆