How should I do this (business logic) in Sql Server? A constraint?

Posted by Pure.Krome on Stack Overflow See other posts from Stack Overflow or by Pure.Krome
Published on 2010-04-19T05:08:06Z Indexed on 2010/04/19 5:13 UTC
Read the original article Hit count: 155

Hi folks,

I wish to add some type of business logic constraint to a table, but not sure how / where.

I have a table with the following fields.

  • ID INTEGER IDENTITY
  • HubId INTEGER
  • CategoryId INTEGER
  • IsFeatured BIT
  • Foo NVARCHAR(200)

etc.

So what i wish is that you can only have one featured thingy, per articleId + hubId.

eg.
1, 1, 1, 1, 'blah'  -- Ok.
2, 1, 2, 1, 'more blah' -- Also Ok
3, 1, 1, 1, 'aaa' -- constraint error
4, 1, 1, 0, 'asdasdad' -- Ok.
5, 1, 1, 0, 'bbbb' -- Ok.

etc.

so the third row to be inserterd would fail because that hub AND category already have a featured thingy.

Is this possible?

© Stack Overflow or respective owner

Related posts about sql-server-2008

Related posts about constraints