Oracle Sequence 相关知识(收集)

一,简单介绍

    oraclesequence就是所谓的序列号,每次取的时候它会自动增加,一般用在需要按
序列号排序的地方。

1
create sequence
   
你首先要有create sequence或者create any sequence权限,
    create sequence emp_sequence
        INCREMENT BY 1  --
每次加几个
        START WITH 1       --
1开始计数
        NOMAXVALUE       --
不设置最大值
        NOCYCLE              --
一直累加,不循环
        CACHE 10;             --
预分配缓存大小为10

   
一旦定义了emp_sequence,你就可以用CURRVALNEXTVAL
    CURRVAL=
返回sequence的当前值
    NEXTVAL=
增加sequence的值,然后返回sequence
   
比如:
        emp_sequence.CURRVAL
        emp_sequence.NEXTVAL

   
可以使用sequence的地方:
        -
不包含子查询、snapshotVIEW SELECT 语句
        - INSERT
语句的子查询中
        - NSERT
语句的VALUES
        - UPDATE
SET

   
可以看如下例子:
    INSERT INTO emp VALUES
    (empseq.nextval, 'LEWIS', 'CLERK',7902, SYSDATE, 1200, NULL, 20);

    SELECT empseq.currval FROM DUAL;

   
但是要注意的是:
    -  
第一次NEXTVAL返回的是初始值;随后的NEXTVAL会自动增加你定义的INCREMENT BY值,
      
然后返回增加后的值。CURRVAL 总是返回当前sequence的值,但是在第一次NEXTVAL
      
初始化之后才能使用CURRVAL,否则会出错。一次NEXTVAL会增加一次sequence的值,
      
所以如果你在同一个语句里面使用多个NEXTVAL,其值就是不一样的。明白?
    -  
如果指定CACHE值,oracle就可以预先在内存里面放置一些sequence,这样存取的快
      
些。
       cache
里面的取完后,oracle自动再取一组到cache 使用cache或许会跳号, 比如
      
数据库突然不正常down掉(shutdown abort),cache中的sequence就会丢失. 所以可
      
以在create sequence的时候用nocache防止这种情况。

2
Alter sequence
   
你或者是该sequenceowner,或者有ALTER ANY sequence权限才能改动sequence
alterstart值之外的所有sequence参数。如果想要改变start值,必须drop  sequence
re-create。例子:
    ALTER sequence emp_sequence
        INCREMENT BY 10
        MAXVALUE 10000
        CYCLE    --
10000后从头开始
        NOCACHE;

   
影响sequence的初始化参数:
        sequence_CACHE_ENTRIES =
        
设置能同时被cachesequence数目。  
   
   
可以很简单的Drop sequence
    DROP sequence order_seq;

二,英文介绍

PURPOSE: 
    To create a sequence.  A sequence is a database object from which 
    multiple users may generate unique integers.  You can use sequences 
    to automatically generate primary key values. 
 
SYNTAX: 
 
CREATE SEQUENCE [schema.]sequence 
    [INCREMENT BY integer] 
    [START WITH integer] 
    [MAXVALUE integer | NOMAXVALUE] 
    [MINVALUE integer | NOMINVALUE] 
    [CYCLE | NOCYCLE] 
    [CACHE integer | NOCACHE] 
    [ORDER | NOORDER] 
 
where: 
 
schema 
    is the schema to contain the sequence.  If you omit schema, Oracle 
    creates the sequence in your own schema. 
 
sequence 
    is the name of the sequence to be created. 
 
INCREMENT BY 
    specifies the interval between sequence numbers.  This value can be 
    any positive or negative Oracle integer, but it cannot be 0.  If 
    this value is negative, then the sequence descends.  If the 
    increment is positive, then the sequence ascends.  If you omit this 
    clause, the interval defaults to 1. 
 
MINVALUE 
    specifies the sequence's minimum value. 
 
NOMINVALUE 
    specifies a minimum value of 1 for an ascending sequence or -10 
    for a descending sequence. 
 
    The default is NOMINVALUE. 
 
MAXVALUE 
    specifies the maximum value the sequence can generate. 
 
NOMAXVALUE 
    specifies a maximum value of 10 
    for a descending sequence. 
 
    The default is NOMAXVALUE. 
 
START WITH 
    specifies the first sequence number to be generated.  You can use 
    this option to start an ascending sequence at a value greater than 
    its minimum or to start a descending sequence at a value less than 
    its maximum.  For ascending sequences, the default value is the 
    sequence's minimum value.  For descending sequences, the default 
    value is the sequence's maximum value. 
 
CYCLE 
    specifies that the sequence continues to generate values after 
    reaching either its maximum or minimum value.  After an ascending 
    sequence reaches its maximum value, it generates its minimum value. 
    After a descending sequence reaches its minimum, it generates its 
    maximum. 
 
NOCYCLE 
    specifies that the sequence cannot generate more values after 
    reaching its maximum or minimum value. 
 
    The default is NOCYCLE. 
 
CACHE 
    specifies how many values of the sequence Oracle preallocates and 
    keeps in memory for faster access.  The minimum value for this 
    parameter is 2.  For sequences that cycle, this value must be less 
    than the number of values in the cycle. 
 
NOCACHE 
    specifies that values of the sequence are not preallocated. 
 
    If you omit both the CACHE parameter and the NOCACHE option, Oracle 
    caches 20 sequence numbers by default.  However, if you are using 
    Oracle with the Parallel Server option in parallel mode and you 
    specify the ORDER option, sequence values are never cached, 
    regardless of whether you specify the CACHE parameter or the NOCACHE 
    option. 
 
ORDER 
    guarantees that sequence numbers are generated in order of request. 
    You may want to use this option if you are using the sequence 
    numbers as timestamps.  Guaranteeing order is usually not important 
    for sequences used to generate primary keys. 
 
NOORDER 
    does not guarantee sequence numbers are generated in order of 
    request. 
 
    If you omit both the ORDER and NOORDER options, Oracle chooses 
    NOORDER by default.  Note that the ORDER option is only necessary to 
    guarantee ordered generation if you are using Oracle with the 
    Parallel Server option in parallel mode.  If you are using exclusive 
    mode, sequence numbers are always generated in order. 
 
PREREQUISITES: 
    To create a sequence in your own schema, you must have CREATE 
    SEQUENCE privilege. 
 
    To create a sequence in another user's schema, you must have CREATE 
    ANY SEQUENCE privilege.  If you are using Trusted Oracle in DBMS MAC 
    mode, your DBMS label must dominate the creation label of the owner 
    of the schema to contain the sequence. 
 
SEE: 
ALTER SEQUENCE, DROP SEQUENCE 

问题:开始偶把cache 设置为100000000,结果,第二天来了以后,Sequence的当前值,就变成100000000。没有设置的时候,(采用默认值)也是一样,最后就至少设置成nocache了。根据cache的说法,不知道会不会影响速度呢?Need deep study。

三,truncate 与delete。 区别

truncate和不带where子句的delete, 以及drop都会删除表内的数据 。

注意:这里说的delete是指不带where子句的delete语句

TRUNCATE和Delete有以下几点区别

  1、TRUNCATE在各种表上无论是大的还是小的都非常快。如果有ROLLBACK命令Delete将被撤销,而TRUNCATE则不会被撤销。

  2、TRUNCATE是一个DDL语言,向其他所有的DDL语言一样,他将被隐式提交,不能对TRUNCATE使用ROLLBACK命令。

  3、TRUNCATE将重新设置高水平线和所有的索引。在对整个表和索引进行完全浏览时,经过TRUNCATE操作后的表比Delete操作后的表要快得多。

  4、TRUNCATE不能触发任何Delete触发器。

  5、不能授予任何人清空他人的表的权限。

  6、当表被清空后表和表的索引讲重新设置成初始大小,而delete则不能。

  7、不能清空父表。

  TRUNCATE TABLE (schema)table_name Drop(REUSE) STORAGE

  在默认是 Drop STORAGE 当使用Drop STORAGE时将缩短表和表索引,将表收缩到最小范围,并重新设置NEXT参数。REUSE STORAGE不会缩短表或者调整NEXT参数

  在特殊情况下使用 REUSE STORAGE

  一个实际应用的典型例子:你用sqlldr加载一个1000万记录的数据表,但是加载了多一半的时候你发现有问题,这个时候你想清空表重新加载。那么最好 reuse storage ,这样再次加载就不需要再次寻找空闲空间了。

  8、truncate 只能对TABLE,delete 可以是table,view,synonym。

  9、TRUNCATE TABLE 的对象必须是本模式下的,或者有drop any table的权限 而 DELETE 则是对象必须是本模式下的,或被授予 DELETE ON SCHEMA.TABLE 或DELETE ANY TABLE的权限。

 








 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值