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:56

I had a similar situation with a project.

My solution: By default, users only see the first 7 characters of the GUID.

It's sufficiently random that collisions are extremely unlikely (1 in 268 million), and it's efficient for speaking and typing.

Internally, of course, I'm using the entire GUID.

查看更多
迷人小祖宗
3楼-- · 2019-01-21 00:57

Handle it at the user interface--add a prefix letter (or letters) onto the ID number when reporting it to the users. So o472 would be an order, b531 would be a bill, and so on. People are quite comfortable mixing letters and digits when giving "numbers" over the phone, and are more accurate than with straight digits.

查看更多
男人必须洒脱
4楼-- · 2019-01-21 00:59

You could use an autoincrement column to generate the unique id. Then have a computed column which takes the value of this column and prepends it with a fixed identifier that reflects the entity type, for example OR1542 and DL1542, would represent order #1542 and delivery #1542, respectively. Your prefix could be extended as much as you want and the format could be arranged to help distiguish between items with the same autoincrement value, say OR011542 and DL021542, with the prefixes being OR01 and DL02.

查看更多
【Aperson】
5楼-- · 2019-01-21 01:04

Maybe an itemType-year-week-orderNumberThisWeek variant?

o2009-22-93402

Such identifier can consist of several database column values and simply formatted into a form of an identifier by the software.

查看更多
Bombasti
6楼-- · 2019-01-21 01:05

We faced a similar problem on a project. We solved it by first creating a simple table that only has one row: a BIGINT set as auto-increment identity. And we created an sproc that inserts a new row in that table, using default values and inside a transaction. It then stores the SCOPE_IDENTITY in a variable, rolls back the transaction and then returns the stored SCOPE_IDENTITY.

This gives us a unique ID inside the database without filling up a table.

If you want to know what kind of object the ID is referring to, I'd lose the transaction rollback and also store the type of object along side the ID. That way findout out what kind of object the Id is referring to is only one select (or inner join) away.

查看更多
登录 后发表回答