I now have a requirement that a column has to be added to a table that holds a unique ID (guid). This ID is used to match records in different tables and databases but there will be NO FK constraints. Would it be better to store the guid as a varchar(32) or as a uniqueidentifier type?
There will be joins done using this column but not on a regular basis. This ID is NOT a PK. I'm asking in terms of storage and performance.
There are two functions using which you can create GUIDs in SQL Server – NewID and NewSequentialID. And there's a data type – "uniqueidentifier" which can be used to store GUIDs. It stores a 16-btye binary value.
if you have a char/varchar value formatted as the standard GUID, you can simply store it as BINARY(16) using the simple CAST(MyString AS BINARY16), without all those mind-boggling sequences of CONCAT + SUBSTR.
The GUID is stored as byte-array, that is to say binary(16). You could also store it as UInt128, or two bigints.
-- If you want to generate a new Guid (uniqueidentifier) in SQL server the you can simply use the NEWID() function. -- This will return a new random uniqueidentifier e.g. You can directly use this with INSERT statement to insert new row in table.
If it's a GUID, store it as a uniqueidentifier
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With