How is a guid actually stored and sorted/compared in SQL Server?

前端 未结 2 405
长情又很酷
长情又很酷 2021-01-13 10:13

Say I have this guid:

{2A87E3E2-2B6A-4149-9F5A-1B76092843D9}

Does it actually store this an an alpha numeric in the database? (I don\'t think so cause it ha

2条回答
  •  无人及你
    2021-01-13 10:49

    A GUID is stored as binary(16) internally. "Using uniqueidentifier Data" on MSDN tells you this. The { } and - are not part of the value.

    GUIDs can be "sorted" and have greater/lesser comparisons: see the canonical "How are GUIDs sorted by SQL Server?".

    Note: this means they don't sort as binary(16) (unless you CAST I suppose...)

    I can't see why you'd want this in real life (not indexing, I mean real world): about the only use for the "sorting" potential of NEWID is for the random rows tricks of ORDER BY NEWID()

    You can have "ascending" GUIDs (based on the article above) with NEWSEQUENTIALID. However, this "sorting" doesn't continue after a Windows restart = pointless IMO.

提交回复
热议问题