我的Java路上那些事兒

          快樂成長(zhǎng)
          posts - 110, comments - 101, trackbacks - 0, articles - 7
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          日歷

          <2013年7月>
          30123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          搜索

          •  

          最新評(píng)論

          Sequence是數(shù)據(jù)庫系統(tǒng)的特性,有的數(shù)據(jù)庫有Sequence,有的沒有。比如Oracle、DB2、PostgreSQL數(shù)據(jù)庫有Sequence,MySQL、SQL Server、Sybase等數(shù)據(jù)庫沒有Sequence。
          定義一個(gè)seq_test,最小值為10000,最大值為99999999999999999,從20000開始,增量的步長(zhǎng)為1,緩存為20的循環(huán)排序Sequence。
          Oracle的定義方法:
          create sequence seq_test
          minvalue 10000
          maxvalue 99999999999999999
          start with 20000
          increment by 1
          cache 20
          cycle
          order;
          Sequence與indentity的基本作用都差不多。都可以生成自增數(shù)字序列。
          Sequence是數(shù)據(jù)庫系統(tǒng)中的一個(gè)對(duì)象,可以在整個(gè)數(shù)據(jù)庫中使用,和表沒有任何關(guān)系;indentity僅僅是指定在表中某一列上,作用范圍就是這個(gè)表。

          一個(gè)表中可以有多個(gè)字段使用sequence字段
          insert into temp(event_id,event_priority,event_status) values(sequence1.nextval, sequence1.nextval,sequence1.nextval);

          mysql 實(shí)現(xiàn)sequence

          由于mysql不帶sequence,所以要手寫的,創(chuàng)建一張儲(chǔ)存sequence的表(tb_sequence),然后手動(dòng)插入一條數(shù)據(jù) ,最后自定義一個(gè)函數(shù)來處理要增長(zhǎng)的值。

          1、創(chuàng)建表tb_sequence,用來存放sequence值:

           create table tb_sequence(name varchar(50) not null,current_value int not null,_increment int not null default 1, primary key(name));   
          2 手動(dòng)插入數(shù)據(jù): 
            insert into tb_sequence values('userid',100,2);  
          3、定義函數(shù) _nextval:
          1. DELIMITER //  
          2. create function _nextval(n varchar(50)) returns integer   
          3. begin  
          4. declare _cur int;  
          5. set _cur=(select current_value from tb_sequence where name= n);  
          6. update tb_sequence  
          7.  set current_value = _cur + _increment  
          8.  where name=n ;  
          9. return _cur;  
          10. end;  
          檢驗(yàn)結(jié)果

           

          select _nextval('userid');  




          評(píng)論

          # re: mysql identity 與sequence的區(qū)別 與 mysql 實(shí)現(xiàn) oracle sequence   回復(fù)  更多評(píng)論   

          2014-06-12 11:15 by jie8476343
          在實(shí)際中發(fā)現(xiàn)高并發(fā)下會(huì)出現(xiàn)返回相同的值,請(qǐng)問如何確保第一個(gè)線程執(zhí)行完_nextval,下一個(gè)線程才可以執(zhí)行

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 浙江省| 苏尼特右旗| 齐齐哈尔市| 江达县| 乐昌市| 乌鲁木齐市| 承德县| 普格县| 吉首市| 镇原县| 西城区| 连云港市| 贡山| 西青区| 广州市| 桐乡市| 南川市| 兴业县| 湾仔区| 平远县| 长乐市| 呼伦贝尔市| 丰城市| 阳高县| 阳城县| 天津市| 丽江市| 灵宝市| 吉安县| 台东市| 防城港市| 突泉县| 六安市| 酉阳| 鹿邑县| 江川县| 东乌| 民乐县| 庆安县| 璧山县| 西林县|