sql server - Best way to enforce this data-integrity constraint? -


i have 3 tables (lets call them foo, bar , baz.

tables:

foo

  • fooid

bar

  • barid
  • fooid

baz

  • bazid
  • barid
  • anothervalue

obviously foreign keys make each baz associated bar, , hence, associated foo. want ensure each set of baz same "anothervalue" associated foo's unique

for instance, if had

foos (1, 2, 3) bars ((10, 1), (11, 1), (12, 1), (13, 2)) bazs ((100, 10, "a"), (101, 10, "b"), (102, 13, "a"), (104, 11, "b")) 

this should blocked because baz 104 , baz 101 both have anothervalue "b" , foo 1.


options have thought of (in order of current preference)

indexed view

i create view on these 3 tables , put unique index on 2 columns

computed column

add fooid computed column baz. add index on anothervalue , fooid.

check constraints

i'm pretty sure can added , work. haven't used check constraints much, , i'm not sure if best way this.

trigger

this seems ugly me.

i'm not sure understand question, seems want carry fooid down baz table , add alternate key (unique contraint) on fooid,anothervalue.

baz •bazid •barid •fooid •anothervalue 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -