开发者

How do I create a Identity starting with a Letter first then numbers after?

I want to create data that contains info about my supplier making it auto generate his ID. For example, SupplierID I want it to app开发者_如何学Goear as - SID001, SID002 all to auto generate after each other.

How do I do this with SQL?


Ask yourself this: what are the costs of doing this? In particular, what is required to compare two strings versus comparing two numbers? To generate strings from numbers?

Then ask yourself, what value is added by having an id of 'SID0001' rather than just 001?

Then ask yourself, is there an easy way to display a prefix without redundantly storing it for each row? (Answer: yes, with a database view).


You could just use a sequence or identity column and store the prefix in a separate column, or alternately, if the table in question will only ever have suppliers with a prefix of SID, don't store the SID part at all and simply add it at the application level.


You could check for the next ID available and concatenate your prefix with that value. The result should be inserted in the SupplierID column.


Some databases(Oracle, postgres etc) support a sequence for number part. Some(mysql) have an auto increment feature, so you get new number when inserting.

You could then concatenate with string to generate string based IDs.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜