As mentioned by TT, you can only have a single identity column and only a single primary key in a table.
For SQL Server 2012 and above, You can however use a SEQUENCE
if you need 2 or more auto increment columns. You can then create a PRIMARY KEY
on IDENTITY
column and a UNIQUE
constraint on the column with sequence as default
CREATE SEQUENCE mainseq START WITH 1 INCREMENT BY 1 ;
GO
create table table1 (
id1 int IDENTITY(1,1) NOT NULL PRIMARY KEY,
id2 bigint not null constraint DF_table1_id2 default next value for mainseq,
data varchar(20) not null
)
GO
INSERT INTO table1(data) VALUES('row 1');
INSERT INTO table1(data) VALUES('row 2');
SELECT * FROM table1;
Note that the column with sequence default behaves in differently than an identity. you cannot directly insert into an identity column (except when identity_insert
is on) however you can manually insert / update the column being defaulted by a sequence.