unique key based on 2 columns in SQl Server 2008?

后端 未结 3 1327
余生分开走
余生分开走 2021-02-04 06:42

is it posible to have SQL Server to have a unique key based on 2 columns?

I can have duplicates in both columns but not at the same time:

MfgID  :   Cu         


        
相关标签:
3条回答
  • 2021-02-04 07:22
    CREATE TABLE table1(
      MfgID INTEGER NOT NULL,
      CustNum VARCHAR(255) NOT NULL,
      CONSTRAINT unique_1 UNIQUE (MfgID, CustNum)
    )
    

    OR

    ALTER TABLE table1 ADD CONSTRAINT unique_1 UNIQUE(fgID, CustNum)
    
    0 讨论(0)
  • 2021-02-04 07:35

    Yes, It is called composite primary key.

    CREATE TABLE table1(
      MfgID INTEGER NOT NULL,
      CustNum VARCHAR(255) NOT NULL,
      PRIMARY KEY (MfgID, CustNum)
    )
    
    0 讨论(0)
  • 2021-02-04 07:45
    CREATE UNIQUE NONCLUSTERED INDEX IX_TableName_MfgID_CustNum ON TableName
    (
            MfgID  ,
            Column2
    ) WITH( IGNORE_DUP_KEY = OFF)
    
    0 讨论(0)
提交回复
热议问题