Can a sql server table have two identity columns?

前端 未结 9 1853
孤城傲影
孤城傲影 2020-11-27 06:23

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 a

9条回答
  •  暖寄归人
    2020-11-27 07:16

    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.

提交回复
热议问题