views:

117

answers:

1

I mostly work with sql-server (when I do work with databases) and I am trying to learn pl-sql.

Is there any equivalent to sql-server's auto-generated Guid as primary keys in Oracle?

+4  A: 

You can use SYS_GUID() to generate a GUID, and use it as DEFAULT value of a column:

CREATE TABLE test_table (
  uid_col RAW(32) DEFAULT SYS_GUID(),
  some_val VARCHAR2(10)
);

EDIT: See answers to this question for more details.

Peter Lang
You'd also have to code a trigger to ensure that a user- or application-provided RAW(32) wasn't supplied, otherwise you could be subject to key collisions. Even that isn't completely certain, as triggers can be disabled and some applications (like SQL*Loader in direct path mode) can ignore triggers altogether.I'm not saying Peter's solution isn't the best availble; just that Oracle doesn't really permit completely system-generated keys.
Adam Musch