how can I use GUID datatype in SQL Server 2008?

大兔子大兔子 提交于 2019-12-21 07:04:34

问题


I want to build an employees table using SQL SERVER 2008 , and in my table I want to have an ID for each employee .

I heared about GUID and I kind of understood that its a data type , But I couldn't use it

could you please show me the way to use it ...

by the way , lets say I want something like this :

CREATE TABLE Employees (
ID guid PRIMARY KEY,
Name NVARCHAR(50) NOT NULL
)

How can I do it ?? because I want to benefit from it , but I couldn't find out how to do that


回答1:


It's not called GUID in SQL Server. It's called uniqueidentifier




回答2:


The type is called UNIQUEIDENTIFIER as others have pointed out already. But I think you absolutely must read this article before proceeding: GUIDs as PRIMARY KEYs and/or the clustering key




回答3:


They are called uniqueidentifier




回答4:


Don't use uniqueidentifier as primary key if clustered (which is the default for PKs)

Seriously: use a standard IDENTITY instead




回答5:


Practical demo, FWIW

DECLARE @guid1 AS uniqueidentifier
SET @guid1 = NEWID()
SELECT @guid1



回答6:


You can also consider using NEWSEQUENCIALID as the default value for your ID column as it would be faster than using NEWID() generate the GUIDs.

BUT (from the same link above):-

If privacy is a concern, do not use this function. It is possible to guess the value of the next generated GUID and, therefore, access data associated with that GUID.



来源:https://stackoverflow.com/questions/4100479/how-can-i-use-guid-datatype-in-sql-server-2008

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