自动创建indentity(1,1)但使用GUID而不是整数

时间:2015-12-24 19:37:54

标签: sql identity guid

一个巧妙的事情是将 identity(2,3)放在列上以获取计数器和索引的唯一值。我想知道是否,以及在这种情况下如何使用guid做同样的事情。

因此,我没有使用 newid()插入一行,而是为我创建了一行。

如果没有办法做到这一点,我想知道其背后的技术原理。如果有的话,当然。

create table Records (
  Id int identity(3,14) primary key,
  Occasion datetime not null,
  Amount float not null,
  Mileage int not null,
  Information varchar(999) default ' '
)

1 个答案:

答案 0 :(得分:1)

由于 lazy 高效@MartinSmith发表评论(但没有费心去制定全尺寸的回复,迫使我为他做这件事,呵呵),我们可以使用 default < / em>关键字来实现所请求的行为,如下所示。

create table Records (
  Id uniqueidentifier primary key default newid(),
  Occasion datetime not null,
  Amount float not null,
  Mileage int not null,
  Information varchar(999) default ' '
)

如果说懒惰的马丁愿意,他可能会复制这个答案的内容并将其作为他自己的回复发布,因为这是他的建议,我只是他的打字机。鉴于他的代表就像一个不朽的上帝,我怀疑他会,但我仍然 - 我想完全清楚这一点。