是否可以将GUID自动生成为Insert语句?
另外,应该使用哪种类型的字段来存储此GUID?
Answers:
您可以使用SYS_GUID()函数在您的插入语句中生成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 E F955E040449808B55601
88FDC68C75D F F955E040449808B55601
可能有用:http: //feuerthoughts.blogspot.com/2006/02/watch-out-for-sequential-oracle-guids.html
您还可以将guid默认包含在表的create语句中,例如:
create table t_sysguid
( id raw(16) default sys_guid() primary key
, filler varchar2(1000)
)
/
尚不清楚将guid自动生成为insert语句的含义,但据推测,我认为您正在尝试执行以下操作:
INSERT INTO MY_TAB (ID, NAME) VALUES (SYS_GUID(), 'Adams');
INSERT INTO MY_TAB (ID, NAME) VALUES (SYS_GUID(), 'Baker');
在那种情况下,我认为ID列应声明为RAW(16);
我正在这样做。我没有要测试的Oracle实例,但是我想这就是您想要的。
可以在以下示例中找到示例: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
如其他答案所述,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;
您可以使用功能波纹管来生成您的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
如果需要非顺序引导,则可以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
我建议使用Oracle的“ dbms_crypto.randombytes”函数。
为什么? 此函数返回一个RAW值,其中包含一个加密安全的伪随机字节序列,该序列可用于生成加密密钥的随机材料。
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