Oracle系列:(24)序列

发布时间:2020-08-02 11:51:07 作者:lsieun
来源:网络 阅读:454


什么是序列【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;

Oracle系列:(24)序列


使用序列,向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);

Oracle系列:(24)序列


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

alter sequence emp_empno_seq
increment by 20;

Oracle系列:(24)序列


修改修改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;

不行,会报错

Oracle系列:(24)序列

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

Oracle系列:(24)序列



有了序列后,还能为主健手工设置值吗?

insert into emp(empno) values(9999);
insert into emp(empno) values(7900);

可以

Oracle系列:(24)序列


(讲课内容)

删除表,会影响序列吗?

你无法做insert操作


删除序列,会影响表吗?

表真正亡,序列亡

【存疑:我做了试验,彻底删除emp表之后,还能继续使用emp_empno_seq的nextval和currval】




在hibernate中,如果是访问oracle数据库服务器,那么User.hbm.xml映射文件中关于<id>标签如何配置呢?

<id name="id" column="id">
   <generator class="increment/identity/uuid/【sequence】/【native】"/>
</id>


(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 identifiers of type longshort 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 identity columns in DB2, MySQL, MS SQLServer, Sybase and HypersonicSQL. The returned identifier is of type long, short or int.

  • sequence

    uses a sequence in DB2, PostgreSQL, Oracle, SAP DB, McKoi or a generator in Interbase. The returned identifier is of type 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.UUIDjava.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 identifier to the object before save() is called. This is the default strategy if no <generator> element is specified.

  • foreign

    uses the identifier of another associated object. It is usually used in conjunction with a <one-to-one> primary key  association.






推荐阅读:
  1. Oracle系列:(32)小结
  2. Oracle系列:(28)PLSQL

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

oracle acle

上一篇:Mysql 5.7主从

下一篇:win10打不开jar文件方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》