Note that there are some explanatory texts on larger screens.

plurals
  1. POIdentity columns for multitenant database schemas
    primarykey
    data
    text
    <p>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 <code>Student</code> table with a <code>RegNumber</code> column. <code>RegNumber</code> has to be assigned sequentially, but the sequence is local to each tenant.</p> <p>The solution I'm thinking of involves using another table to hold the "next available" <code>RegNumber</code> value for each tenant, which leads me to a couple of questions:</p> <ol> <li>Is there a better way?</li> <li>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?</li> </ol> <p>In MySQL, I could use SELECT FOR UPDATE, but what about SQL Server 2008? There's quite a bit of discussion <a href="https://stackoverflow.com/questions/1483725/select-for-update-with-sql-server">on this SO question</a>, but it seems to be based on SQL Server 2005. Any changes in 2008? What's the recommended strategy?</p> <p><strong>Edit 1:</strong> 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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload