简体   繁体   中英

Oracle constraint within a sequence

I'm trying to create a constraint on a column that it must be within a sequence (ie col_name < seq.maxval )

I tried to use a regular constraint, but the column doesn't have anything to tie to - it is just a sequence, not a column in a table.

Checks can't reference any kind of query, so I don't think that would work either.

ALTER TABLE STE_FILECOLL ADD (
  CONSTRAINT STE_FC_CLFC_REF_STEF_IDFILE
  FOREIGN KEY (CLFILECOLL) 
  REFERENCES ????
  ENABLE VALIDATE
);

I expect there is a way to make sure that the values of a column are within a sequence, but an hour of reading documentation and duckduckgoing have been fruitless, so I turn to here.

In Oracle DB version 12c , a sequence's next value might be set as default for a column :

create table STE_FILECOLL
(
  col0     int default seq1.nextval not null,
  col_name int
);

and then check constraints might be added to provide the desired condition as :

alter table STE_FILECOLL
  add constraint STE_CC_CLFC_REF_STEF_IDFILE
  check (col_name < col0); 

The only sequence pseudocolumns are nextval and currval , so you can't use that kind of syntax. Inline constraints are pretty limited. I think your best bet is to use a trigger.

[...]
select as.maxval into l_maxval from all_sequences where sequence_name = 'my_sequence';

if :new.col_name > l_maxval then
    raise_application_error( -20001, 'too big' );
end if;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM