oracle 實現主鍵自增

來源:互聯網
上載者:User

標籤:number   sequence   建立表   oca   --   row   rem   create   art   

-- 建立表

drop table test;

create table test(id number(10), name varchar2(10));

-- 建立對列

drop sequence seq_id;

create sequence seq_id minvalue 1 nomaxvalue start with 1 increment by 1 nocycle nocache;

/* 

minvalue 1     最小值

nomaxvalue     不設定最大值(由機器決定),或根據表欄位的值範圍設定 maxvalue

start with 1   從1開始計數,數值可變

increment by 1 每次加1,數值可變

nocycle        一直累加,不迴圈

nocache        不建緩衝區,如果建立cache那麼系統將自動讀取cache值個seq,這樣會加快運行速度;

如果當機或oracle死了,那麼下次讀取的seq值將不連貫*/

-- sys 登陸授權

grant create trigger to scott; 

-- 建立觸發器, 注意建立觸發器 “end;” 後面必須加一個斷行符號和“/”,才可以執行; 

create or replace trigger test_id_increment

before insert on test 

for each row 

begin

  select seq_id.nextval into:new.id from dual;

end;

/

-- 測試

select * from test;

insert into test(name) values(‘張三‘);

insert into test values(null, ‘張三‘);

select * from test;

oracle 實現主鍵自增

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.