Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL: Storing guids

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.

like image 368
Dustin Davis Avatar asked May 05 '11 19:05

Dustin Davis


People also ask

How GUIDs are stored in SQL Server?

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.

How do I save GUID?

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.

How is GUID stored?

The GUID is stored as byte-array, that is to say binary(16). You could also store it as UInt128, or two bigints.

How SQL GUID is generated?

-- 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.


1 Answers

If it's a GUID, store it as a uniqueidentifier

like image 124
Jack Marchetti Avatar answered Sep 23 '22 13:09

Jack Marchetti