September 25, 2007 at 8:42 am
Hi All,
I have a table called #PromoMeasure with following fields and types.
--Table definition
CREATE TABLE #PromoMeasure
(
ID int IDENTITY(1,1),PromotionType nvarchar(100) NOT NULL, AdType nvarchar(100), DisplayType nvarchar(100),WhereCriteria nvarchar(1000)
)
--Insert records
INSERT INTO #PromoMeasure(PromotionType, AdType, DisplayType,WhereCriteria)
VALUES('BOGOF','SHELF TALKER',NULL,
'[NewUnitsContribution%] =-52')
GO
INSERT INTO #PromoMeasure(PromotionType, AdType, DisplayType,WhereCriteria)
VALUES('Price Promotion',NULL,NULL,
'[NewUnitsContribution%] =-48')
GO
Using PromotionType,AdType, and DisplayType fields I want to create a Composite key.
As you can see in above inter records these fields may be NULL. Is there anyway to achieve the same purpose?
I want to restrict the user to not add a combination of these field that already exist. In that combination out of those three fields the AdType and DisplayType fields can be NULL.
How to do that?
Please help.
Thanks a million in advance.
Zee
September 25, 2007 at 11:16 am
CREATE UNIQUE INDEX IDX_PromoMeasure_PromotionType_AdType_DisplayType ON #PromoMeasure (PromotionType, AdType, DisplayType)
September 25, 2007 at 2:25 pm
Just keep in mind: NULL must be unique as well.
_____________
Code for TallyGenerator
September 25, 2007 at 2:54 pm
Just remember - you can have a UNIQUE index with nullable fields. It cannot be a primary key however. Primary keys don't allow any nullable fields (even if composite key).
Just trying to make sure you didn't mean PRIMARY KEY by COMPOSITE KEY in the title.
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
September 25, 2007 at 9:46 pm
In other words, the following works...
CREATE TABLE #MyHead
(
ColA INT, ColB INT, ColC INT
)
CREATE UNIQUE INDEX AK_SomeKey ON #MyHead (ColA, ColB, ColC)
INSERT INTO #MyHead (ColA, ColB, ColC)
SELECT 1,Null,Null UNION ALL
SELECT 2,Null,Null UNION ALL
SELECT 2,Null,1 UNION ALL
SELECT 2,1 ,1 UNION ALL
SELECT 2,1 ,Null
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply