gpt4 book ai didi

oracle - 可选外键,其中之一必须是强制的 - 如何?

转载 作者:行者123 更新时间:2023-12-02 00:48:16 24 4
gpt4 key购买 nike

我有一个表,它有两个可选的外键,每个外键对应一个不同的表,并且必须填写其中一个外键,但哪一个外键并不重要。我正在考虑使用触发器来强制执行此“约束”,但这样做感觉不对。我无法重新设计表格,所以我只能坚持下去。

我们正在使用 Oracle 10g

有更好的方法吗?

编辑:我不小心遗漏了一些信息。至少必须填写一栏,并且只能填写一栏。

最佳答案

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

 alter table t add constraint c check (col1 is not null or col2 is not null)

或者如果它们是互斥的:

 alter table t add constraint c check ((col1 is not null and col2 is null
or (col2 is not null and col1 is null))

关于oracle - 可选外键,其中之一必须是强制的 - 如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7675033/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com