开发者

handling of indvidual id and global id

开发者 https://www.devze.com 2023-04-02 17:08 出处:网络
I have an app that uses standard id integer autoincrement values. I also have another table that manages a global_id which is currently set as an integer. I\'m thinking it would be preferable to use a

I have an app that uses standard id integer autoincrement values. I also have another table that manages a global_id which is currently set as an integer. I'm thinking it would be preferable to use a non-integer for this global value so that it would be clear whether the item id or global id is being used based upon existence of a non-integer: for example:

site.com/item/id/123 - item id
site.com/item/id/12j45 - global id

Internally the global identification table will still keep it's id value (not sure about this). But I'd like to have a non-integer global_identifier_id. Is there a MySQL datatype that could handle this? Like get the id value and postpend a single (potentially) random character (1 to 1b, 2 to 2d).

Is there a bett开发者_如何学Goer scheme for handling this? For example, was also thinking id of object in question with objects registered to a phrase - hence location is id 123 but becomes 123loc as a global value. Perhaps, there is a rails component that already does this?

thx

@Doug

yes regarding uniqueness across tables; basically, a version of the object relational impedence mismatch issue (lists of objects). There is currently two sets of ids and I manage it manually similar to what you propose as two namespaces.

The namespaces are not needed though because if one is set up as int only and the other is set up as anything else, you know you are looking at a global_id.


It would be preferable to have two namespaces, such as:

site.com/item/id/123
site.com/item/gid/12j45

Otherwise, you would be requiring the global_id to have a character to differentiate it from the integer field, and create unnecessary cycles to determine how to query the data, based on the content of the data.

What are you trying to accomplish with having a global_id? Is it a level of uniqueness across multiple tables? If so, simply appending a letter to the id field won't suffice. Probably setting up a few triggers would do the trick, but I'd think through the process a little more first.

UPDATE: For uniqueness across multiple tables, there are a bunch of answers, but one simple one is:

CREATE TABLE global_references (
    id int NOT NULL auto_increment PRIMARY KEY,
    reference_type varchar(50) NOT NULL,
    reference_id int NOT NULL,
    UNIQUE KEY akGlobalReferences (reference_type, reference_id);

When you add a new record, insert a corresponding row in this table with the tablename and new id, and use the resulting id as your globally unique ID.

You may already have this in place and are looking for an alternative method of storage to add a character. If you truly want the same URL for both types of IDs, I'd recommend just having an 'inferred' prefix to the ID field. For instance, if the two URLs:

sites.com/item/id/123
sites.com/item/id/g123

You would check the first character. If it's g, strip it off and check the global table. Otherwise, check the table in question. However, I still recommend separate URLs for this.


I would just use a varchar for this field. That way, you can append whatever you want to the original item id. Maybe add something like _a27d to the original id (underscore plus 4 random characters) -- so you can always look before the underscore and get your original id back.


You can avoid having both a local id and a global id.

CREATE TABLE global_ids (
    id int NOT NULL auto_increment PRIMARY KEY);

Each of the tables has its own id that has a foreign key to the global_ids table.

CREATE TABLE t1 (
    id int NOT NULL auto_increment PRIMARY KEY,
    FOREIGN KEY (id) REFERENCES global_ids(id));

CREATE TABLE t2 (
    id int NOT NULL auto_increment PRIMARY KEY,
    FOREIGN KEY (id) REFERENCES global_ids(id));

You still end up doing two inserts (one for the global_ids, then one for the actual table), but you don't have to deal with two types of id.

0

精彩评论

暂无评论...
验证码 换一张
取 消