Compound IDENTITY column in SQL SERVER 2008

Posted by Asaf R on Stack Overflow See other posts from Stack Overflow or by Asaf R
Published on 2010-03-25T19:17:12Z Indexed on 2010/03/25 19:33 UTC
Read the original article Hit count: 442

  • An Orders table has a CustomerId column and an OrderId column.
  • For certain reasons it's important that OrderId is no longer than 2-bytes.
  • There will be several million orders in total, which makes 2-bytes not enough.
  • A customer will have no more than several thousand orders making 2-bytes enough.
  • The obvious solution is to have the (CustomerId, OrderId) be unique rather than (OrderId) itself.

The problem is generating the next Customer's OrderId. Preferably, without creating a separate table for each customer (even if it contains only an IDENTITY column), in order to keep the upper layers of the solution simple.

Q: how would you generate the next OrderId so that (CustomerId, OrderId) is unique but OrderId itself is allowed to have repetitions? Does Sql Server 2008 have a built in functionality for doing this?

For lack of a better term I'm calling it a Compound IDENTITY column.

© Stack Overflow or respective owner

Related posts about sql

Related posts about tsql