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

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -