A UUID is a Universally Unique Identifier, meaning that these identifiers are unique over your whole DB, even better unique" across space and time". UUID's are stored in 128bit format and follow a standarized format (rfc4122) This makes the type a great alternative to the ID as identity column, I would dare say a superior alternative.
Advantages of the UUID:
-
Easy replications
-
Between systems
This is possible because the mac-address of the system is integrated in the UUID.
You would be able to use the primary keys generated by a third party. No need for mapping between ID's! This makes data integrations effortless!
-
Generated randomly, impossible to guess next/previous value
-
This is very important in the context of public API's. Most API protocols expose their unique keys in the url.
The developer does not have to worry about hashing/encrypting identifiers. (Imagine a click and print coupon where you can guess the next coupon...)
Supported by almost every respected ORM (sometimes even the defualt over the ID)
Add the advantages of a regular ID to this list as well...
UUID usage's for DB2 (pseudo code)
Similar to ID:
CREATE OR REPLACE TABLE USERS (
uuid uuid not null generated always as identity,
firstname varchar(50) not null,
lastname varchar(50) not null
primary key (uuid )
);
An alternative would be to combine GENERATED ALWAYS with DEFAULT GENERATE_UUID():
CREATE OR REPLACE TABLE USERS (
uuid varchar(50) generated always default GENERATE_UUID(),
firstname varchar(50) not null,
lastname varchar(50) not null
primary key (uuid)
);
My least favorite alternative is provide a default so applications don't have to pass the value every time. But it allows invalid userinput:
CREATE OR REPLACE TABLE USERS (
uuid varchar(50) not null default GENERATE_UUID(),
firstname varchar(50) not null,
lastname varchar(50) not null
primary key (id)
);
Note: It seems like we can't use scalar functions like GENERATE_UNIQUE() as default value today? Is this a separate idea ticket?
Workarounds/Alternatives (executable SQL attached to the ticket)
Rowid
UUID Columns idea
GUID/UUID identity support idea
CREATE OR REPLACE TABLE USERS (
rid rowid generated always,
firstname varchar(50) not null,
lastname varchar(50) not null
);
insert into users (firstname, lastname)
values ('Mario', 'Aquino'), ('Dimi', 'VLH');
-- Does NOT work => only one rowid/table allowed
CREATE OR REPLACE TABLE ADDRESSES (
rid rowid generated always,
street varchar(50),
city varchar(50),
user_id rowid,
foreign key (user_id) REFERENCES users(id)
);
-- Does not work because the types are not compatible?
CREATE OR REPLACE TABLE ADDRESSES (
rid rowid generated always,
street varchar(50),
city varchar(50),
user_id varchar(26),
foreign key (user_id) REFERENCES users(id)
);
Triggers
CREATE OR REPLACE TABLE USERS (
uuid varchar(13) not null,
firstname varchar(50) not null,
lastname varchar(50) not null,
primary key (uuid)
);
CREATE OR REPLACE TRIGGER generate_uuid AFTER INSERT ON USERS FOR EACH ROW MODE DB2SQL UPDATE USERS SET UUID = GENERATE_UNIQUE();
insert into users (uuid, firstname, lastname)
values ('tempstring', 'Mario', 'Aquino'), ('tempstring2', 'Dimi', 'VLH');
select *
from users;
SQL adapters in open source languages will have to check if the trigger exists before every operation.
On insert we have to send some dummydata to place in the table before the trigger is executed. For an identity column it becomes a daunting task to provide unique dummydata.
Alternative with SQL Sequence Object
CREATE OR REPLACE TABLE USERS (
uuid integer not null,
firstname varchar(50) not null,
lastname varchar(50) not null,
primary key (uuid)
);
CREATE OR REPLACE SEQUENCE unique_identifiers AS INTEGER
START WITH 1
INCREMENT BY 1
NO MAXVALUE
NO CYCLE
CACHE 20;
-- Does not work, because the uuid is not generated
insert into users (firstname, lastname)
values ('Mario', 'Aquino'), ('Dimi', 'VLH');
insert into users (uuid, firstname, lastname)
values (next value for unique_identifiers, 'Mario', 'Aquino'), (next value for unique_identifiers,'Dimi', 'VLH');
select *
from users;
What about other databases?
Postgresql has great support for this column type. MySQL and SQLite stumble behind, but their connector-packages provide support from within the OpenSource language. I'm not a fan of this approach since you have to translate the logic to every OpenSource language...
I know this idea has been posted in the past. But I think this should be reconsidered.
UUID's are very useful, especially in collaboration with other applications and third party's. The workarounds do not suffice and are too much repetitive work...
Closing this one out so discussion can be contained in the one request.