create table ImagenesUsuario
{
idImagen int primary key not null IDENTITY
}
这是行不通的。 我怎样才能做到这一点?
create table ImagenesUsuario
{
idImagen int primary key not null IDENTITY
}
这是行不通的。 我怎样才能做到这一点?
句法简单的变化是所有需要:
create table ImagenesUsuario (
idImagen int not null identity(1,1) primary key
)
通过明确地使用“约束”的关键字,你可以给主键约束特定的名称,而不是依赖SQL Server上进行自动分配一个名称:
create table ImagenesUsuario (
idImagen int not null identity(1,1) constraint pk_ImagenesUsario primary key
)
添加的“分组”关键字是否是有根据您所使用的表的最有意义的(即搜索的特定idImagen平衡和量的写作远远超过其他一些聚类索引表的好处)。
这类似于我们产生我们团队中的脚本。 首先创建表,然后应用PK / FK等方面的限制。
CREATE TABLE [dbo].[ImagenesUsuario] (
[idImagen] [int] IDENTITY (1, 1) NOT NULL
)
ALTER TABLE [dbo].[ImagenesUsuario] ADD
CONSTRAINT [PK_ImagenesUsuario] PRIMARY KEY CLUSTERED
(
[idImagen]
) ON [PRIMARY]
如果您使用T-SQL
,唯一你的代码错误是,你用大括号{}
代替括号()
PS:两个IDENTITY
和PRIMARY KEY
暗示NOT NULL
,这样你就可以忽略,如果你的愿望。