Oracle 中的嵌套表主键和外键
我正在尝试向嵌套表添加主键和外键,但很难知道如何操作。
这就是我所拥有的;
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 行开始出错:
更改表 profile_storage_tbl 添加约束 fk_userID 外键(用户 ID) 参考用户(id)
错误报告:
SQL 错误:ORA-30730:嵌套表列上不允许引用约束 30730.00000 - “嵌套表列不允许引用约束”
*原因:尝试定义嵌套的引用约束 表格列。
操作:不要对嵌套表列指定引用约束。
I'm trying to add a primary and foreign key to a nested table, struggling to know how.
This is what I have;
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;
Now the problem is this part, trying to do a foreign key -
alter table profile_storage_tbl add CONSTRAINT fk_userID FOREIGN KEY (userID)
REFERENCES user(id);
Gives this error -
*Error starting at line 3 in command:
alter table profile_storage_tbl add CONSTRAINT fk_userID FOREIGN KEY (userID)
REFERENCES user(id)
Error report:
SQL Error: ORA-30730: referential constraint not allowed on nested table column
30730. 00000 - "referential constraint not allowed on nested table column"
*Cause: An attempt was made to define a referential constraint on a nested
table column.
Action: Do not specify referential constraints on nested table columns.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要么您创建 2 个单独的表
profile_storage_tbl
和user
,并在它们之间使用外键或者您创建profile_storage_tbl
作为user
表内的嵌套表。尝试两者都做是没有意义的。 (事实上,嵌套表对我来说毫无意义,期间 - 但那是另一回事!)Either you create 2 separate tables
profile_storage_tbl
anduser
with a foreign key between them or you createprofile_storage_tbl
as a nested table within theuser
table. It doesn't make sense to try to do both. (In fact nested tables make little sense to me, period - but that's another matter!)正如异常文本所示,不允许在嵌套表列上创建外键约束(Oracle 11)。
这里描述了一种解决方法: http: //ksun-oracle.blogspot.com/2011/05/foreign-key-on-nested-table.html。但不能保证这会在下一个 Oracle 版本中起作用。
It is just as the exception text says, creating a foreign key constraint on nested table columns is not allowed (Oracle 11).
There is sort of a workaround described here: http://ksun-oracle.blogspot.com/2011/05/foreign-key-on-nested-table.html. But there is no guarantee, that this would work on the next oracle release.
在幕后,oracle 将创建两个表 profile_storage_tbl 和 user,而 profile_storage_tbl 在 user 上有一个外键。
您可以自己执行此操作,其优点是可以更好地控制关系(也包括与其他表的关系)。
Behind the scene oracle will create two tables profile_storage_tbl and user whereas profile_storage_tbl has a foreign key on user.
You can do that on your own, with the advatage to have better control over the releations (also to other tables).