Can a sql server table have two identity columns?

孤街醉人 提交于 2019-11-26 19:00:58
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.

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

Simon Powers

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

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.

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.

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...

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

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,

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!