posts - 110, comments - 101, trackbacks - 0, articles - 7
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理
          Sequence是數據庫系統的特性,有的數據庫有Sequence,有的沒有。比如Oracle、DB2、PostgreSQL數據庫有Sequence,MySQL、SQL Server、Sybase等數據庫沒有Sequence。
          定義一個seq_test,最小值為10000,最大值為99999999999999999,從20000開始,增量的步長為1,緩存為20的循環排序Sequence。
          Oracle的定義方法:
          create sequence seq_test
          minvalue 10000
          maxvalue 99999999999999999
          start with 20000
          increment by 1
          cache 20
          cycle
          order;
          Sequence與indentity的基本作用都差不多。都可以生成自增數字序列。
          Sequence是數據庫系統中的一個對象,可以在整個數據庫中使用,和表沒有任何關系;indentity僅僅是指定在表中某一列上,作用范圍就是這個表。

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

          mysql 實現sequence

          由于mysql不帶sequence,所以要手寫的,創建一張儲存sequence的表(tb_sequence),然后手動插入一條數據 ,最后自定義一個函數來處理要增長的值。

          1、創建表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 手動插入數據: 
            insert into tb_sequence values('userid',100,2);  
          3、定義函數 _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;  
          檢驗結果

           

          select _nextval('userid');  




          評論

          # re: mysql identity 與sequence的區別 與 mysql 實現 oracle sequence   回復  更多評論   

          2014-06-12 11:15 by jie8476343
          在實際中發現高并發下會出現返回相同的值,請問如何確保第一個線程執行完_nextval,下一個線程才可以執行

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 成都市| 阿瓦提县| 府谷县| 米泉市| 昭通市| 合水县| 鹿邑县| 洱源县| 鄂伦春自治旗| 东宁县| 休宁县| 乡城县| 石柱| 卓资县| 佛教| 焉耆| 米林县| 台南县| 清水县| 沽源县| 额敏县| 珠海市| 钟祥市| 密山市| 股票| 册亨县| 清远市| 阳城县| 乃东县| 临朐县| 迭部县| 新余市| 高台县| 松江区| 衡阳市| 太仆寺旗| 梧州市| 财经| 涞源县| 四平市| 崇仁县|