开发者

GUID type in database

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 开发者_如何学JAVAmust missed something.


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.


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.


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.


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜