2012-01-26 29 views
0

我試圖向主表和外鍵添加一個嵌套表,努力知道如何。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); 

給出了這樣的錯誤 -

*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.

回答

1

要麼創建2個獨立的表profile_storage_tbluser與他們之間的外鍵您創建profile_storage_tbl作爲user表中的嵌套表。嘗試這兩種方法都沒有意義。 (事實上​​,嵌套表對我來說沒有什麼意義,但是這是另一回事!)

+0

你好,謝謝你的回覆。我已經創建了profile_storage_tbl作爲用戶表中的嵌套表。問題是,我有另一個表,我需要與profile_storage_tbl嵌套表有關係。所以我需要知道我將如何添加約束。 – user1165419

1

正如異常文本所述,不允許在嵌套表列上創建外鍵約束(Oracle 11)。

這裏描述了一種解決方法:http://ksun-oracle.blogspot.com/2011/05/foreign-key-on-nested-table.html。但是沒有保證,這將在下一個oracle版本上起作用。

+0

謝謝。我想問一下,嵌套表可以爲每行提供主鍵或唯一標識符嗎? – user1165419

+0

是主鍵約束和唯一約束。後者你可以在鏈接的文章中看到。你可以在這裏看到一個主鍵的例子:http://asktom.oracle.com/pls/asktom/f?p = 100:11:0 :::: P11_QUESTION_ID:2318607631616#4040376500346333254 –

0

在幕後oracle將創建兩個表profile_storage_tbl和user,而profile_storage_tbl在用戶上有一個外鍵。 你可以自己做到這一點,優點是有更好的控制權(也適用於其他表)。