自动增量唯一标识符

发布于 2024-09-02 06:25:14 字数 131 浏览 2 评论 0原文

基本上我想以与身份类似的方式使用 uniqueidentifier 。我不想向其中插入值,它应该自动插入值,每行都有不同的值。 我无法在 uniqueidentifier 类型的列上设置自动增量(属性“自动增量”设置为 false 并且不可编辑)。

Basically I want to use uniqueidentifier in similar way as identity. I don't want to insert values into it, It should just insert values automatically, different value for each row.
I'm not able to set autoincrement on columns of type uniqueidentifier(the property 'autoincrement' is set to false and is not editable).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

提笔书几行 2024-09-09 06:25:14

或者甚至更好:使用 newsequentialid() 作为 UNIQUEIDENITIFER 列的默认值。这将为您提供一系列有点顺序的 GUID。

CREATE TABLE dbo.YourTable   
   (SerialID UNIQUEIDENTIFIER 
        CONSTRAINT DF_SerialID DEFAULT newsequentialid(),
     .... (other columns)......
   )

问题是:newsequentialid可用作列默认值 - 您不能将其作为函数或任何东西调用。但这似乎符合您的要求。

更新: SQL Server Management Studio 中似乎存在一个已确认的错误,该错误会阻止将 newsequentialid() 指定为交互式表设计器中列的默认值。

请参阅:http://social .msdn.microsoft.com/Forums/en-US/sqltools/thread/cad8a4d7-714f-44a2-adb0-569655ac66e6

解决方法:创建表而不指定任何默认值,然后在以下位置键入此 T-SQL 语句一个普通的查询窗口并运行它:

ALTER TABLE dbo.YourTable
    ADD CONSTRAINT DF_SerialID DEFAULT newsequentialid() FOR SerialID

这应该可以解决问题!

Or even better: use the newsequentialid() as the default for your UNIQUEIDENITIFER column. That'll give you a somewhat sequential series of GUIDs.

CREATE TABLE dbo.YourTable   
   (SerialID UNIQUEIDENTIFIER 
        CONSTRAINT DF_SerialID DEFAULT newsequentialid(),
     .... (other columns)......
   )

Trouble is: newsequentialid is only available as a column default - you cannot call it as a function or anything. But that seems to fit your requirements.

UPDATE: there appears to be an acknowledged bug in SQL Server Management Studio that prevents specifying newsequentialid() as the default for a column in the interactive table designer.

See: http://social.msdn.microsoft.com/Forums/en-US/sqltools/thread/cad8a4d7-714f-44a2-adb0-569655ac66e6

Workaround: create your table without specifying any default, and then type in this T-SQL statement in a normal query window and run it:

ALTER TABLE dbo.YourTable
    ADD CONSTRAINT DF_SerialID DEFAULT newsequentialid() FOR SerialID

That should do the trick!

苏别ゝ 2024-09-09 06:25:14

我猜你的意思是在 SQLServer 而不是 C#...

将列设置为 PRIMARY KEY 和 ROWGUID

RowGuid http://img341 .imageshack.us/img341/8867/sqlserverrowguid.png

I guess you mean in SQLServer and not C#...

Set the column as PRIMARY KEY and ROWGUID

RowGuid http://img341.imageshack.us/img341/8867/sqlserverrowguid.png

卷耳 2024-09-09 06:25:14

我认为

CREATE TABLE dbo.YourTable
(
    SerialID UNIQUEIDENTIFIER PRIMARY KEY DEFAULT newsequentialid()
)

更简单

I think

CREATE TABLE dbo.YourTable
(
    SerialID UNIQUEIDENTIFIER PRIMARY KEY DEFAULT newsequentialid()
)

is simplier

罪#恶を代价 2024-09-09 06:25:14

使用 NewID() 作为默认值。至少这是您为 SQL Server 所做的事情。

Use NewID() as the default value. At least this is what you would do for SQL Server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文