您好,登錄后才能下訂單哦!
什么是序列【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;
使用序列,向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,默認(rèn)start with是1,alter sequence 序列名
alter sequence emp_empno_seq increment by 20;
修改修改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ò)
但是可以在創(chuàng)建序列的時(shí)候 ,指定起始值和增長(zhǎng)值
有了序列后,還能為主健手工設(shè)置值嗎?
insert into emp(empno) values(9999); insert into emp(empno) values(7900);
可以
(講課內(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 generatorsAll generators implement the interface
|
免責(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)容。