Oracle-外键参考不同表不同的模式
我对在新表上执行参考完整性有问题。
在不同的模式中有不同的表,每个表都有其主要键:
schema1.table1
schema2.table2
schema3.table3
我想创建一个新表<> /strong>在其他信息及其主要ID中,具有“ Reference_schema”列和“ Reference_id”列。 我希望列“引用ID” 能够在相关表上引用ID ,即“ reference_schema” =“ schema1”来引用主键schema1.table1.id。
3张桌子上的主要钥匙在工会中并不唯一。
我尝试了 综合了联合所有视图中的主要键,但是Oracle并未强制执行视图约束。
I have problem with Enforcing Referential Integrity to a new table.
There are different tables in different schemas, each one has its primary key:
schema1.table1
schema2.table2
schema3.table3
I want to create a new table, which among other information and its primary id, has a column "reference_schema" and a column "reference_id".
I want the column "referencing id" to reference the id on the relevant table, that is of the "reference_schema"="schema1" to reference the primary key schema1.table1.id.
The primary keys on the 3 tables, aren't unique in a UNION.
I have tried synthesizing a primary key in a UNION ALL view, but Oracle does not enforce view constraints.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不是不同的架构引起问题,而是您无法创建外键约束的事实,该约束将引用两个(或更多)不同的表。
我的意思是,您 can 使用台外约束语法做到这一点,但这是行不通的。为什么?因为 - 如果在中不存在所有引用表中,则将违反约束。
一个简单的例子是Scott的样本架构(部门编号10,但没有员工拥有该empno):
那么,您做什么可以做什么?使用触发器。这样的东西:
测试:
It is not the different schema that causes problems, but the fact that you can't create a foreign key constraint which would reference two (or more) different tables.
I mean, you can do it, using out-of-line constraint syntax, but that just won't work. Why? Because - if that value doesn't exist in all referenced tables, constraint will be violated.
A simple example is Scott's sample schema (there is department number 10, but no employee has that EMPNO):
So, what can you do? Use a trigger. Something like this:
Testing:
从Oracle 12中,您可以使用虚拟列并将约束放在虚拟列上。
例如,如果您有表:
那么您可以创建表格为:
注意:如果您想从同一模式中的不同表更改为不同模式中的同一表 位置。
到正确的 >
From Oracle 12, you can use virtual columns and put the constraints on the virtual column.
For example, if you have the tables:
Then you can create your table as:
Note: If you want to change from different tables in the same schema to the same table in different schemas then just update the foreign key constraint to point to the correct location.
db<>fiddle here