Oracle之序列详解

做过web开发的人员基本上都知道,数据库表中的主键值有的时候我们会用数字类
型的并且自增。这样mysql、sql server中的都可以使用工具创建表的时候很容
易实现。但是oracle中没有设置自增的方法,一般情况我们会使用序列和触发器
来实现主键自增的功能。下面这面文章主要介绍序列。    
 
 
  • 1
  • 2
  • 3
  • 4
知识点一:什么是序列?
序列: 是oacle提供的用于产生一系列唯一数字的数据库对象。
 
 
  • 1
知识点二:序列有什么作用?
1.  自动提供唯一的数值
2.  共享对象
3.  主要用于提供主键值
4.  将序列值装入内存可以提高访问效率
 
 
  • 1
  • 2
  • 3
  • 4
知识点三:怎么创建序列?
1、  要有创建序列的权限 create sequencecreate any sequence

2、  创建序列的语法
        CREATE SEQUENCE sequence  //创建序列名称
       [INCREMENT BY n]  //递增的序列值是n 如果n是正数就递增,如果
       是负数就递减 默认是1
       [START WITH n]    //开始的值,递增默认是minvalue 递减是
      // maxvalue

       [{MAXVALUE n | NOMAXVALUE}] //最大值
       [{MINVALUE n | NOMINVALUE}] //最小值
       [{CYCLE | NOCYCLE}] //循环/不循环
       [{CACHE n | NOCACHE}];//分配并存入到内存中




  NEXTVAL 返回序列中下一个有效的值,任何用户都可以引用
  CURRVAL 中存放序列的当前值
  NEXTVAL 应在 CURRVAL 之前指定 ,二者应同时有效

Create sequence seqEmp increment by 1 start with 1 maxvalue 3 minvalue 1
Cycle cache 2;
//先nextval 后 currval
Select seqEmp.nextval  from dual; 
Select seqEmp.currval  from dual;
Cache<max-min/increment



//解释
{
Create 创建
Sequence 序列 seqEmop 序列名称
Increment by 步长
Stat with 1 开始值
Maxvalue  最大值
Minvalue  最小值

Cycle 循环 nocycle 不循环
Cache 缓存   Cache<maxvalue-minvalue/increment by//一般不采用缓
存
Nextvalue 下一个
Currval 当前值

}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
知识点四:实例
//实例应用
//实现id的自动递增
//第一步
create table cdpt(
id number(6),
name varchar2(30),
constraint pk_id primary key(id)
);

Create sequence seq_cdpt
Increment by 1
Start with 1
Maxvalue 999999
Minvalue 1
Nocycle
nocache

insert into cdpt values(seq_cdpt.nextval,’feffefe’);
commit;
select * from cdpt;
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
知识点五:什么时候会出现裂缝(不连续的情况)?

序列在下列情况下出现裂缝:
    1、  回滚
    2、  系统异常
    3、  多个表同时使用同一序列
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
知识点六:怎么修改序列呢?
//修改序列的增量, 最大值, 最小值, 循环选项, 或是否装入内存
alter SEQUENCE sequence  //创建序列名称
       [INCREMENT BY n]  //递增的序列值是n 如果n是正数就递增,如果是负数就递减 默认是1
       [START WITH n]    //开始的值,递增默认是minvalue 递减是maxvalue`这里写代码片`
       [{MAXVALUE n | NOMAXVALUE}] //最大值
       [{MINVALUE n | NOMINVALUE}] //最小值
       [{CYCLE | NOCYCLE}] //循环/不循环
       [{CACHE n | NOCACHE}];//分配并存入到内存中

例如:
    Alter sequence seqEmp maxvalue 5;
    Select seqEmp.nextval from dual;
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
修改注意:
修改序列的注意事项:
1.   必须是序列的拥有者或对序列有 ALTER 权限
2.   只有将来的序列值会被改变
3.   改变序列的初始值只能通过删除序列之后重建序列的方法实现
 
 
  • 1
  • 2
  • 3
  • 4
知识点七:怎么删除序列?
1.   使用DROP SEQUENCE 语句删除序列
2.   删除之后,序列不能再次被引用
 
 
  • 1
  • 2
    <div class="article-bar-bottom" style="height: auto; overflow: hidden;">
                    <div class="tags-box artic-tag-box">
        <span class="label"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">文章标签:</font></font></span>
                    <a data-track-click="{&quot;mod&quot;:&quot;popu_626&quot;,&quot;con&quot;:&quot;oracle&quot;}" class="tag-link" href="http://so.csdn.net/so/search/s.do?q=oracle&amp;t=blog" target="_blank"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">oracle                       </font></font></a><a data-track-click="{&quot;mod&quot;:&quot;popu_626&quot;,&quot;con&quot;:&quot;数据库&quot;}" class="tag-link" href="http://so.csdn.net/so/search/s.do?q=数据库&amp;t=blog" target="_blank"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">数据库                        </font></font></a>
    </div>
                    <div class="tags-box">
        <span class="label"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">个人分类:</font></font></span>
                    <a class="tag-link" href="https://blog.csdn.net/xinghuo0007/article/category/6883573" target="_blank"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">sql server                      </font></font></a>
    </div>
                <div class="article_info_click" style="left: 181px; width: auto; top: 0px; display: none;"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">▼查看关于本篇文章更多信息</font></font></div></div>
        <!-- !empty($pre_next_article[0]) -->
                    <div class="related-article related-article-prev text-truncate">
        <a href="https://blog.csdn.net/xinghuo0007/article/details/72675137">
            <span><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">上一篇</font></font></span><font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> CSS怎么去掉select的下拉箭头样式(转)          </font></font></a>
    </div>
                            <div class="related-article related-article-next text-truncate">
        <a href="https://blog.csdn.net/xinghuo0007/article/details/72791852">
            <span><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">下一篇</font></font></span><font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> java基础之File类详解           </font></font></a>
    </div>
    </div>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值