Oracle系列:(24)序列

什么是序列【Sequence】

我們提供的服務(wù)有:成都網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、外貿(mào)營銷網(wǎng)站建設(shè)、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、銀海ssl等。為近1000家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的銀海網(wǎng)站制作公司

(1)類似于MySQL中的auto_increment自動增長機(jī)制,但Oracle中無auto_increment機(jī)制

(2)是oracle提供的一個(gè)產(chǎn)生唯一數(shù)值型值的機(jī)制

(3)通常用于表的主健值

(4)序列只能保證唯一,不能保證連續(xù)

     聲明:oracle中,只有rownum永遠(yuǎn)保持從1開始,且繼續(xù)

(5)序列值,可放于內(nèi)存,取之較快

 

題問:為什么oracle不直接用rownum做主健呢?

rownum=1這條記錄不能永遠(yuǎn)唯一表示SMITH這個(gè)用戶

但主鍵=1確可以永遠(yuǎn)唯一表示SMITH這個(gè)用戶

主鍵的目的就是為了唯一地標(biāo)識一條記錄,而rownum無法實(shí)現(xiàn)唯一標(biāo)識某一條記錄。

為什么要用序列

(1)以前我們?yōu)橹鹘≡O(shè)置值,需要人工設(shè)置值,容易出錯(cuò)

(2)以前每張表的主健值,是獨(dú)立的,不能共享

為emp表的empno字段,創(chuàng)建序列emp_empno_seq,

create sequence 序列名
create sequence emp_empno_seq;

刪除序列emp_empno_seq,drop sequence 序列名

drop sequence emp_empno_seq;

查詢emp_empno_seq序列的當(dāng)前值currval和下一個(gè)值nextval,第一次使用序列時(shí),必須選用:序列名.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,默認(rèn)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;

不行,會報(bào)錯(cuò)

Oracle系列:(24)序列

但是可以在創(chuàng)建序列的時(shí)候 ,指定起始值和增長值

Oracle系列:(24)序列

有了序列后,還能為主健手工設(shè)置值嗎?

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

可以

Oracle系列:(24)序列

(講課內(nèi)容)

刪除表,會影響序列嗎?

你無法做insert操作

刪除序列,會影響表嗎?

表真正亡,序列亡

【存疑:我做了試驗(yàn),徹底刪除emp表之后,還能繼續(xù)使用emp_empno_seq的nextval和currval】

在hibernate中,如果是訪問oracle數(shù)據(jù)庫

(1)是否需要底層數(shù)據(jù)庫支持

identity需要底層數(shù)據(jù)庫支持auto_increment。在MySQL數(shù)據(jù)庫中,需要設(shè)置表的主鍵字段為自增長。

而increment和uuid不需要底層數(shù)據(jù)庫支持、不需要設(shè)置主鍵字段為自增長。

(2)是否支持多線程并發(fā)操作?

increment只能單線程訪問,多線程不行。

identity和uuid都支持并發(fā)操作。

(3)適用場景

如果只使用(專用)oracle數(shù)據(jù)庫,可以使用sequence,Hibernate會自動在Oracle數(shù)據(jù)庫中創(chuàng)建一個(gè)序列。

如果不確定使用oracle、mysql、sqlserver,可以使用native,它是一個(gè)通用的變量。一般都會使用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.

本文名稱:Oracle系列:(24)序列
網(wǎng)站網(wǎng)址:http://muchs.cn/article40/iioeho.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、響應(yīng)式網(wǎng)站網(wǎng)站維護(hù)、網(wǎng)頁設(shè)計(jì)公司小程序開發(fā)、定制開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司