可选外键其中一个必须是强制性的 - 如何?

jwo*_*rin 2 oracle database-design foreign-keys oracle10g

我有一个表有两个可选的外键,每个表都有一个不同的表,其中一个是要填写的,但是哪一个并不重要.我正在考虑使用触发器来强制实施这种"约束",但这样做却感觉不对.我无法重新设计表格,因此我坚持使用它.

我们正在使用Oracle 10g

有一个更好的方法吗?

编辑:我不小心遗漏了一些信息.至少要填写一​​列HAS,并且只能填写一列.

Ton*_*ews 6

使用检查约束.如果他们都可以填充,那么:

 alter table t add constraint c check (col1 is not null or col2 is not null)
Run Code Online (Sandbox Code Playgroud)

或者如果它们是互斥的:

 alter table t add constraint c check ((col1 is not null and col2 is null
                                       or (col2 is not null and col1 is null))
Run Code Online (Sandbox Code Playgroud)