Skip to Main Content

Oracle Database Discussions

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

GUIDs, UUIDs, SYS_GUID() and the Oracle database

athompson88May 28 2015 — edited May 28 2015

I work in a .Net shop, but our Oracle databases run on Linux. GUIDs are generated in the application most of time and stored in the database as RAW (functions are used to change endianness as needed). On occasion, some loader scripts use the SYS_GUID() function, but while this generates a unique ID (we're on 11.2.0.4, so the duplicate key bug should be handled), it's still sequence based. This bothers the developers and software architects to no end because it makes it difficult to visually scan through rows to identify if queries are producing duplicate records. In any case, I found a post on stackoverflow which might offer a good alternative.

Method #1: Java call

Wrap the following Java call inside a function:

java.util.UUID.randomUUID().toString();


Method #2: PL/SQL call

create or replace function random_uuid return RAW is
  v_uuid RAW
(16);
begin
  v_uuid
:= sys.dbms_crypto.randombytes(16);
 
return (utl_raw.overlay(utl_raw.bit_or(utl_raw.bit_and(utl_raw.substr(v_uuid, 7, 1), '0F'), '40'), v_uuid, 7));
end random_uuid;


Source: http://stackoverflow.com/questions/13951576/how-to-generate-reliably-a-random-uuid-on-oracle


Has anyone ever used either of these approaches to generate GUIDs/UUIDs? Also, I'm not sure what's going on with the utl_raw calls in the pl/sql approach and why post-processing is necessary of the randombytes result.

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 25 2015
Added on May 28 2015
0 comments
3,448 views