Oracle中,何种情况下Sequence被限制使用

Oracle中,何种情况下Sequence被限制使用

1、在使用Oracle序列的currval和nextval时的限制

创建一个序列
create sequence test_seq
minvalue 1
maxvalue 10000000
start with 1
increment by 1
cache 20;

2、在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: 此处不允许序号

3、在查询视图或物化视图时

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: 此处不允许序号

4、带有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: 此处不允许序号

5、有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: 此处不允许序号

6、有UNION, INTERSECT, MINUS操作符的语句不能使用

SQL> 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;
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
intersect
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
minus
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: 此处不允许序号

7、在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: 此处不允许序号

8、在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: 列在此处不允许

9、还有就在check约束中不能使用

本文来源:[url]http://www.55linux.com/oracle/DBA/536.html | 55linux[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: OracleSequence是一种生成唯一数字序列的对象。它通常用于生成主键或唯一标识符,确保数据表的每条记录都有一个唯一的标识符。在使用Sequence时,可以指定起始值、步长和最大值等参数,并且可以通过调用NEXTVAL来获取下一个值。Sequence很常用且易于使用,因此是Oracle数据库重要的一个特性。 ### 回答2: OracleSequence是一种对象,用于生成唯一的数字序列。使用Sequence可以为表的某个字段(通常是主键)提供唯一的值,而不必依赖于外部数据源或手动输入。SequenceOracle数据库的应用非常广泛,因为它可以提高表的性能和数据的完整性。 在Oracle使用Sequence非常简单。首先,需要创建一个Sequence对象,可以采用以下语法: CREATE SEQUENCE sequence_name START WITH start_value INCREMENT BY increment_value MAXVALUE max_value MINVALUE min_value CYCLE | NOCYCLE CACHE cache_size; 其: - sequence_name:Sequence的名称; - start_value:Sequence的初始值,默认为1; - increment_value:每次递增的值,默认为1; - max_value:Sequence的最大值; - min_value:Sequence的最小值; - CYCLE | NOCYCLE:循环/不循环模式,即当Sequence达到最大值时是否重新从最小值开始; - cache_size:缓存的值的数量,默认为20。 创建Sequence之后,就可以在SQL语句使用它了。例如,插入一个新的记录时,可以使用以下语句来生成一个唯一的主键: INSERT INTO table_name (id, name, age) VALUES (sequence_name.NEXTVAL, 'John', 30); 其sequence_name.NEXTVAL表示获取Sequence的下一个值,用于赋值给主键字段id。 Sequence也可以在表定义时创建,例如: CREATE TABLE table_name ( id NUMBER(10) PRIMARY KEY DEFAULT sequence_name.NEXTVAL, name VARCHAR2(20), age NUMBER(3) ); 这样,每次插入一条记录时,id字段就会使用Sequence的下一个值。 总的来说,OracleSequence是一种非常方便的工具,可以提高表的性能和数据的完整性。使用Sequence的前提是必须确定要使用它的字段,在创建表和插入记录时,都要使用Sequence的下一个值来赋值给该字段。如果没有使用Sequence的下一个值,就会导致插入重复的主键值或者数据不完整。 ### 回答3: OracleSequence是一种对象,它可以用来生成一连串连续的数字,通常用于给表的一列赋值。在实际应用Sequence可以用于生成主键,因为主键需要满足唯一性和连续性的要求。 创建Sequence: 创建Sequence可以使用CREATE SEQUENCE语句,如下所示: CREATE SEQUENCE seq_test; 这个语句创建了一个名称为seq_test的Sequence,由于没有指定一些参数,因此Oracle使用默认参数来创建它。 Sequence的属性: Sequence有一些可自定义的属性,可以通过ALTER SEQUENCE语句来修改它们。例如,以下是修改Sequence的最小值和最大值的方法: ALTER SEQUENCE seq_test MINVALUE 0 MAXVALUE 100; 这个语句seq_test的最小值设为0,最大值设为100。 使用Sequence使用Sequence需要在INSERT语句使用NEXTVAL函数,如下所示: INSERT INTO table_name (id, name, address) VALUES(seq_test.NEXTVAL, 'John', 'New York'); 这个语句将向表table_name插入一行数据,其id列使用seq_test生成的连续数字。 Sequence的应用: 除了用于生成主键之外,Sequence还可以用于其他一些应用。例如,可以使用Sequence来替代循环语句生成一系列数字,如下所示: SELECT seq_test.NEXTVAL FROM dual CONNECT BY LEVEL <= 10; 这个语句将生成一系列数字,从seq_test的当前值开始,逐个加1,直到生成10个数字。 总结: OracleSequence是一种非常有用的对象,它可以用于生成连续数字,可用于给表某一列赋值。通过创建和修改Sequence的属性,可以更灵活地控制其生成数字的方式。Sequence还可以用于其他一些应用,例如生成一系列数字。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值