溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Oracle系列:(24)序列

發(fā)布時(shí)間:2020-08-02 11:51:07 來(lái)源:網(wǎng)絡(luò) 閱讀:471 作者:lsieun 欄目:數(shù)據(jù)庫(kù)


什么是序列【Sequence】

(1)類似于MySQL中的auto_increment自動(dòng)增長(zhǎng)機(jī)制,但Oracle中無(wú)auto_increment機(jī)制

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

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

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

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

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

 

題問(wèn):為什么oracle不直接用rownum做主健呢?

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

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


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



為什么要用序列

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

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

Oracle系列:(24)序列

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

Oracle系列:(24)序列



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

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

可以

Oracle系列:(24)序列


(講課內(nèi)容)

刪除表,會(huì)影響序列嗎?

你無(wú)法做insert操作


刪除序列,會(huì)影響表嗎?

表真正亡,序列亡

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




在hibernate中,如果是訪問(wèn)oracle數(shù)據(jù)庫(kù)服務(wù)器,那么User.hbm.xml映射文件中關(guān)于<id>標(biāo)簽如何配置呢?

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


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

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

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

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

increment只能單線程訪問(wèn),多線程不行。

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

(3)適用場(chǎng)景

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

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






向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI