Custom PrimaryKey Generation with autoincrement

后端 未结 6 772
小蘑菇
小蘑菇 2021-01-22 21:08

I need to define and generate primary key for 2 or more tables.

Tables hold same type of data but FOR Some BUSINESS RULES we have to make them separate say like

6条回答
  •  有刺的猬
    2021-01-22 21:25

    The other answers all make valid points.

    I would like to add, since it appears there is some desire for there to be separate sequences for both local and international customers, that relying on any particular sequential behavior is a bad idea. In particular, for identity columns there can be gaps if an insertion fails and is rolled back (an identity is reserved for the transaction immediately, but if a constraint or trigger fails it will never be inserted). It is also possible to change the seed and spacing and also to use identity insert to insert non-sequential values.

    I would advocate for a true surrogate identity column with a customer type flag. Your identity column will be unique across all customers. If you absolutely want separate sequences you can either write a trigger to get a true non-gapped sequence or use identity. Then you can use a persisted computed column as you suggest to build the combined key and put an index on that column depending on your needs.

提交回复
热议问题