35

I need to have one column as the primary key and another to auto increment an order number field. Is this possible?

EDIT: I think I'll just use a composite number as the order number. Thanks anyways.

William Hurst
  • 2,231
  • 5
  • 33
  • 54

9 Answers9

43
CREATE TABLE [dbo].[Foo](
    [FooId] [int] IDENTITY(1,1) NOT NULL,
    [BarId] [int] IDENTITY(1,1) NOT NULL
)

returns

Msg 2744, Level 16, State 2, Line 1
Multiple identity columns specified for table 'Foo'. Only one identity column per table is allowed.

So, no, you can't have two identity columns. You can of course make the primary key not auto increment (identity).

Edit: msdn:CREATE TABLE (Transact-SQL) and CREATE TABLE (SQL Server 2000):

Only one identity column can be created per table.

Eugene Yokota
  • 94,654
  • 45
  • 215
  • 319
22

You can use Sequence for second column with default value IF you use SQL Server 2012

--Create the Test schema
CREATE SCHEMA Test ;
GO

-- Create a sequence
CREATE SEQUENCE Test.SORT_ID_seq
    START WITH 1
    INCREMENT BY 1 ;
GO

-- Create a table
CREATE TABLE Test.Foo
    (PK_ID int IDENTITY (1,1) PRIMARY KEY,
    SORT_ID int not null  DEFAULT (NEXT VALUE FOR Test.SORT_ID_seq));
GO

INSERT INTO Test.Foo VALUES ( DEFAULT )
INSERT INTO Test.Foo VALUES ( DEFAULT )
INSERT INTO Test.Foo VALUES ( DEFAULT )

SELECT * FROM Test.Foo 

-- Cleanup
--DROP TABLE Test.Foo
--DROP SEQUENCE Test.SORT_ID_seq
--DROP SCHEMA Test

http://technet.microsoft.com/en-us/library/ff878058.aspx

benkevich
  • 548
  • 5
  • 9
  • Good suggestion, only problem is Truncation of the Table. – GoldBishop Mar 15 '16 at 15:39
  • I'm not sure that Truncation is a problem, if you want to [reset sequence](https://msdn.microsoft.com/en-us/library/ff878572.aspx#code-snippet-7), you can easily do that – benkevich Mar 22 '16 at 11:20
  • Yes but unlike the Identity identifier, you have to either drop the sequence and then re-add or go through and redefine the start for the sequence. For development and/or testing, Sequence is not an ideal implementation. Once you get past the dev/testing stage, it is a massively useful implementation. Just a PITA during the dev-stages – GoldBishop Mar 23 '16 at 17:30
9

Add one identity column and then add a computed column whose formula is the name of the identity column

Now both will increment at the same time

Community
  • 1
  • 1
Simon Powers
  • 99
  • 1
  • 1
3

No it is not possible to have more than one identity column.

The Enterprise Manager does not even allow you to set > 1 column as identity. When a second column is made identity

Also note that @@identity returns the last identity value for the open connection which would be meaningless if more than one identity column was possible for a table.

Only333
  • 31
  • 1
2
create table #tblStudent
(
    ID int primary key identity(1,1),
    Number UNIQUEIDENTIFIER DEFAULT NEWID(),
    Name nvarchar(50)
)

Two identity column is not possible but if you accept to use a unique identifier column then this code does the same job as well. And also you need an extra column - Name column- for inserting values.

Example usage:

insert into #tblStudent(Name) values('Ali')

select * from #tblStudent

Ps: NewID() function creates a unique value of type uniqueidentifier.

Ali Karaca
  • 3,365
  • 1
  • 35
  • 41
1

The primary key doesn't need to be an identity column.

You can't have two Identity columns.

You could get something close to what you want with a trigger...

Joe Ratzer
  • 18,176
  • 3
  • 37
  • 51
  • This is problematic. A primary key does not allow null values. Usually auto-generated keys are left out on inserts. Even if you use a trigger with INSTEAD OF INSERT to generate your primary key it will puke (Tested on SQLSEVER2000) –  Jan 13 '11 at 16:24
  • 1
    @snmcdonald: Don't use SQL Server 2000 in 2011 year. – abatishchev May 29 '11 at 15:40
0

I've just created a code that will allow you inserting two identities on the same table. let me share it with you in case it helps:

create trigger UpdateSecondTableIdentity
On TableName For INSERT
as
update TableName
set SecondIdentityColumn = 1000000+@@IDENTITY
where ForstId = @@IDENTITY;

Thanks,

NiL
  • 287
  • 4
  • 10
0

in sql server it's not possible to have more than one column as identity.

Mladen Prajdic
  • 15,457
  • 2
  • 43
  • 51
-1

A workaround would be to create an INSERT Trigger that increments a counter.

So I have a table that has one identity col : applicationstatusid. its also the primary key. I want to auto increment another col: applicationnumber

So this is the trigger I write.

 create trigger [applicationstatus_insert] on [ApplicationStatus] after insert as 
       update [Applicationstatus] 
       set [Applicationstatus].applicationnumber =(applicationstatusid+ 4000000) 
       from [Applicationstatus] 
       inner join inserted on [applicationstatus].applicationstatusid = inserted.applicationstatusid
Neha Verma
  • 29
  • 1
  • CREATE Sequence below allows you to make a default value that auto-increments. Before 2012 you could use a function that increments a value in a table and returns it (similar to what you have done in your answer). And that could be a default value for a column. Triggers should be avoided. – TamusJRoyce Sep 01 '17 at 20:51