这是一个工作示例。
drop table person_example cascade constraints;
drop table compatibility_example cascade constraints;
create table person_example (person_id number primary key, name varchar2(50),age number);
create table compatibility_example (compat_id number,person_id1 number, person_id2 number);
alter table compatibility_example add
(
constraint fk_comp_per1 foreign key (person_id1) references person_example(person_id),
constraint fk_comp_per2 foreign key (person_id2) references person_example(person_id)
);
insert into person_example (person_id,name,age) values(1,'John',23);
insert into person_example (person_id,name,age) values(2,'Josh',24);
select * from person_example;
PERSON_ID NAME AGE
---------- -------------------------------------------------- ----------
1 John 23
2 Josh 24
2 rows selected.
insert into compatibility_example (compat_id,person_id1,person_id2 ) values(1,1,2);
select * from compatibility_example;
COMPAT_ID PERSON_ID1 PERSON_ID2
---------- ---------- ----------
1 1 2
1 row selected.
set linesize 750
select compat_id,person_id1,person_id2,
p1.name person_1, p1.age age1, p2.name person_2, p2.age age2
from
compatibility_example ce
left outer join person_example p1
on ce. person_id1=p1.person_id
left outer join person_example p2
on ce. person_id2=p2.person_id;
COMPAT_ID PERSON_ID1 PERSON_ID2 PERSON_1 AGE1 PERSON_2 AGE2
---------- ---------- ---------- -------------------------------------------------- ---------- -------------------------------------------------- ----------
1 1 2 John 23 Josh 24
1 row selected.