How do I make a composite key with SQL Server Management Studio?

前端 未结 7 1562
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-28 05:25

How do I make a composite key with SQL Server Management Studio?

I want two INT columns to form the identity (unique) for a table

7条回答
  •  时光取名叫无心
    2020-11-28 05:45

    here is some code to do it:

    -- Sample Table
    create table myTable 
    (
        Column1 int not null,
        Column2 int not null
    )
    GO
    
    -- Add Constraint
    ALTER TABLE myTable
        ADD CONSTRAINT pk_myConstraint PRIMARY KEY (Column1,Column2)
    GO
    

    I added the constraint as a separate statement because I presume your table has already been created.

提交回复
热议问题