Skip to main content

How to Create UUID in Oracle Database

Complete guide with SYS_GUID() and RAW storage

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. The postgresql uuid guide explains the performance difference between UUID v4 and time-sortable v7 identifiers.

Generate UUID for Oracle

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

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. For embedded database UUID generation, the sqlite uuid guide covers both server and mobile app scenarios.

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. Use the mysql uuid guide to optimize UUID index performance — BINARY(16) uses half the space of VARCHAR(36).

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

Comments & Feedback

Share your experience or ask questions about this tool

Copied!