How do I add a auto_increment primary key in SQL Server database? -
i have table set has no primary key. need add primary key, no null, auto_increment.
i'm working microsoft sql server database. understand can't done in single command every command try keeps returning syntax errors.
edit ---------------
i have created primary key , set not null. however, can't set auto_increment.
i've tried:
alter table tablename modify id nvarchar(20) auto_increment alter table tablename alter column id nvarchar(20) auto_increment alter table tablename modify id nvarchar(20) auto_increment alter table tablename alter column id nvarchar(20) auto_increment
i'm using nvarchar because wouldn't let me set not null under int
it can done in single command. need set identity property "auto number"
alter table mytable add mytableid int not null identity (1,1) primary key
more precisely set named table level constraint
alter table mytable add mytableid int not null identity (1,1), add constraint pk_mytable primary key clustered (mytableid)
see alter table , identity on msdn
Comments
Post a Comment