Database-wide unique-yet-simple identifiers in SQL

2019-01-21 00:16发布

First, I'm aware of this question, and the suggestion (using GUID) doesn't apply in my situation.

I want simple UIDs so that my users can easily communicate this information over the phone :

Hello, I've got a problem with order 1584

as opposed to

hello, I've got a problem with order 4daz33-d4gerz384867-8234878-14

I want those to be unique (database wide) because I have a few different kind of 'objects' ... there are order IDs, and delivery IDs, and billing-IDs and since there's no one-to-one relationship between those, I have no way to guess what kind of object an ID is referring to.

With database-wide unique IDs, I can immediately tell what object my customer is referring to. My user can just input an ID in a search tool, and I save him the extra-click to further refine what is looking for.

My current idea is to use identity columns with different seeds 1, 2, 3, etc, and an increment value of 100.

This raises a few question though :

  • What if I eventually get more than 100 object types? granted I could use 1000 or 10000, but something that doesn't scale well "smells"

  • Is there a possibility the seed is "lost" (during a replication, a database problem, etc?)

  • more generally, are there other issues I should be aware of?

  • is it possible to use an non integer (I currently use bigints) as an identity columns, so that I can prefix the ID with something representing the object type? (for example a varchar column)

  • would it be a good idea to user a "master table" containing only an identity column, and maybe the object type, so that I can just insert a row in it whenever a need a new idea. I feel like it might be a bit overkill, and I'm afraid it would complexify all my insertion requests. Plus the fact that I won't be able to determine an object type without looking at the database

  • are there other clever ways to address my problem?

11条回答
我想做一个坏孩纸
2楼-- · 2019-01-21 00:39

Why not a simple Base36 representation of a bigint? http://en.wikipedia.org/wiki/Base_36

查看更多
何必那么认真
3楼-- · 2019-01-21 00:41

I use a high/low algorithm for this. I can't find a description for this online though. Must blog about it.

In my database, I have an ID table with an counter field. This is the high part. In my application, I have a counter that goes from 0 to 99. This is the low part. The generated key is 100 * high + low.

To get a key, I do the following

initially high = -1
initially low = 0

method GetNewKey()
begin
  if high = -1 then
    high = GetNewHighFromDatabase

  newkey = 100 * high + low.
  Inc low
  If low = 100 then
    low = 0
    high = -1

  return newKey
end

The real code is more complicated with locks etc but that is the general gist.

There are a number of ways of getting the high value from the database including auto inc keys, generators etc. The best way depends on the db you are using.

This algorithm gives simple keys while avoiding most the db hit of looking up a new key every time. In testing, I found it had similar performance to guids and vastly better performance than retrieving an auto inc key every time.

查看更多
劳资没心,怎么记你
4楼-- · 2019-01-21 00:43

I would implement by defining a generic root table. For lack of a better name call it Entity. The Entity table should have at a minimum a single Identity column on it. You could also include other fields that are common accross all your objects or even meta data that tells you this row is an order for example.

Each of your actual Order, Delivery...tables will have a FK reference back to the Entity table. This will give you a single unique ID column

Using the seeds in my opinion is a bad idea, and one that could lead to problems.

Edit

Some of the problems you mentioned already. I also see this being a pain to track and ensure you setup all new entities correctly. Imagine a developer updating the system two years from now.

After I wrote this answer I had thought a but more about why your doing this, and I came to the same conclusion that Matt did.

查看更多
我想做一个坏孩纸
5楼-- · 2019-01-21 00:43

You could create a master UniqueObject table with your identity and a subtype field. Subtables (Orders, Users, etc.) would have a FK to UniqueObject. INSTEAD OF INSERT triggers should keep the pain to a minimum.

查看更多
兄弟一词,经得起流年.
6楼-- · 2019-01-21 00:46

Why not use identities on all the tables, but any time you present it to the user, simply tack on a single char for the type? e.g. O1234 is an order, D123213 is a delivery, etc.? That way you don't have to engineer some crazy scheme...

查看更多
戒情不戒烟
7楼-- · 2019-01-21 00:50

MS's intentional programing project had a GUID-to-word system that gave pronounceable names from random ID's

查看更多
登录 后发表回答