Solved

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

Posted on 2011-02-13
8
220 Views
Last Modified: 2012-05-11
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
Comment
Question by:calorisplanitia
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 16

Accepted Solution

by:
BurnieP earned 300 total points
ID: 34884049
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
 
LVL 32

Assisted Solution

by:Ephraim Wangoya
Ephraim Wangoya earned 200 total points
ID: 34884052

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
 

Author Comment

by:calorisplanitia
ID: 34884063
What if there is a case like chat application where hundreds of msgs are inserted at a time?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:calorisplanitia
ID: 34884074
will the table be available for view when the records are inserted, while using identity?
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 34884084

Even in such a case, an Identity column will still perform better than generating your own unique id
0
 
LVL 16

Expert Comment

by:BurnieP
ID: 34884089
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
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 34886697
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
 

Author Comment

by:calorisplanitia
ID: 35000019
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question