I'm creating a multitenant app where some of the tables need to have sequentially assigned integer values. The ordering is done independently for each tenant. As a concrete example, consider a Student
table with a RegNumber
column. RegNumber
has to be assigned sequentially, but the sequence is local to each tenant.
The solution I'm thinking of involves using another table to hold the "next available" RegNumber
value for each tenant, which leads me to a couple of questions:
- Is there a better way?
- What is the best way to do a "SELECT FROM tenant_studentid_sequence" and "INSERT INTO students" in a single transaction without excessive locking, and without the possibility of skipping or duplicating values?
In MySQL, I could use SELECT FOR UPDATE, but what about SQL Server 2008? There's quite a bit of discussion on this SO question, but it seems to be based on SQL Server 2005. Any changes in 2008? What's the recommended strategy?
Edit 1: I think I should clarify what I meant by "independently for each tenant". What I'm looking for is a way for each tenant to have a sequentially ordered set of student IDs. That is, tenant A will have students with IDs 1, 2, 3, ..., and so will tenant B. Think of them as business keys. I have GUIDs for global identity which is hidden from the customer.