Is it possible to add a logic Constraint to a Foreign Key?

后端 未结 7 1797
闹比i
闹比i 2021-01-05 15:29

I\'ve got two tables and I\'ve added a foreign key constraint. Kewl - works great. Now, is it possible to further constrain that relationship against some data in the parent

7条回答
  •  时光取名叫无心
    2021-01-05 15:58

    You can create a CHECK CONSTRAINT on the column.

    ALTER TABLE Mammals
    ADD CONSTRAINT CHK_AnimalType CHECK (dbo.fnGetAnimalType(animalId) = 1 );
    

    Now you need a function fnGetAnimalType that will return the animalType of the given animalId.

    Here is more info from MSDN.

提交回复
热议问题