自增唯一标识符

Autoincrement uniqueidentifier(自增唯一标识符)
本文介绍了自增唯一标识符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我想以与身份类似的方式使用 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).

推荐答案

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

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)......
   )

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

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.

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

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.

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

解决方法:创建您的表而不指定任何默认值,然后在正常查询窗口中输入此 T-SQL 语句并运行它:

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

这应该可以解决问题!

这篇关于自增唯一标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
SSIS: Model design issue causing duplications - can two fact tables be connected?(SSIS:模型设计问题导致重复-两个事实表可以连接吗?)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)
SQL Server Graph Database - shortest path using multiple edge types(SQL Server图形数据库-使用多种边类型的最短路径)
Invalid column name when using EF Core filtered includes(使用EF核心过滤包括时无效的列名)