Skip to main content

How to Create UUID in Oracle Database

Complete guide with SYS_GUID() and RAW storage

How to Create UUID in Oracle Database. Call SYS_GUID() to generate a UUID in Oracle instantly. Looking for alternatives to the built-in function? A postgres uuid generator overview lists extension-based options for versions before 13.

Generate UUID for Oracle

SELECT SYS_GUID() FROM DUAL;
550e8400-e29b-41d4-a716-446655440000

Oracle Database takes a different approach to unique identifiers than you might expect from other systems: you'll reach for the built-in SYS_GUID() function rather than a dedicated UUID data type. Call it directly in a query or set it as a column default to generate a 16-byte globally unique value — though the raw output won't look like the hyphenated string you're used to seeing elsewhere. Below you'll find copy-paste ready SQL for generating a UUID in Oracle, converting a standard UUID string into Oracle's storage format, and the practical alternative when you need RFC 4122-compliant identifiers for cross-system compatibility.

How to Generate a UUID in Oracle

The simplest way to generate a UUID in Oracle is the built-in SYS_GUID() function, which returns a 16-byte globally unique identifier with no extension or setup required. The built-in function does not return a random value, a point the mysql uuid guide explains with sample queries.

sqlplus
-- Generate a GUID directly in a query
SELECT SYS_GUID() FROM DUAL;
-- Output: 5A3F8B2C1D4E4F6A9B8C7D6E5F4A3B2C (RAW hex, no hyphens)

-- Use it as a column default
CREATE TABLE users (
    id RAW(16) DEFAULT SYS_GUID() PRIMARY KEY,
    email VARCHAR2(255) NOT NULL
);

-- Insert without specifying id -- generated automatically
INSERT INTO users (email) VALUES ('user@example.com');

Explanation

Convert a String to a UUID in Oracle

A UUID generated elsewhere — in application code or another database — arrives as a standard hyphenated string, and Oracle needs it converted before it fits into a RAW(16) column. Without a built-in function in the core engine, the sqlite uuid guide shows two ways to create values.

sqlplus
-- Convert a hyphenated UUID string to RAW(16) for storage
SELECT HEXTORAW(REPLACE('550e8400-e29b-41d4-a716-446655440000', '-', ''))
FROM DUAL;

-- Convert a RAW(16) value back to a plain hex string
SELECT RAWTOHEX(id) FROM users WHERE email = 'user@example.com';

-- Simpler alternative: store as VARCHAR2(36) if the hyphenated
-- display format matters more than the 16-byte storage savings
CREATE TABLE sessions (
    id VARCHAR2(36) PRIMARY KEY,
    user_id RAW(16) NOT NULL
);

-- Insert a UUID generated in application code directly -- no conversion needed
INSERT INTO sessions (id, user_id)
VALUES ('550e8400-e29b-41d4-a716-446655440000', SYS_GUID());

Explanation

Other Options for Generating UUIDs in Oracle

When an application needs a standards-compliant UUID rather than Oracle's native GUID format — for compatibility with other databases or services — the practical approach is to generate it outside SYS_GUID() entirely.

sqlplus
-- Format one SYS_GUID() call as a hyphenated string for display only
-- (still Oracle's native GUID -- not an RFC 4122 v4 UUID)
SELECT LOWER(
    SUBSTR(hex, 1, 8)  || '-' || SUBSTR(hex, 9, 4)   || '-' ||
    SUBSTR(hex, 13, 4) || '-' || SUBSTR(hex, 17, 4)  || '-' ||
    SUBSTR(hex, 21, 12)
) AS display_uuid
FROM (SELECT RAWTOHEX(SYS_GUID()) AS hex FROM DUAL);
sqlplus
-- Practical alternative: generate the UUID in application code
-- (Java's UUID.randomUUID(), Python's uuid.uuid4(), etc.) and
-- insert it as-is -- Oracle just stores the value you provide.
INSERT INTO orders (id, order_date)
VALUES ('550e8400-e29b-41d4-a716-446655440000', SYSDATE);

-- id can be RAW(16) (convert first with HEXTORAW/REPLACE)
-- or VARCHAR2(36) (insert the hyphenated string directly)

Explanation

About the author

Vinitha Pu

Founder and designer

Vinitha Pu is a graphic designer based in Palakkad, Kerala, who builds free, privacy-friendly browser tools. She studied Textile and Apparel Design at Avinashilingam University and completed a C-Tech in Graphic Design at Image Creative Education in 2025. She designs, builds and maintains the UUID generators, validator, decoder and the language and database UUID guides on UUID Generator.

  • Content author: Vinitha Pu
  • Fact checked by: Vinitha Pu. Every generator is checked against the RFC 4122 and RFC 9562 layouts and every code example is run before it goes live.
  • Published Updated

Vinitha Pu on Behance · Vinitha Pu on LinkedIn

Comments & Feedback

Share your experience or ask questions about this tool

Copied!