- An Orders table has a CustomerId column and an OrderId column.
- For certain reasons it's important that an order's id is no longer than 2-bytes.
- There will be several million orders in total, which makes 2-bytes not enough for a global order id.
- A customer will have no more than several thousand orders making 2-bytes enough.
- The obvious solution is to have the (CustomerId, CustomerOrderNumber) be unique rather than OrderId itself.
The problem is generating the next CustomerOrderId. 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, CustomerOrderId) is unique but CustomerOrderNumber 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.