Oracle系列:(24)序列
admin
2023-06-06 04:21:33
0次


什么是序列【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映射文件中关于标签如何配置呢?


   


(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.






相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...