如果b在table1中是主键或者唯一键,那就可以用外键约束,如下:
alter table table2 add constraint fk_table2_table1
foreign key (c) references table1 (b);如果b既不是主键又非唯一键,那就必须用trigger来处理了
create tigger trg_table2_new on table2
before insert for each row
declare
w_cnt integer;
begin select 1
into w_cnt
from table1
where b = :new.c
and rownum = 1;exception when no_data_found then
raise_application_error(-20000,'value c does not exist in table1');
end;
/
alter table table2 add constraint fk_table2_table1
foreign key (c) references table1 (b);如果b既不是主键又非唯一键,那就必须用trigger来处理了
create tigger trg_table2_new on table2
before insert for each row
declare
w_cnt integer;
begin select 1
into w_cnt
from table1
where b = :new.c
and rownum = 1;exception when no_data_found then
raise_application_error(-20000,'value c does not exist in table1');
end;
/
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货