GUID vs INT IDENTITY [duplicate]
Kimberley Tripp (SQLSkills.com) has an article on using GUID’s as primary keys. She advices against it because of the unnecessary overhead.
Kimberley Tripp (SQLSkills.com) has an article on using GUID’s as primary keys. She advices against it because of the unnecessary overhead.
C# (safe) code (Compliments of the NHibernate Guid Comb Generator) Guid GenerateComb() { byte[] destinationArray = Guid.NewGuid().ToByteArray(); DateTime time = new DateTime(0x76c, 1, 1); DateTime now = DateTime.Now; TimeSpan span = new TimeSpan(now.Ticks – time.Ticks); TimeSpan timeOfDay = now.TimeOfDay; byte[] bytes = BitConverter.GetBytes(span.Days); byte[] array = BitConverter.GetBytes((long) (timeOfDay.TotalMilliseconds / 3.333333)); Array.Reverse(bytes); Array.Reverse(array); Array.Copy(bytes, bytes.Length – … Read more
Could you not just store the .Net Guid as a string and read it into Java? That way you don’t need to worry about byte order or anything. If not then This explains how the bytes are laid out in C# http://msdn.microsoft.com/en-us/library/fx22893a.aspx
If you read the Examples section from the GUID constructor, you’ll find your answer: Guid(1,2,3,new byte[]{0,1,2,3,4,5,6,7}) creates a Guid that corresponds to “00000001-0002-0003-0001-020304050607”. a is a 32-bit integer, b is a 16-bit integer, c is a 16-bit integer, and d is simply 8 bytes. Because a, b, and c are integer types rather than raw … Read more
Here’s a code snippet showing how to insert a GUID using a parameterised query: using(SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); using(SqlTransaction trans = conn.BeginTransaction()) using (SqlCommand cmd = conn.CreateCommand()) { cmd.Transaction = trans; cmd.CommandText = @”INSERT INTO [MYTABLE] ([GuidValue]) VALUE @guidValue;”; cmd.Parameters.AddWithValue(“@guidValue”, Guid.NewGuid()); cmd.ExecuteNonQuery(); trans.Commit(); } }
Some good identifiers: MAC Address: It’s fairly easy to get at, and it’s usually unique. However, it can be spoofed/changed rather easily, so it depends on how unique it needs to be. CPU Serial Number: It’s not available on lots of older systems, but it’s there. Check out this MSDN page. It won’t change, but … Read more
As of Ruby 1.9, uuid generation is built-in. Use the SecureRandom.uuid function. For example: require ‘securerandom’ SecureRandom.uuid # => “96b0a57c-d9ae-453f-b56f-3b154eb10cda”
Guid id = Guid.NewGuid();
You could just use the same Win32 API function that SQL Server uses: UuidCreateSequential and apply some bit-shifting to put the values into big-endian order. And since you want it in C#: private class NativeMethods { [DllImport(“rpcrt4.dll”, SetLastError=true)] public static extern int UuidCreateSequential(out Guid guid); } public static Guid NewSequentialID() { //Code is released into … Read more
The answer is: You should not need to know this. As stated in the accepted answer to a related question: A GUID doesn’t make guarantees about randomness, it makes guarantees around uniqueness. An even stronger statement on security and randomness is made in RFC4122, which speficies the UUID format: Do not assume that UUIDs are … Read more