Oracle 建立自增列

來源:互聯網
上載者:User

標籤:

類似SqlServer中的識別欄位   identity,Oracle實現同樣的效果有點小複雜額,如下:

 1 --1.建立表 2 create table Student( 3        ID integer not null primary key, 4        Name varchar2(40) not null, 5        Sex integer, 6        Address varchar2(100) 7 )       8 tablespace MyTest_Data;--指明資料表空間 9 10 --2.建立序列11 create sequence SEQ_StuID    12 minvalue 1                   13 start with 1                 14 increment by 1               15 nomaxvalue                   16 nocache;                     17 18 --3.建立觸發器19 create OR REPLACE TRIGGER Trigger_StuID 20    BEFORE insert21    ON Student 22    FOR EACH ROW23 BEGIN24      if inserting then                                          25        if :NEW."ID" is null then                                26           select SEQ_StuID.nextval into :NEW."ID" from dual; 27        end if;                                                  28     end if;         29 END;30 31 --4.啟用觸發器32 alter table "admin"."Student" --使用者.表名33 enable all triggers;34 35 --5.插入資料36 insert into Student(NAME,SEX,ADDRESS) values(‘張三‘,1,‘第七街區‘);37 38 --可以使用【序列名.newxtval】查看下一個要使用的序號39 --注意:以下語句執行一次會按指定的增量增加40 select SEQ_StuID.nextval from dual

 

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.