2

I am entering student id as a randon number into the DB

  int num = r.Next(1000);
  Session["number"] = "SN" + (" ") + num.ToString();

But is there any chance of getting a duplicate number?How can i avoid this?

EDIT :: I have a identity column and the student id is separate from the ID,i am going to enter a random student id into the DB from UI.

chandra sekhar
  • 185
  • 3
  • 5
  • 15

9 Answers9

7

It is a very common task to have a column in a DB that is merely an integer unique ID. So much so that every database I've ever worked with has a specific column type, function, etc. for dealing with it. It will vary based on whatever specific database you use, but you should figure out what that is and use it.

  • You need a value that is unique not, random. The two are different. Random numbers repeat, they aren't unique. Unique numbers also aren't random. For example, if you just increment numbers up from 0 it will be unique, but that's not in any way random.
  • You could use a GUID, which would be unique, but it would be 128 bits. That's pretty big. Most databases will just have a counter that they increment every time you add an item, so 32 bits is usually enough. This will save you a lot of space. Incrementing a counter is also quicker than calculating a GUID's new value. For DB operations that tend to involve adding lots of items, that could matter.
  • As Jodrell mentions in the comments, you should also consider the size of the index if you use a GUID or other large field. Storing and maintaining that index will be much more expensive (in both time and space) with column that needs that many more bits.
  • If you try to do something yourself there's a good chance you'll do it wrong. Either your algorithm won't be entirely unique, it will have race conditions due to improper synchronization, it will be less performant because of excessive synchronization, it will be significantly larger because that's what it took to reduce the risk of collisions, etc. At the end of the day the database will have access to tools that you don't; let it take care of it so you don't need to worry about what you could mess up.
Servy
  • 202,030
  • 26
  • 332
  • 449
  • @chandrasekhar, read this, don't use a random number generator, don't use a GUID. – Jodrell Oct 05 '12 at 16:11
  • 1
    Think how much bigger the index on 128bit column will be compared to a 32bit column, how many students are you anticipating? – Jodrell Oct 05 '12 at 16:21
  • @Jodrell That deserves a mention in the answer. I didn't even think of that. – Servy Oct 05 '12 at 16:23
  • If you are using GUIDs you ought to make sure the index is well padded but I think that is beyond the scope of the question. – Jodrell Oct 05 '12 at 16:26
  • 1
    So what is the solution?? I am confused :\ – chandra sekhar Oct 05 '12 at 16:34
  • @chandrasekhar, since you are using SQLServer, use an identity column. This is what they are for. – Jodrell Oct 05 '12 at 16:36
  • @chandrasekhar That will depend on what type of database you are using. Whatever it is, it will have some way of automatically generating a unique value for each row. Use that. You could probably use google to find a tutorial on adding an identity column for . If you are having trouble after searching for a while, you could ask for help (but we would need to know which database). – Servy Oct 05 '12 at 16:36
  • @chandrasekhar, seems I may be wrong, only 1 IDENTITY per table http://stackoverflow.com/questions/349092/can-a-sql-server-table-have-two-identity-columns – Jodrell Oct 05 '12 at 17:10
3

you can use Guid's instead of random int , they are going to always be unique

There is no way to guarentee an int is unique unless you check every one that already exists, and even then - like the comments say , you are guarenteed duplicates when you pass 1000 ids

EDIT:

I mention that I think Guid's are best here because of the question , first indexing the table is not going to take long at all - it is assumed that there are going to be less then 1000 students because of the size of int, 128 bits is fine in a table with less then 1000 rows.

Guid's are a good thing to learn - even though they are not always the most effecient way

Creating a unique Guid in c# has a benifit that you can keep using and displaying that id - like in the question , without another trip to Db to figure out which unique id was assigned to the student

Scott Selby
  • 9,420
  • 12
  • 57
  • 96
  • its common practice to depend on Guid's to be unique, yes technically its not guarenteed to be unique , we depend on DNA to match killers where there is way more likely chance to random people have the same DNA then 2 unique Guid's – Scott Selby Oct 05 '12 at 16:11
  • true for version 1 GUIDs, other versions have different uniquness properties http://www.davewentzel.com/content/everything-you-need-know-about-guids – Jodrell Oct 05 '12 at 16:42
3

Sure there is a very likely chance that you will get a duplicate number. Next is just giving you a number between 0 and 1000, but there is no guarantee that the number will not be some number that Next has returned in the past.

If you are trying to work with unique values, look into possibly using Guids instead of integers or have a constantly increasing integer value instead of any random number. Here the reference page on Guid http://msdn.microsoft.com/en-us/library/system.guid.aspx

Steve Baer
  • 101
  • 4
2

Yes, you will get duplicates. If you want a truly unique item, you will need to use Guid. If you still want to use numbers, then you will need to keep track of the numbers you have already used, similar to identity column in database.

BlakeH
  • 3,354
  • 2
  • 21
  • 31
  • rather than making your own version of an identity column, why not just use an identity column? He's adding it to a DB after all. – Servy Oct 05 '12 at 16:14
  • Oh, I agree. I have to give the guy the benefit of the doubt sometimes - but a lot of times I think people are asking the wrong questions :) The next question will inevitably be, How do I get a generated identity column value for a new record? – BlakeH Oct 05 '12 at 16:30
  • If he can't figure it out and needs to ask then so be it; it's a good question to ask. The question is, "Is it unique (it's not) and if not, how do I make it unique." The best way to make it unique is a DB identity column, not to roll your own DB identity column. That's still answering the question ask asked. – Servy Oct 05 '12 at 16:32
2

Yes, you will certainly get duplicates. You could use a GUID instead:

Guid g = Guid.NewGuid();

GUIDs are theoretically "Globally Unique".

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
2

You can try to generate id using Guid:

Session["number"] = "SN" + (" ") + Guid.NewGuid().ToString();

It will highly descrease a chance to get duplicate id.

SHSE
  • 2,423
  • 17
  • 16
1

If you are using random numbers then no there is no way of avoiding it. There will always be a chance of a collision.

I think what you are probably looking for is an Identity column, or whatever the equivalent is for your database server.

Jodrell
  • 34,946
  • 5
  • 87
  • 124
1

In LINQ to SQL it is possible to set row like this:

    [Column ( IsPrimaryKey = true, IsDbGenerated = true )] 
    public int ID { get; set; }

I dont know if it helps you in asp, but maybe it is a good hint...

-1

Yes there is a chance of course.

Quick solution:
Check if it is a duplicate number first and try again until it is no longer a duplicate number.

Lucas Moeskops
  • 5,445
  • 3
  • 28
  • 42
  • You'll have to keep track of the used numbers in some way. – Lucas Moeskops Oct 05 '12 at 16:07
  • For a database it would take a network call to do the check which is both very inefficient and also subject to race conditions (two items get added with a new ID after they both verify it's unused). – Servy Oct 05 '12 at 16:10