Oracle中Sequence使用的限制

來源:互聯網
上載者:User

在使用Oracle序列的currval和nextval時的限制

建立一個序列

create sequence test_seq

minvalue 1

maxvalue 10000000

start with 1

increment by 1

cache 20;

在delete,select,update語句的子查詢中不能使用sequence的值

SQL>  delete from test_jy where test_id <(select test_seq.currval from dual);

delete from test_jy where test_id <(select test_seq.currval from dual)

ORA-02287: 此處不允許序號

SQL>  select * from test_jy where test_id <(select test_seq.currval from dual);

select * from test_jy where test_id <(select test_seq.currval from dual)

ORA-02287: 此處不允許序號

SQL>  update test_jy set test_id=0 where test_id <(select test_seq.currval from dual);

update test_jy set test_id=0 where test_id <(select test_seq.currval from dual)

ORA-02287: 此處不允許序號

在查詢檢視或物化視圖時

SQL> select a.* from test_v a where a.userid<test_seq.currval  ;

select a.* from test_v a where a.userid<test_seq.currval

ORA-02287: 此處不允許序號

帶有distinct操作符的select語句不能使用

SQL> select distinct a.*,test_seq.currval from test_v a ;

select distinct a.*,test_seq.currval from test_v a

ORA-02287: 此處不允許序號

有group by,order by操作的select語句不能使用

SQL>  select  test_jy.*,test_seq.currval from test_jy group by test_jy.test_id;

select  test_jy.*,test_seq.currval from test_jy group by test_jy.test_id

ORA-02287: 此處不允許序號

SQL>  select  test_jy.*,test_seq.currval from test_jy order by test_jy.test_id;

select  test_jy.*,test_seq.currval from test_jy order by test_jy.test_id

ORA-02287: 此處不允許序號

有UNION, INTERSECT, MINUS操作符的語句不能使用

SQL> select  test_jy.*,test_seq.currval from test_jy where test_id=1

2  union

3  select  test_jy.*,test_seq.currval from test_jy where test_id=2;

select  test_jy.*,test_seq.currval from test_jy where test_id=1

union

select  test_jy.*,test_seq.currval from test_jy where test_id=2

ORA-02287: 此處不允許序號

SQL> select  test_jy.*,test_seq.currval from test_jy where test_id=1

2  intersect

3  select  test_jy.*,test_seq.currval from test_jy where test_id=2;

select  test_jy.*,test_seq.currval from test_jy where test_id=1

intersect

select  test_jy.*,test_seq.currval from test_jy where test_id=2

ORA-02287: 此處不允許序號

SQL> select  test_jy.*,test_seq.currval from test_jy where test_id=1

2  minus

3  select  test_jy.*,test_seq.currval from test_jy where test_id=2;

select  test_jy.*,test_seq.currval from test_jy where test_id=1

minus

select  test_jy.*,test_seq.currval from test_jy where test_id=2

ORA-02287: 此處不允許序號

在select語句中的where子句中

SQL> select  test_jy.* from test_jy where test_id<test_seq.currval;

select  test_jy.* from test_jy where test_id<test_seq.currval

ORA-02287: 此處不允許序號

在create table或alter table語句的中default值是不能使用sequence

SQL> alter table test_jy modify test_id number(20) default test_seq.currval;

alter table test_jy modify test_id number(20) default test_seq.currval

ORA-00984: 列在此處不允許

還有就在check約束中不能使用

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.