简体   繁体   English

Oracle中的嵌套表主键和外键

[英]Nested table primary and foreign key in Oracle

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: *在命令的第3行开始出错:
alter table profile_storage_tbl add CONSTRAINT fk_userID FOREIGN KEY (userID) REFERENCES user(id) 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" SQL错误:ORA-30730:嵌套表列30730上不允许引用约束.00000 - “嵌套表列上不允许引用约束”
*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. 操作:不要在嵌套表列上指定参照约束。

Either you create 2 separate tables profile_storage_tbl and user with a foreign key between them or you create profile_storage_tbl as a nested table within the user table. 要么创建2个单独的表profile_storage_tbl要么user之间创建外键, 或者user表中创建profile_storage_tbl作为嵌套表。 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!) (事实上​​,嵌套表对我来说没什么意义,期间 - 但这是另一回事!)

It is just as the exception text says, creating a foreign key constraint on nested table columns is not allowed (Oracle 11). 正如异常文本所说,不允许在嵌套表列上创建外键约束(Oracle 11)。

There is sort of a workaround described here: http://ksun-oracle.blogspot.com/2011/05/foreign-key-on-nested-table.html . 这里描述了一种解决方法: 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版本。

Behind the scene oracle will create two tables profile_storage_tbl and user whereas profile_storage_tbl has a foreign key on user. 在场景后面oracle将创建两个表profile_storage_tbl和user,而profile_storage_tbl在用户上有一个外键。 You can do that on your own, with the advatage to have better control over the releations (also to other tables). 你可以自己做这件事,有利于更好地控制关系(也可以到其他表)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 子表的主键作为Oracle父表中的外键 - Primary Key of Child Table as Foreign Key in Parent Table in Oracle 在oracle中如何在嵌套表中定义外键? - in the oracle how to define a foreign key in the nested table? Oracle 复合主键/外键题 - Oracle composite primary key / foreign key question 如何在oracle查询中识别主键表和外键表列数据类型和数据长度的差异 - How to identify difference in primary key table and foreign key table columns datatype and datalength in oracle query 具有主(外)键和引用的SQL Oracle编码 - SQL Oracle coding with primary(foreign) key and references 在多个表中分配主键和外键 - Assigning primary and foreign key in multiple tables oracle 主键由两个外键组成? 神谕 - Primary key composed of two foreign keys? Oracle oracle查询以提取数据字典信息,包括此外键指向的主键表和列 - oracle query to extract data dictionary information, including primary key table and column that this foreign key is pointing to 外键引用多个主键值(来自一个表)-Oracle SQL PLUS - Foreign key referring to more than one primary key values(from one table) - Oracle SQL PLUS 同一张表中的主键和外键 - Primary Key and Foreign key in same table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM