是否可以在Insert语句中自动生成GUID?
此外,我应该使用哪种类型的字段来存储此GUID?
答案 0 :(得分:124)
您可以使用SYS_GUID()函数在insert语句中生成GUID:
insert into mytable (guid_col, data) values (sys_guid(), 'xxx');
存储GUID的首选数据类型是RAW(16)。
正如Gopinath的回答:
select sys_guid() from dual
union all
select sys_guid() from dual
union all
select sys_guid() from dual
你得到了
88FDC68C75DDF955E040449808B55601
88FDC68C75DEF955E040449808B55601
88FDC68C75DFF955E040449808B55601
正如Tony Andrews所说, 仅在一个角色上有所不同
88FDC68C75D的 d 强> F955E040449808B55601
88FDC68C75D <强>电子强> F955E040449808B55601
88FDC68C75D的˚F强> F955E040449808B55601
也许有用:http://feuerthoughts.blogspot.com/2006/02/watch-out-for-sequential-oracle-guids.html
答案 1 :(得分:26)
您还可以将guid包含在表的create语句中作为默认值,例如:
create table t_sysguid
( id raw(16) default sys_guid() primary key
, filler varchar2(1000)
)
/
答案 2 :(得分:7)
通过自动生成一个插入语句的guid并不清楚你的意思但猜测,我认为你正在尝试做类似以下的事情:
INSERT INTO MY_TAB (ID, NAME) VALUES (SYS_GUID(), 'Adams');
INSERT INTO MY_TAB (ID, NAME) VALUES (SYS_GUID(), 'Baker');
在这种情况下,我认为ID列应该声明为RAW(16);
我正在做这件事。我没有方便测试的Oracle实例,但我认为这就是你想要的。
答案 3 :(得分:3)
sys_guid()是一个糟糕的选择。生成UUID并避免顺序值的一种方法是自己生成随机十六进制字符串:
select regexp_replace(
to_char(
DBMS_RANDOM.value(0, power(2, 128)-1),
'FM0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'),
'([a-f0-9]{8})([a-f0-9]{4})([a-f0-9]{4})([a-f0-9]{4})([a-f0-9]{12})',
'\1-\2-\3-\4-\5') from DUAL;
答案 4 :(得分:2)
您可以运行以下查询
select sys_guid() from dual
union all
select sys_guid() from dual
union all
select sys_guid() from dual
答案 5 :(得分:2)
您可以使用下面的函数来生成您的UUID
create or replace FUNCTION RANDOM_GUID
RETURN VARCHAR2 IS
RNG NUMBER;
N BINARY_INTEGER;
CCS VARCHAR2 (128);
XSTR VARCHAR2 (4000) := NULL;
BEGIN
CCS := '0123456789' || 'ABCDEF';
RNG := 15;
FOR I IN 1 .. 32 LOOP
N := TRUNC (RNG * DBMS_RANDOM.VALUE) + 1;
XSTR := XSTR || SUBSTR (CCS, N, 1);
END LOOP;
RETURN SUBSTR(XSTR, 1, 4) || '-' ||
SUBSTR(XSTR, 5, 4) || '-' ||
SUBSTR(XSTR, 9, 4) || '-' ||
SUBSTR(XSTR, 13,4) || '-' ||
SUBSTR(XSTR, 17,4) || '-' ||
SUBSTR(XSTR, 21,4) || '-' ||
SUBSTR(XSTR, 24,4) || '-' ||
SUBSTR(XSTR, 28,4);
END RANDOM_GUID;
通过上述功能生成的GUID示例:
的 8EA4-196D-BC48-9793-8AE8-5500-03DC-9D04 强>
答案 6 :(得分:2)
示例: http://www.orafaq.com/usenet/comp.databases.oracle.server/2006/12/20/0646.htm
SELECT REGEXP_REPLACE(SYS_GUID(), '(.{8})(.{4})(.{4})(.{4})(.{12})', '\1-\2-\3-\4-\5') MSSQL_GUID FROM DUAL
结果:
6C7C9A50-3514-4E77-E053-B30210AC1082
答案 7 :(得分:1)
如果需要非顺序引导,则可以通过哈希函数发送sys_guid()
结果(请参见https://stackoverflow.com/a/22534843/1462295)。这个想法是保留原始创作中使用的任何唯一性,并获得一些经过改组的位。
例如:
LOWER(SUBSTR(STANDARD_HASH(SYS_GUID(), 'SHA1'), 0, 32))
示例显示默认顺序guid与通过哈希将其发送:
SELECT LOWER(SYS_GUID()) AS OGUID FROM DUAL
UNION ALL
SELECT LOWER(SYS_GUID()) AS OGUID FROM DUAL
UNION ALL
SELECT LOWER(SYS_GUID()) AS OGUID FROM DUAL
UNION ALL
SELECT LOWER(SYS_GUID()) AS OGUID FROM DUAL
UNION ALL
SELECT LOWER(SUBSTR(STANDARD_HASH(SYS_GUID(), 'SHA1'), 0, 32)) AS OGUID FROM DUAL
UNION ALL
SELECT LOWER(SUBSTR(STANDARD_HASH(SYS_GUID(), 'SHA1'), 0, 32)) AS OGUID FROM DUAL
UNION ALL
SELECT LOWER(SUBSTR(STANDARD_HASH(SYS_GUID(), 'SHA1'), 0, 32)) AS OGUID FROM DUAL
UNION ALL
SELECT LOWER(SUBSTR(STANDARD_HASH(SYS_GUID(), 'SHA1'), 0, 32)) AS OGUID FROM DUAL
输出
80c32a4fbe405707e0531e18980a1bbb
80c32a4fbe415707e0531e18980a1bbb
80c32a4fbe425707e0531e18980a1bbb
80c32a4fbe435707e0531e18980a1bbb
c0f2ff2d3ef7b422c302bd87a4588490
d1886a8f3b4c547c28b0805d70b384f3
a0c565f3008622dde3148cfce9353ba7
1c375f3311faab15dc6a7503ce08182c
答案 8 :(得分:0)
我建议使用Oracle的“ dbms_crypto.randombytes”功能。
select REGEXP_REPLACE(dbms_crypto.randombytes(16), '(.{8})(.{4})(.{4})(.{4})(.{12})', '\1-\2-\3-\4-\5') from dual;
如果仅更改一个字符,则不要使用“ sys_guid”功能。
ALTER TABLE locations ADD (uid_col RAW(16));
UPDATE locations SET uid_col = SYS_GUID();
SELECT location_id, uid_col FROM locations
ORDER BY location_id, uid_col;
LOCATION_ID UID_COL
----------- ----------------------------------------------------------------
1000 09F686761827CF8AE040578CB20B7491
1100 09F686761828CF8AE040578CB20B7491
1200 09F686761829CF8AE040578CB20B7491
1300 09F68676182ACF8AE040578CB20B7491
1400 09F68676182BCF8AE040578CB20B7491
1500 09F68676182CCF8AE040578CB20B7491
https://docs.oracle.com/database/121/SQLRF/functions202.htm#SQLRF06120
答案 9 :(得分:0)
创建一个350个字符的GUID:
dbms_random.STRING('a',350)-返回混合大小写字母字符的字符串
dbms_random.STRING('x',350)-返回大写字母数字字符的字符串