Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
if i have an sql database that contains a lot of data with int primary keys and i want to convert them into uniqueidentifier with keeping the relationships
what should i do
my reason for using uniqueidentifier keys is :
i work with many servers with the same database schema and the only way to ensure that duplicate keys will never occurs is using uniqueidentifier
Posted
Comments
Maciej Los 5-Nov-14 11:10am    
What kind of database engine?

If you already have a working database and application then this is a very hard thing to do and requires a partial redesign of both, which can be nontrivial at best.

Good luck!
 
Share this answer
 
Comments
Herman<T>.Instance 5-Nov-14 9:45am    
He better keeps the keys but adds a unique field to the desired tables. On these fields the other servers could find or match data
Mehdi Gholam 5-Nov-14 9:52am    
Yes definitely a starting point, but the real work starts from there :)
oula alsheikh 5-Nov-14 9:56am    
i didn't understand what you meant by definitely a starting point can you explain more?
Mehdi Gholam 5-Nov-14 10:03am    
That is the first thing to do, but the real work starts when you try to remove the dependency on int pid's in your tables, so you can replicate data between your servers.
BillWoodruff 6-Nov-14 5:52am    
+5
The simplest way is:

C#
int k = 0x12345678 ;
System.Guid g = new System.Guid ( k , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ) ; 

{12345678-0000-0000-0000-000000000000}


But I'd rather do it this way:

C#
int k = 0x12345678 ;
System.Guid g = new System.Guid ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 
(byte) ( k >> 24 & 0xFF ) , (byte) ( k >> 16 & 0xFF ) , (byte) ( k >> 8 & 0xFF ) , (byte) ( k & 0xFF ) ) ;

{00000000-0000-0000-0000-000012345678}


You can continue to use integers on the local database and use GUIDs only for the central database -- and use an unused section of the GUID to indicate which site the record came from.
 
Share this answer
 
Comments
oula alsheikh 5-Nov-14 10:01am    
thanks i think this is a very good idea to put a section for site the record came from
in this way i can ensure that there will be no conflict
BillWoodruff 6-Nov-14 5:54am    
+5 if it's good for the OP, it's good to me :)
There is a Guid constructor overload[^] which allows you to specify the constituent part of the guid.

C#
int pk = 123456789;
short system = 5;
var guid = new Guid(pk, system, 0, new byte[] {0, 0, 0, 0, 0, 0, 0, 0});


But it's an overkill becuase Guid is 16 bytes and you really need just 5 or 6. Maybe you should create your own structure.
 
Share this answer
 
Comments
Tomas Takac 5-Nov-14 10:41am    
To the downvoter: I'd like to know the reason, thanks.
BillWoodruff 6-Nov-14 5:53am    
+5 vote to counteract absurd down-vote.
Tomas Takac 6-Nov-14 8:31am    
thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900