如何在SQL Server 2008中使用GUID数据类型?

时间:2022-09-15 19:47:31

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

我想使用SQL SERVER 2008构建一个employees表,在我的表中我希望为每个员工都有一个ID。

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

我听说GUID,我有点理解它是一种数据类型,但我无法使用它

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

我该怎么做 ??因为我想从中受益,但我无法找到如何做到这一点

6 个解决方案

#1


21  

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

它在SQL Server中不称为GUID。它被称为uniqueidentifier

#2


13  

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

该类型被称为UNIQUEIDENTIFIER,正如其他人已经指出的那样。但我认为你必须在继续之前阅读本文:GUID作为PRIMARY KEYs和/或集群密钥

#3


5  

They are called uniqueidentifier

它们被称为uniqueidentifier

#4


2  

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

如果是clustered,则不要使用uniqueidentifier作为主键(这是PK的默认值)

Seriously: use a standard IDENTITY instead

说真的:改用标准IDENTITY

#5


1  

Practical demo, FWIW

实用演示,FWIW

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

#6


0  

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.

您还可以考虑使用NEWSEQUENCIALID作为ID列的默认值,因为它比使用NEWID()生成GUID更快。

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.

如果担心隐私,请不要使用此功能。可以猜测下一个生成的GUID的值,因此可以访问与该GUID相关联的访问数据。

#1


21  

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

它在SQL Server中不称为GUID。它被称为uniqueidentifier

#2


13  

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

该类型被称为UNIQUEIDENTIFIER,正如其他人已经指出的那样。但我认为你必须在继续之前阅读本文:GUID作为PRIMARY KEYs和/或集群密钥

#3


5  

They are called uniqueidentifier

它们被称为uniqueidentifier

#4


2  

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

如果是clustered,则不要使用uniqueidentifier作为主键(这是PK的默认值)

Seriously: use a standard IDENTITY instead

说真的:改用标准IDENTITY

#5


1  

Practical demo, FWIW

实用演示,FWIW

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

#6


0  

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.

您还可以考虑使用NEWSEQUENCIALID作为ID列的默认值,因为它比使用NEWID()生成GUID更快。

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.

如果担心隐私,请不要使用此功能。可以猜测下一个生成的GUID的值,因此可以访问与该GUID相关联的访问数据。