Oracle資料庫自增欄位的設定

來源:互聯網
上載者:User

首先:

建立一個sequence,定義好起始值,增值大小,最大值即可。一般自增用到從1開始自增為1的居多。

假定建立的sequence名字為: MY_SEQ

其次:

建立觸發器

CREATE OR REPLACE TRIGGER MY_TRIG
BEFORE INSERT
ON MY_TABLE
FOR EACH ROW
DECLARE
  NEXTVAL INTEGER;
begin
    select MY_SEQ.NEXTVAL into NEXTVAL from dual;
    :NEW.ID := NEXTVAL;
  end;

說明:直接執行上述sql語句,即可建立觸發器

要注意的是,在第二行before insert是插入觸發。如果改為before insert or update則當修改資料的時候也會觸發自增,這就要看具體的需求了。

相關文章

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.