pyramation
pyramation

Reputation: 1671

how to implement unique IDs across many tables

If you look at facebook's graph API, it seems as though all objects share the same ID space, and all Ids are unique even if they are in different tables.

Is there a feature in MySQL that handles this? (if not, high level idea of how to implement?)

Upvotes: 0

Views: 261

Answers (3)

uzyn
uzyn

Reputation: 6683

You may want to check out UUID(). It returns a globally unique ID so your IDs will never clash.

To convert it to integer format, you can

UNHEX(REPLACE(UUID(),'-',''))

for storing in a BINARY(16) column.

(Source for converting to integer: Nicholas Sherlock's comment at MySQL reference)

Upvotes: 3

Randy
Randy

Reputation: 16677

OR use a single sequence to drive the ID values.

Upvotes: 0

user1549550
user1549550

Reputation:

If you have a single master database server, you can create a table called Object that has an integer auto-incrementing primary key and an object type column. Every time you create an object, you insert a row into this Object table first, get the id, then use that id to insert a row into whatever table will hold the object information. So to create an Event:

INSERT INTO Object (object_type) VALUES ('Event')

Get the last insert id, let's say it's 12345.

INSERT INTO Event (id, name, location) VALUES (12345, 'Cookout', 'My back yard')

Upvotes: 0

Related Questions