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
这应该可以解决问题!
这篇关于自增唯一标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:自增唯一标识符


- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 更改自动增量起始编号? 2021-01-01
- SQL 临时表问题 2022-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01