Page 1 of 1

Keyvalues in the Database

PostPosted: Mon Oct 08, 2012 8:04 am
by SyLuS
Hey guys,

i want to add sone stuff to the database. By doing so ive seen, that the primary keys in the tables always filled with xxxxx-xxxx-xxxxx. Is there any reason for doing this? And how do you create these code? Is it just random or some kind of hash?

LG

SyLuS

Re: Keyvalues in the Database

PostPosted: Mon Oct 08, 2012 8:51 am
by Dunnerholl
its a uniqueidentifier (GUID) created by c# code and "necessary" for the currently used databaselayer (nhibernate in some version)

Re: Keyvalues in the Database

PostPosted: Mon Oct 08, 2012 8:55 am
by SyLuS
Okay.

Can you please tell me how it is create or where i can found the code which creates it?

Im currently write a little off server tool to administer merchants (specially changing item list).

Re: Keyvalues in the Database

PostPosted: Mon Oct 08, 2012 9:08 am
by Graveen
just take an unique string.

can be sylus-001, sylus-002, etc....

i tell it once again: it must be unique !

Re: Keyvalues in the Database

PostPosted: Mon Oct 08, 2012 9:32 am
by SyLuS
You dont have to tell me, that it have to be unique.

I just wanted to know, how do you create this unique value. Is there any hash function for doing that?

My solution were to take a random number, convert them to binary. Foreach 0 create an random number and foreach 1 create an random character so i will get an random string with characters and numbers.

So i use this 3 Times ( XXXXX - XXXX - XXXXX ) and use this as unique key.

But if there is a function already i dont have to code it ^^

Dont invent the wheel every time new.

Re: Keyvalues in the Database

PostPosted: Mon Oct 08, 2012 12:16 pm
by Dunnerholl
as i said before: System.Guid

System.Guid.NewGuid();

Re: Keyvalues in the Database

PostPosted: Mon Oct 08, 2012 2:04 pm
by SyLuS
Thanks

Re: Keyvalues in the Database

PostPosted: Mon Oct 08, 2012 6:42 pm
by Blue
Any key you think of which is unique will do. So its completely legit to do names like "toaspell1". The autogeneration always uses GUID to prevent collisions. The advantage is you can easily merge in data from others which won't be easily possible with lets say integer keys.