GUIDs can be probabilistically unique without using a registration authority because the algorithm which generates them uses some data that is pretty much guaranteed to be unique.
- GUIDs encode a timestamp of the time at which they're created.
- They also include the address of the network card of the computer that generated them, which is itself unique.
- The above components (timestamp and network card address) should typically be good enough to ensure uniqueness, however there are safeguards in the algorithm to make sure that, if two GUIDs are generated on the same machine at the same time, they will have another unique part (a large random number).
While it is possible for someone to create a GUID that is not unique, the algorithm generating the value would have to do it by not following the rules. GUIDs are so large (128 bits) that it's virtually impossible to randomly create one that matches an existing one.
The following code examples demonstrate generating unique identifiers using this function in several contexts:
1. GENERATE-UUID function returns a unique value. The GENERATE-UUID returns a universally unique 16-byte raw value as demonstrated by the following code. RAW fields cannot be indexed in database tables:
DEFINE TEMP-TABLE test
FIELD rawField AS RAW
INDEX lcidx IS PRIMARY UNIQUE rawField ASCENDING.
DEFINE VARIABLE iVar AS INTEGER NO-UNDO.
DO iVar = 1 TO 1000000 ON ERROR UNDO, LEAVE:
CREATE test.
ASSIGN
rawField = GENERATE-UUID.
END.
MESSAGE iVar
VIEW-AS ALERT-BOX INFO BUTTONS OK.
2. The BASE64-ENCODE function and the GENERATE-UUID function may be used to generate a unique CHARACTER index as demonstrated by the following code. Defining the key field as CASE-SENSITIVE is a required unless the code traps for duplicates generated by the BASE64-ENCODE function:
DEFINE TEMP-TABLE test
FIELD lcField AS CHARACTER CASE-SENSITIVE
INDEX lcidx IS PRIMARY UNIQUE lcField ASCENDING.
DEFINE VARIABLE iVar AS INTEGER NO-UNDO.
DO iVar = 1 TO 1000000 ON ERROR UNDO, LEAVE:
CREATE test.
ASSIGN
lcField = SUBSTRING(BASE64-ENCODE(GENERATE-UUID), 1, 22).
END.
MESSAGE iVar
VIEW-AS ALERT-BOX INFO BUTTONS OK.
3. The GUID function and the GENERATE-UUID function may be used to generate a unique CHARACTER index as demonstrated by the following code. Defining the key field as CASE-SENSITIVE is not required here because the GUID function will generate unique strings for each unique RAW value generated by the GENERATE-UUID function:
DEFINE TEMP-TABLE test
FIELD lcField AS CHARACTER
INDEX lcidx IS PRIMARY UNIQUE lcField ASCENDING.
DEFINE VARIABLE iVar AS INTEGER NO-UNDO.
DO iVar = 1 TO 1000000 ON ERROR UNDO, LEAVE:
CREATE test.
ASSIGN
lcField = GUID(GENERATE-UUID).
END.
MESSAGE iVar
VIEW-AS ALERT-BOX INFO BUTTONS OK.