112

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

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

Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
Acibi
  • 1,737
  • 3
  • 16
  • 22
  • Does this answer your question? [How to generate a version 4 (random) UUID on Oracle?](https://stackoverflow.com/questions/13951576/how-to-generate-a-version-4-random-uuid-on-oracle) – marciel.deg Jul 10 '23 at 20:10

10 Answers10

161

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

Kiquenet
  • 14,494
  • 35
  • 148
  • 243
Tony Andrews
  • 129,880
  • 21
  • 220
  • 259
  • Weird, the sys_guid() is always giving me the same GUID.. Do I need to give a seed to the function or? – Acibi Jun 14 '10 at 13:45
  • 12
    Are you sure they are exactly the same? It does tend to return very similar (but different) values - e.g. when I just tried I got 88FDC68C75DEF955E040449808B55601 and 88FDC68C75DFF955E040449808B55601, which differ only at the 12th character! – Tony Andrews Jun 14 '10 at 13:54
  • Try select sys_guid() from dual and compare values. Modify answer about it. – Kiquenet Oct 15 '14 at 10:37
  • @TonyAndrews In MySQL I use `UUID_TO_BIN(uuid(),true)` to generate an **ordered uuid** which seems to be recommended. I wonder if there is an equivalent for generating an **ordered uuid** in Oracle SQL or maybe using `sys_guid()` as `RAW(16)` is enough? – Edgar Magallon Jul 16 '23 at 21:44
  • 1
    @EdgarMagallon as far as I know there is no ordered GUID concept in Oracle. – Tony Andrews Jul 17 '23 at 08:06
  • @TonyAndrews oh, thanks! I will try to test it, I think using `sys_guid` is enough – Edgar Magallon Jul 17 '23 at 18:32
28

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

TTT
  • 2,365
  • 17
  • 16
18

Example found on: 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 

Result:

6C7C9A50-3514-4E77-E053-B30210AC1082 
Zartch
  • 945
  • 10
  • 25
  • 3
    Wish I could buy you a drink for this. +1 – Baodad Jan 14 '20 at 23:53
  • 1
    Fantastic! Thank you :) – ZooZ Aug 12 '21 at 11:19
  • My aim was to generate non-predictable values, but "SYS_GUID" is no good. Here are 3 values I generated in a row: - 0305B54BBD617444E06332030A0A5C6F - 0305B54BBD627444E06332030A0A5C6F - 0305B54BBD637444E06332030A0A5C6F They are very similar. Other methods need to be used (at least for my use-case) – wormsparty Aug 16 '23 at 07:13
9

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.

Kenneth Baltrinic
  • 2,941
  • 2
  • 28
  • 45
8

If you need non-sequential guids you can send the sys_guid() results through a hashing function (see https://stackoverflow.com/a/22534843/1462295 ). The idea is to keep whatever uniqueness is used from the original creation, and get something with more shuffled bits.

For instance:

LOWER(SUBSTR(STANDARD_HASH(SYS_GUID(), 'SHA1'), 0, 32))  

Example showing default sequential guid vs sending it through a hash:

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  

output

80c32a4fbe405707e0531e18980a1bbb
80c32a4fbe415707e0531e18980a1bbb
80c32a4fbe425707e0531e18980a1bbb
80c32a4fbe435707e0531e18980a1bbb
c0f2ff2d3ef7b422c302bd87a4588490
d1886a8f3b4c547c28b0805d70b384f3
a0c565f3008622dde3148cfce9353ba7
1c375f3311faab15dc6a7503ce08182c
BurnsBA
  • 4,347
  • 27
  • 39
7

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;
MikeWyatt
  • 7,842
  • 10
  • 50
  • 71
  • 5
    Those are not guaranteed to be unique. You'd need to pair it with something like a unique constraint on wherever you're storing it, if you're storing it. (and generating a new number if you encounter a repeated value and violate the unique constraint, however unlikely that may be) – James Daily Jan 05 '18 at 01:09
3

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
3

I would recommend using Oracle's "dbms_crypto.randombytes" function.

Why?
This function returns a RAW value containing a cryptographically secure pseudo-random sequence of bytes, which can be used to generate random material for encryption keys.

select REGEXP_REPLACE(dbms_crypto.randombytes(16), '(.{8})(.{4})(.{4})(.{4})(.{12})', '\1-\2-\3-\4-\5') from dual;

You should not use the function "sys_guid" because only one character changes.

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

Alex Fischer
  • 193
  • 1
  • 2
  • 12
2

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

BERGUIGA Mohamed Amine
  • 6,094
  • 3
  • 40
  • 38
1

Creating a 350 characters GUID:

dbms_random.STRING ('a', 350) - returning string in mixed case alpha characters

dbms_random.STRING ('x', 350) - returning string in uppercase alpha-numeric characters

Oranit Dar
  • 1,539
  • 18
  • 17