A constraint that only allows one of two tables to reference a base table

戏子无情 提交于 2019-11-29 07:31:18
gbn

CHECK constraints with UDFs (which is Oded's answer) don't scale well and have poor concurrency. See these:

So:

  • create a new table, say TableA2XY
  • this has the PK of TableA and a char(1) column with a CHECK to allow ony X or Y. And a unique constraint on the PK of A too.
  • tableX and tableY have new char(1) column with a check to allow only X or Y respectively
  • tableX and tableY have their FK to TableA2XY on both columns

This is the superkey or subtype approach

  • all DRI based
  • no triggers
  • no udfs with table access in CHECK constraints.

Yes, this is possible using CHECK constraints.

Apart from the normal foreign key constraint, you will need to add a CHECK constraint on both referencing tables to ensure that a foreign key is not used in the other referencing table.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!