
drop table detail;
drop table master;
create table master(id     varchar2(2));
create table detail(id     number,
                    m_id   varchar2(2));
alter table master add constraint pkm_id primary key (id);
alter table detail add constraint pkd_id primary key (id);
alter table detail add constraint fk_mtab_m_id foreign key (m_id) references master (id);

create or replace trigger bi_detail
 before insert
 on detail
 for each row
declare
  l_cnt number;
begin
  select count(*) into l_cnt from detail;
  if l_cnt >= 5 then
    raise_application_error(-20101,'Maximum number of records exceeded');
  end if;
end;
/

insert into master values ('M1');
insert into detail values (1,'M1');
insert into detail values (2,'M1');
insert into detail values (3,'M1');
insert into detail values (4,'M1');
insert into detail values (5,'M1');
commit;
