2016-10-01 62 views
1

我已經創建瞭如下的對象關係類型。帶對象類型的PL/SQL PRIMARY KEY

CREATE OR REPLACE TYPE familycar_t AS OBJECT (
make VARCHAR (10), 
model VARCHAR(10), 
year NUMBER (4), 
fuel_type VARCHAR (10)); 
/

CREATE OR REPLACE TYPE sedan_t AS OBJECT (
familycar ref familycar_t, 
cylinder_Size number(10)); 
/

CREATE TABLE familycar OF familycar_t (PRIMARY KEY (make,model)); 

現在我想爲「轎車」表設置主要如下。

CREATE TABLE sedan OF sedan_t (PRIMARY KEY (familycar_t.make,familycar_t.model)); 

但錯誤發生一樣,

Error starting at line : 4 in command - CREATE TABLE sedan OF sedan_t (PRIMARY KEY (familycar_t.make,familycar_t.model)) Error report - SQL Error: ORA-00904: "FAMILYCAR_T"."MAKE": invalid identifier 00904. 00000 - "%s: invalid identifier" *Cause:
*Action:

什麼是它的解決方案嗎?

+1

你有沒有試過:'主鍵(familycar.make,familycar.model)'?也就是說,引用對象,而不是類型。 –

+0

我嘗試了下面的方法,但不正確... CREATE TABLE sedan OF sedan_t(PRIMARY KEY(make%familycar,model%familycar)); CREATE TABLE sedan OF sedan_t(PRIMARY KEY(familycar.make%familycar_t,familycar.model%familycar_t)); CREATE TABLE sedan OF sedan_t(PRIMARY KEY(familycar.make%familycar_t,familycar.model%familycar_t)); – Chamal

回答

1

使用引用的另一種方法是使用繼承:

CREATE OR REPLACE TYPE familycar_t AS OBJECT (
    make VARCHAR (10), 
    model VARCHAR(10), 
    year NUMBER (4), 
    fuel_type VARCHAR (10) 
) NOT FINAL; 
/

CREATE OR REPLACE TYPE sedan_t UNDER familycar_t (
    cylinder_Size number(10) 
); 
/

CREATE TABLE familycar OF familycar_t (PRIMARY KEY (make,model)); 

CREATE TABLE sedan OF sedan_t (PRIMARY KEY (make,model)); 

但是,你並不真正需要的​​表在這種情況下:

INSERT INTO familycar 
SELECT sedan_t('Ford', 'Model-T', 1908, 'Petrol', 4) FROM DUAL UNION ALL 
SELECT familycar_t('Ford', 'Model-A', 1903, 'Petrol') FROM DUAL; 

SELECT f.*, 
     TREAT(VALUE(f) AS sedan_t).cylinder_size AS cylinder_size 
FROM familycar f; 

輸出

MAKE  MODEL   YEAR FUEL_TYPE CYLINDER_SIZE 
---------- ---------- ---------- ---------- ------------- 
Ford  Model-T   1908 Petrol     4 
Ford  Model-A   1903 Petrol   (null) 
+0

是的,先生,這是正確的答案。謝謝 – Chamal

0

您無法爲類型爲REF的屬性創建索引(請參閱here)。 如果REF的範圍有限,只能在REF屬性或列上定義索引。

CREATE OR REPLACE TYPE sedan_t AS OBJECT (
    familycar familycar_t, 
    cylinder_Size number(10) 
    ); 
/
CREATE TABLE sedan OF sedan_t (PRIMARY KEY (familycar.make, familycar.model)); 

Table SEDAN created. 
相關問題