In alcuni scenari è possibile far rispettare un limite a livello di tabella usando le viste materializzate:
create table tq84_t ( user_id number, foo varchar2(10), constraint pk_tq84_t primary key (user_id, foo) ); create materialized view log on tq84_t; create materialized view tq84_mv refresh on commit as select user_id, count(*) cnt from tq84_t group by user_id; alter table tq84_mv add constraint check_max_2_registrations check (cnt < 3); Come funziona
Con questa vista materializzata, Oracle valida il vincolo al momento del COMMIT:
insert into tq84_t values (1, 'a'); insert into tq84_t values (1, 'b'); commit; Questa operazione va a buon fine. La seguente invece no:
insert into tq84_t values (1, 'c'); commit; L'operazione fallisce con:
ORA-12008: error in materialized view refresh path ORA-02290: check constraint (META.CHECK_MAX_2_REGISTRATIONS) violated Il prezzo da pagare è che il vincolo viene verificato solo in fase di commit e non è immediato: la vista deve essere rinfrescata e il messaggio di errore è decisamente poco user-friendly.