sql server - Multi column foreign key, with hard coded reference value -
i have validationstatus table, following (primary key) columns:
attribute code address invalid address refuted address store address verified email invalid email refuted email store email unknown email valid email verified
is possible establish foreign key between address table , one?
specifically each address has validation status; if hard code text, be:
alter table dbo.addresses add constraint fk_addresses_validationstatus foreign key('address', validationstatus) references dbo.validationstatus(attribute, code)
you cannot that. solution changing design below:
- add identity or unique integer dbo.validationstatus
create foreign key point new integer field
alter table dbo.addresses add constraint fk_addresses_validationstatus foreign key(validationstatus) references dbo.validationstatus(newuniquefield)
add constraint address table limit range of address validation range. sample assume address validation 1,2,3 , 4. create check constraint like
alter table dbo.address add check (validationstatus in (1,2,3,4))
this way use benefit of foreign key.
another way having 2 different status table , use simple fk.
note: in first method, have remember change check constraint if have new status.
another method using combination of character , number a1, a2, a3, a4 address status , e1, e2 , on email status. each new entiry need pick character. in case can have simple fk without having multiple tables.
Comments
Post a Comment