Is it possible to a db constraint in for this rule?

Posted by Pure.Krome on Stack Overflow See other posts from Stack Overflow or by Pure.Krome
Published on 2010-03-26T00:25:21Z Indexed on 2010/03/26 0:33 UTC
Read the original article Hit count: 438

Hi folks,

I wish to make sure that my data has a constraint the following check (constraint?) in place

  • This table can only have one BorderColour per hub/category. (eg. #FFAABB)
  • But it can have multiple nulls. (all the other rows are nulls, for this field)

Table Schema

ArticleId INT PRIMARY KEY NOT NULL
HubId TINYINT NOT NULL
CategoryId INT NOT NULL
Title NVARCHAR(100) NOT NULL
Content NVARCHAR(MAX) NOT NULL
BorderColour VARCHAR(7) -- Can be nullable.

I'm gussing I would have to make a check constraint? But i'm not sure how, etc.

sample data.

1, 1, 1, 'test', 'blah...', '#FFAACC'
1, 1, 1, 'test2', 'sfsd', NULL
1, 1, 2, 'Test3', 'sdfsd dsf s', NULL
1, 1, 2, 'Test4', 'sfsdsss', '#AABBCC'

now .. if i add the following line, i should get some sql error....

INSERT INTO tblArticle VALUES (1, 2, 'aaa', 'bbb', '#ABABAB')

any ideas?

© Stack Overflow or respective owner

Related posts about sql-server

Related posts about check-constraints