2016-07-20 11 views
1

Kann man bei Verwendung von SQL Objects eindeutige/Fremdschlüssel für Referenzfelder setzen? Angenommen, ich habe die folgende Tabelle:Oracle SQL Objects - Eindeutiger/Fremdschlüssel für Referenzen

create table MY_TABLE (
    SOME_REFERENCE ref MY_TYPE 
); 

Kann ich einen eindeutigen Schlüssel für eine Referenz festlegen? Etwas wie folgt aus:

alter table MY_TABLE modify (constraint ABC unique(SOME_REFERENCE)) 

Ist es möglich, so etwas wie einzigartig/Fremdschlüssel auf Referenzfelder (Vielleicht Abhilfen durch Verwendung?) Achive (Welche ORA-02329: column of datatype REF cannot be unique or a primary key gibt)

Antwort

1

Es ist die Workaround, aber es ist schwierig.

create type my_type as object 
(id number, v varchar2(20)); 

create table my_type_table of my_type; 

insert into my_type_table values(1,'a'); 

create table MY_TABLE (
    SOME_REFERENCE ref MY_TYPE 
); 

create or replace function test_func(p_ref ref MY_TYPE) return number DETERMINISTIC 
is 
    v_typ MY_TYPE; 
begin 
    SELECT DEREF(p_ref) INTO v_typ FROM DUAL; 
    return v_typ.id; 
end; 

CREATE UNIQUE INDEX fn_idx_un ON MY_TABLE (test_func(SOME_REFERENCE)); 

Und testen.

insert into my_type_table values(1,'a'); 
insert into MY_TABLE SELECT REF(e) FROM my_type_table e; (*1) 
insert into MY_TABLE SELECT REF(e) FROM my_type_table e; (*2) 

* 1 - funktioniert gut, und Zeile wird eingefügt.

* 2 - Ausnahme wird ausgelöst. ORA-00001: unique constraint (FN_IDX_UN) violated

+0

Danke, das ist was ich suche! Gibt es eine Möglichkeit, etwas Ähnliches für Fremdschlüssel zu erreichen? – tobspr