我正在尝试将主键和外键添加到嵌套表中,努力知道如何。
这就是我所拥有的;
create or replace type profile as object
(
id VARCHAR2(10), --- Suppose to be Primary Key
userID VARCHAR2(10) --- Suppose to be Foreign Key for user table
);
create or replace type profile_nest as table of profile;
CREATE OR REPLACE TYPE user_t UNDER group_T
(profile profile_nest_ty,);
CREATE TABLE user OF user_t
(id NOT NULL,
PRIMARY KEY (id),
nested table profile store as profile_storage_tbl;
现在问题出在这一部分,试图做一个外键 -
alter table profile_storage_tbl add CONSTRAINT fk_userID FOREIGN KEY (userID)
REFERENCES user(id);
给出了这个错误 -
*在命令的第3行开始出错:
alter table profile_storage_tbl add CONSTRAINT fk_userID FOREIGN KEY(userID) 参考用户(id)
错误报告:
SQL错误:ORA-30730:嵌套表列上不允许引用约束 30730. 00000 - “嵌套表列上不允许引用约束”
*原因:尝试在嵌套上定义参照约束 表栏。
操作:不要在嵌套表列上指定参照约束。
答案 0 :(得分:1)
要么,要创建2个单独的表profile_storage_tbl
和user
,并在它们之间使用外键或创建profile_storage_tbl
作为user
表中的嵌套表。尝试两者都没有意义。 (实际上嵌套表对我来说没什么意义,期间 - 但这是另一回事!)
答案 1 :(得分:1)
正如异常文本所说,不允许在嵌套表列上创建外键约束(Oracle 11)。
此处描述了一种解决方法:http://ksun-oracle.blogspot.com/2011/05/foreign-key-on-nested-table.html。但是无法保证,这将适用于下一个oracle版本。
答案 2 :(得分:0)
在场景后面,oracle将创建两个表profile_storage_tbl和user,而profile_storage_tbl在用户上有一个外键。 你可以自己做这件事,有利于更好地控制关系(也可以到其他表)。