设为首页 收藏本站
查看: 1282|回复: 0

[经验分享] Oracle系列:(24)序列

[复制链接]

尚未签到

发表于 2018-9-7 12:49:24 | 显示全部楼层 |阅读模式
  什么是序列【Sequence】
  (1)类似于MySQL中的auto_increment自动增长机制,但Oracle中无auto_increment机制
  (2)是oracle提供的一个产生唯一数值型值的机制
  (3)通常用于表的主健值
  (4)序列只能保证唯一,不能保证连续
  声明:oracle中,只有rownum永远保持从1开始,且继续
  (5)序列值,可放于内存,取之较快
  题问:为什么oracle不直接用rownum做主健呢?
  rownum=1这条记录不能永远唯一表示SMITH这个用户
  但主键=1确可以永远唯一表示SMITH这个用户
  主键的目的就是为了唯一地标识一条记录,而rownum无法实现唯一标识某一条记录。
  为什么要用序列
  (1)以前我们为主健设置值,需要人工设置值,容易出错
  (2)以前每张表的主健值,是独立的,不能共享
  为emp表的empno字段,创建序列emp_empno_seq,
create sequence 序列名create sequence emp_empno_seq;  删除序列emp_empno_seq,drop sequence 序列名
drop sequence emp_empno_seq;  查询emp_empno_seq序列的当前值currval和下一个值nextval,第一次使用序列时,必须选用:序列名.nextval
select emp_empno_seq.nextval from dual;  
select emp_empno_seq.currval from dual;
DSC0000.jpg

  使用序列,向emp表插入记录,empno字段使用序列值
insert into emp(empno) values(emp_empno_seq.nextval);  
insert into emp(empno) values(emp_empno_seq.nextval);
  
insert into emp(empno) values(emp_empno_seq.nextval);
DSC0001.jpg

  修改emp_empno_seq序列的increment by属性为20,默认start with是1,alter sequence 序列名
alter sequence emp_empno_seq  
increment by 20;
DSC0002.jpg

  修改修改emp_empno_seq序列的的increment by属性为5
alter sequence emp_empno_seq  
increment by 5;
  修改emp_empno_seq序列的start with属性,行吗
alter sequence emp_empno_seq  
start with 100;
  不行,会报错
DSC0003.jpg

  但是可以在创建序列的时候 ,指定起始值和增长值
DSC0004.jpg

  有了序列后,还能为主健手工设置值吗?
insert into emp(empno) values(9999);  
insert into emp(empno) values(7900);
  可以
DSC0005.jpg

  (讲课内容)
  删除表,会影响序列吗?
  你无法做insert操作
  删除序列,会影响表吗?
  表真正亡,序列亡
【存疑:我做了试验,彻底删除emp表之后,还能继续使用emp_empno_seq的nextval和currval】  在hibernate中,如果是访问oracle数据库服务器,那么User.hbm.xml映射文件中关于标签如何配置呢?
  
   
  

  (1)是否需要底层数据库支持
  identity需要底层数据库支持auto_increment。在MySQL数据库中,需要设置表的主键字段为自增长。
  而increment和uuid不需要底层数据库支持、不需要设置主键字段为自增长。
  (2)是否支持多线程并发操作?
  increment只能单线程访问,多线程不行。
  identity和uuid都支持并发操作。
  (3)适用场景
  如果只使用(专用)oracle数据库,可以使用sequence,Hibernate会自动在Oracle数据库中创建一个序列。
  如果不确定使用oracle、mysql、sqlserver,可以使用native,它是一个通用的变量。一般都会使用native。
  Hibernate帮助文档
Various additional generators
  All generators implement the interface org.hibernate.id.IdentifierGenerator. This is a very simple interface. Some applications can choose to provide their own specialized implementations, however, Hibernate provides a range of built-in implementations. The shortcut names for the built-in generators are as follows:

  •   increment
      generates>long, short or int that are unique only when no other process is inserting data into the same table. Do not use in a cluster.
  •   identity
      supports>long, short or int.
  •   sequence
      uses a sequence in DB2, PostgreSQL, Oracle, SAP DB, McKoi or a generator in Interbase. The returned>long, short or int
  •   uuid
      Generates a 128-bit UUID based on a custom algorithm. The value generated is represented as a string of 32 hexidecimal digits. Users can also configure it to use a separator (config parameter "separator") which separates the hexidecimal digits into 8{sep}8{sep}4{sep}8{sep}4.
  •   uuid2
      Generates a IETF RFC 4122 compliant (variant 2) 128-bit UUID. The exact "version" (the RFC term) generated depends on the pluggable "generation strategy" used. Capable of generating values as java.util.UUID, java.lang.String or as a byte array of length 16 (byte[16]). The "generation strategy" is defined by the interface org.hibernate.id.UUIDGenerationStrategy.
  •   guid
      uses a database-generated GUID string on MS SQL Server and MySQL.
  •   native
      selects identity,sequence or hilo depending upon the capabilities of the underlying database.
  •   assigned
      lets the application assign an>save() is called. This is the default strategy if no  element is specified.
  •   foreign
      uses the> primary key  association.



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-566111-1-1.html 上篇帖子: Oracle系列:(23)同义词 下篇帖子: Oracle系列:(25)索引
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表