views:

29

answers:

2

Hello, I have a table

  CREATE TABLE [dbo].[user](
    [userID] [int] IDENTITY(1,1) NOT NULL,
    [fcode] [int] NULL,
    [scode] [int] NULL,
    [dcode] [int] NULL,
    [name] [nvarchar](50) NULL,
    [address] [nvarchar](50) NULL,
 CONSTRAINT [PK_user_1] PRIMARY KEY CLUSTERED 
(
    [userID] ASC
)
) ON [PRIMARY]

GO

how can add unique constraint for columns fcode, scode, dcode with t-sql and/or management studio? fcode, scode, dcode must be unique together.

+2  A: 
CREATE TABLE [dbo].[user](
    [userID] [int] IDENTITY(1,1) NOT NULL,
    [fcode] [int] NULL,
    [scode] [int] NULL,
    [dcode] [int] NULL,
    [name] [nvarchar](50) NULL,
    [address] [nvarchar](50) NULL,
 CONSTRAINT [PK_user_1] PRIMARY KEY CLUSTERED 
(
    [userID] ASC
),
CONSTRAINT [UQ_codes] UNIQUE NONCLUSTERED
(
    [fcode], [scode], [dcode]
)
) ON [PRIMARY]
devmake
+5 Dam thats cool :) didnt know it was possible!
Jimbo
+1  A: 

If you already have your table in the database, you can always add a unique constraint later on:

ALTER TABLE dbo.User
  ADD CONSTRAINT ucCodes UNIQUE (focde, scode, dcode)
marc_s