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:

  1. add identity or unique integer dbo.validationstatus
  2. create foreign key point new integer field

    alter table dbo.addresses add constraint fk_addresses_validationstatus foreign key(validationstatus) references dbo.validationstatus(newuniquefield) 
  3. 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

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -