Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8941989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:25:44+00:00 2026-06-15T11:25:44+00:00

I have a table with INT primary key column. I do not want to

  • 0

I have a table with INT primary key column. I do not want to use GUID or IDENTITY as primary key.

I wanted to know what is the best possible way to get the next ID without using an IDENTITY or GUID as primary key column in the table. I do not mind using GUID or IDENTITY in the table unless it’s the primary key column.

I have to find the next available ID (i.e get the MAX ID and increment it by 1), and use that:

SELECT @id=ISNULL(max(AlbumId)+1,1) FROM Albums

However, I want to prevent other applications from inserting into the table when I’m doing this so that we don’t have any problems.

NOTE: Kindly do not mark this question as DUPLICATE . I have gone through this answer and could not understand it properly. Would anyone be kind enough to explain me what is it they are doing? And can I use the same technique?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:25:45+00:00Added an answer on June 15, 2026 at 11:25 am

    For SQL Server 2000 – 2008 R2, using INT IDENTITY is by far the easiest and most reliable way to do it.

    Trying to do this yourself is like reinventing the wheel and carries a lot of ways in which it can go wrong – so why bother?? You need to (a) make sure this mechanism is concurrency safe (and just doing SELECT MAX(ID) + 1 is NOT safe!), and you (b) need to make sure your mechanism doesn’t become a major bottleneck in system performance, either…

    What’s your problem with using INT IDENTITY as your primary key?? Doesn’t seem very rational…

    For SQL Server 2012 and newer, you could also consider using a SEQUENCE (which is basically an IDENTITY that’s not specifically coupled to a single table)

    Update: that other SO question that you mention in your post is doing basically a “do-it-yourself” simulation of a SEQUENCE:

    • a table holds pairs of (sequencename, currentvalue)
    • when you ask for a new value for a given sequence, the new value is determined by using just an UPDATE statement coupled with the OUTPUT clause

    Why so complicated? This is the easiest and most efficient way (other than using an IDENTITY) to safely handle concurrent requests. The UPDATE statement will always exclusively lock that one row in the “sequence table” for the sequence you want to get a new ID from – thus preventing any other transactions to grab the same ID and get duplicated values – and it returns the new ID using the OUTPUT clause back to the caller.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a (simplified) table consisting of three columns: id INT PRIMARY KEY NOT
I have the following tables: create table TableA ( Id int primary key identity,
I have two SQL statements: CREATE TABLE legs(legid INT PRIMARY KEY AUTO_INCREMENT NOT NULL,
Given this: create table Location( LocationId int identity(1,1) not null primary key, Address nvarchar(max)
I have a table called Table with columns: ID (int, primary key, clustered, unique
I have a table which has the below columns. Ticket_id (Primary key, Int) Attachment1
Suppose I have a table Item (Id int Primary Key, Number INT) having records
e.g. If I have a table like below:- create table Categories(CategoryId int primary key,
I have a mysql table structure like that: id int primary key name varchar
I have the following pseudo-SQL schema: table flight id int primary key date timestamp

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.