Is there any way of constraining a column (say "ID") to follow a created sequence (say "ID_SEQ")?
Without an automatic constraint, manual inserts might throw the entire sequence out-of-whack. What can be done to fix this? Just call NextVal twice?
Is there any way of constraining a column (say "ID") to follow a created sequence (say "ID_SEQ")?
Without an automatic constraint, manual inserts might throw the entire sequence out-of-whack. What can be done to fix this? Just call NextVal twice?
You can use a trigger to accomplish this. Example:
create or replace trigger product_insert before insert on product for each row begin
select id_seq.nextval
into :new.product_id
from dual;
end;
There is no way to create a declarative constraint for this sort of thing. You could, of course, create a BEFORE INSERT row-level trigger that would automatically populate the key based on the sequence (ignoring any value that was provided if you're concerned about ad hoc inserts that don't use the sequence).
If you are in a situation where you have to increment a sequence because someone's ad hoc inserts created rows with keys greater than the current value of the sequence, your two options are