Context: web application, SQL Server / SQL Azure through EF Code first, asp.net mvc 3, windows azure.
I have a TPH table company
which contains both customer companies and provider companies. The table contains a column type
, it's the table discriminator: c
for customers and p
for providers.
The primary key of the table is an auto incremented integer id
, managed by the database.
Now I would like to have a functional compouned key, ( short_id, type
), where short_id
is also an autoincremented integer but I don't know how and when generate short_id
values to avoid concurrency issues.
How can I prevent two different customer companies to get the same short_id
if one is entered in the database in new york at the very same time the other one is entered in paris.
id name type short_id
1 company_a c 1
2 company_b c 2
3 company_c p 1
4 company_d c 3
5 company_e p 2
6 company_f c 4
I know it's unlikely since the example is dumb but the question is technical.
Thank you for reading
You might try this, though it could get slow if the table is large:
INSERT INTO company (name, type, short_id) SELECT 'mycompany', 'c', ISNULL(MAX(short_id),0) + 1 FROM company WHERE type = 'c'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.