千家信息网

Oracle系列:(24)序列

发表于:2024-09-22 作者:千家信息网编辑
千家信息网最后更新 2024年09月22日,什么是序列【Sequence】(1)类似于MySQL中的auto_increment自动增长机制,但Oracle中无auto_increment机制(2)是oracle提供的一个产生唯一数值型值的机制
千家信息网最后更新 2024年09月22日Oracle系列:(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;


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


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

alter sequence emp_empno_seqincrement by 20;


修改修改emp_empno_seq序列的的increment by属性为5

alter sequence emp_empno_seqincrement by 5;


修改emp_empno_seq序列的start with属性,行吗

alter sequence emp_empno_seqstart with 100;

不行,会报错

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



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

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

可以


(讲课内容)

删除表,会影响序列吗?

你无法做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 identifiers of type 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 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.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 identifier to the object before save() is called. This is the default strategy if no element is specified.

  • foreign

    uses the identifier of another associated object. It is usually used in conjunction with a primary key association.






0