Solved

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

Posted on 2011-02-13
8
219 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:ewangoya
ewangoya 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

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:ewangoya
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

828 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