fiberOptics
fiberOptics

Reputation: 7165

How to create two or more Unique Columns in SQL Azure?

I want to select two columns in my table and make them unique but I don't know how to do it in SQL Azure database. As you can see in the image below, it doesn't show any option to modify the table properties, so everything is done using sql queries:
enter image description here

Here is the generated script of the table:

USE [mydbase]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[clientaccess](
    [ID] [bigint] IDENTITY(1,1) NOT NULL,
    [ModuleName] [nvarchar](50) NOT NULL,
    [ClientAuthenticationId] [bigint] NOT NULL,
    [HasAccess] [bit] NOT NULL,
 CONSTRAINT [PK_clientaccess_ID] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON)
)

GO

ALTER TABLE [dbo].[clientaccess]  WITH CHECK ADD  CONSTRAINT [CAI_caID] FOREIGN KEY([ClientAuthenticationId])
REFERENCES [dbo].[clientauthentication] ([ID])
GO

ALTER TABLE [dbo].[clientaccess] CHECK CONSTRAINT [CAI_caID]
GO  

This is the preview where I encountered the problem, it contains duplicate records:
enter image description here

Hope someone understand my explanation.

Upvotes: 2

Views: 2740

Answers (1)

ypercubeᵀᴹ
ypercubeᵀᴹ

Reputation: 115520

Sometimes GUIS have limitations (or not but you haven't discovered yet how all functionalities work). You can always add a unique constraint with ALTER TABLE:

ALTER TABLE [dbo].[clientaccess] 
  ADD CONSTRAINT Module_Client_UQ                --- choose a name
  UNIQUE (ModuleName, ClientAuthenticationId) ;

Upvotes: 7

Related Questions