Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

Best way to generate userid(integer value), should i use identity column or something else? Please advise

I have a login table which generates users and generates an id for the user. I am not sure whether to use IDENTITY_Column(autonumber) so that sql server generates a unique id by itself or use something else.
Some people advised me using identity specification lock table and causes performance issues.

At present i am using the following:
SET NOCOUNT ON;
	DECLARE @intErrorCode INT
	BEGIN TRANSACTION
		SET @TID=(select (counterfrom + currentcounter) from e_counter 
		SELECT @intErrorCode = @@ERROR
		IF (@intErrorCode <> 0) GOTO PROBLEM
		
		insert into e_Login
		(id,Username,Password) values
		(@TID, 'U-','U-')
		SELECT @intErrorCode = @@ERROR
		IF (@intErrorCode <> 0) GOTO PROBLEM
		
		UPDATE e_counter set currentcounter=(currentcounter + 1) where functionno=@FUNID
		SELECT @intErrorCode = @@ERROR
		IF (@intErrorCode <> 0) GOTO PROBLEM
		
    COMMIT TRANSACTION
    
    PROBLEM:
	IF (@intErrorCode <> 0) 
		BEGIN
			ROLLBACK TRAN
			SET @TID=0
		END

Open in new window


I want to know the right approach as i need to use these for chat applications in msgs tables also where lots of records are inserted at a time by many users.
Please advise
Thanks in advance.
0
calorisplanitia
Asked:
calorisplanitia
  • 3
  • 2
  • 2
  • +1
2 Solutions
 
BurniePCommented:
It depends.

Using Identity with auto increment you are 100% positive that every inserts will have a unique ID.  If you build it yourself, you need to be very cetain that if 2 inserts happen at the same time, you will not get the same generated ID which will cause trouble.  And by looking at the way you are doing it, you seem to be vulnerable to it.

I am personally using Identity with auto-increment in every table where it is required.  I never had any problems with lock issues, and never heard of any.
0
 
Ephraim WangoyaCommented:

You are better ff using IDENTITY column
There is no need for adding overhead just to get a unique ID when SQL server is already optimized to do the function for you.

An identity column does not adversely affect your performance in any way
0
 
calorisplanitiaAuthor Commented:
What if there is a case like chat application where hundreds of msgs are inserted at a time?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
calorisplanitiaAuthor Commented:
will the table be available for view when the records are inserted, while using identity?
0
 
Ephraim WangoyaCommented:

Even in such a case, an Identity column will still perform better than generating your own unique id
0
 
BurniePCommented:
It is even more better to use identity if you have hundreds of message inserted at a time.  The table will be available for view since inserting is not locking existing rows.
0
 
SAMIR BHOGAYTAFreelancer and IT ConsultantCommented:
Hi,

You have to use the identity column and also use this column with the concatenation with the userid + identity column and store it into any varchar type datafield and also define as unique value.
0
 
calorisplanitiaAuthor Commented:
Apologies,  i forgot to mark this question as completed.
I tested myself also and found that identity coulmns are much better.
Thanx to you all for helping me out.!
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now