How to generate a GUID in Oracle?

匿名 (未验证) 提交于 2019-12-03 01:58:03

问题:

Is it possible to auto-generate a GUID into an Insert statement?

Also, what type of field should I use to store this GUID?

回答1:

You can use the SYS_GUID() function to generate a GUID in your insert statement:

insert into mytable (guid_col, data) values (sys_guid(), 'xxx'); 

The preferred datatype for storing GUIDs is RAW(16).

As Gopinath answer:

 select sys_guid() from dual  union all  select sys_guid() from dual  union all   select sys_guid() from dual 

You get

88FDC68C75DDF955E040449808B55601
88FDC68C75DEF955E040449808B55601
88FDC68C75DFF955E040449808B55601

As Tony Andrews says, differs only at one character

88FDC68C75DDF955E040449808B55601
88FDC68C75DEF955E040449808B55601
88FDC68C75DFF955E040449808B55601

Maybe useful: http://feuerthoughts.blogspot.com/2006/02/watch-out-for-sequential-oracle-guids.html



回答2:

You can also include the guid in the create statement of the table as default, for example:

create table t_sysguid ( id     raw(16) default sys_guid() primary key , filler varchar2(1000) ) / 

See here: http://rwijk.blogspot.com/2009/12/sysguid.html



回答3:

It is not clear what you mean by auto-generate a guid into an insert statement but at a guess, I think you are trying to do something like the following:

INSERT INTO MY_TAB (ID, NAME) VALUES (SYS_GUID(), 'Adams'); INSERT INTO MY_TAB (ID, NAME) VALUES (SYS_GUID(), 'Baker'); 

In that case I believe the ID column should be declared as RAW(16);

I am doing this off the top of my head. I don't have an Oracle instance handy to test against, but I think that is what you want.



回答4:

You can run the following query

 select sys_guid() from dual  union all  select sys_guid() from dual  union all   select sys_guid() from dual 


回答5:

sys_guid() is a poor option, as other answers have mentioned. One way to generate UUIDs and avoid sequential values is to generate random hex strings yourself:

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; 


回答6:

you can use function bellow in order to generate your 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; 

Example of GUID genedrated by the function above:
8EA4-196D-BC48-9793-8AE8-5500-03DC-9D04



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!