自动增量唯一标识符
基本上我想以与身份类似的方式使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
或者甚至更好:使用
newsequentialid()
作为 UNIQUEIDENITIFER 列的默认值。这将为您提供一系列有点顺序的 GUID。问题是:newsequentialid仅可用作列默认值 - 您不能将其作为函数或任何东西调用。但这似乎符合您的要求。
更新: SQL Server Management Studio 中似乎存在一个已确认的错误,该错误会阻止将
newsequentialid()
指定为交互式表设计器中列的默认值。请参阅:http://social .msdn.microsoft.com/Forums/en-US/sqltools/thread/cad8a4d7-714f-44a2-adb0-569655ac66e6
解决方法:创建表而不指定任何默认值,然后在以下位置键入此 T-SQL 语句一个普通的查询窗口并运行它:
这应该可以解决问题!
Or even better: use the
newsequentialid()
as the default for your UNIQUEIDENITIFER column. That'll give you a somewhat sequential series of GUIDs.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:
That should do the trick!
我猜你的意思是在 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
我认为
更简单
I think
is simplier
使用 NewID() 作为默认值。至少这是您为 SQL Server 所做的事情。
Use NewID() as the default value. At least this is what you would do for SQL Server.