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

前端 未结 7 1517
爱一瞬间的悲伤
爱一瞬间的悲伤 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

    enter image description here

    1. Open the design table tab
    2. Highlight your two INT fields (Ctrl/Shift+click on the grey blocks in the very first column)
    3. Right click -> Set primary key
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-28 05:47

    In design mode (right click table select modify) highlight both columns right click and choose set primary key

    0 讨论(0)
  • 2020-11-28 05:51

    Highlight both rows in the table design view and click on the key icon, they will now be a composite primary key.

    I'm not sure of your question, but only one column per table may be an IDENTITY column, not both.

    0 讨论(0)
  • 2020-11-28 05:52
    create table myTable 
    (
        Column1 int not null,
        Column2 int not null
    )
    GO
    
    
    ALTER TABLE myTable
        ADD  PRIMARY KEY (Column1,Column2)
    GO
    
    0 讨论(0)
  • 2020-11-28 05:57
    create table my_table (
        id_part1 int not null,
        id_part2 int not null,
        primary key (id_part1, id_part2)
    )
    
    0 讨论(0)
提交回复
热议问题