GUID type in database

狂风中的少年 提交于 2019-12-24 11:40:12

问题


GUID is not an official data type in database. In our existing SQL Server design, the Uniqueidentifier type is used for GUID value. Now we are switching to Sybase database. Should we use varchar(36) to replace that Uniqueidentifier type?

I am still confused by GUID. I was told GUID is 16 bytes long but its character string is 36 characters in length. I must missed something.


回答1:


A GUID is actually an integer type - it's a 128 bit integer (16 bytes).

It's often represented as a string of 36 characters - but the actual value is a 128bit integer value.




回答2:


The reason that it's 36 characters is that a Guid is typically displayed as:

########-####-####-####-############

# = 1 hex character (0-9, A-F)
32 hex chars and 4 hyphens

A quick Google search found this Sybase site for newid that may help you out.




回答3:


Since an integer is 32 bits and a Guid is 128 bits, you could convert your Guid to four 32 bit integers (or two 64 bit integers). See here: Represent a Guid as a set of integers

But it would probably be easier to keep it in the database as a string (or binary) if you don't have a native Guid type. Unfortunately, you won't have the ability to generate them on the database side like in SQL Server, so its use as a primary key would be limited.




回答4:


There is type uniqueidentifier.
varchar(36) should be equivalent in other databases.

To convert a uniqueidentifier value to a char data type:

DECLARE @myid uniqueidentifier
SET @myid = NEWID()
SELECT CONVERT(char(255), @myid) AS 'char'
GO

uniqueidentifier (Transact-SQL)



来源:https://stackoverflow.com/questions/2547947/guid-type-in-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!