Oracle 建立主鍵自增表示例代碼_oracle

來源:互聯網
上載者:User

前言

本篇文章給大家主要介紹的是在oracle中如果建立自增長表的方法,這裡要用到序列。下面話不多說,我們來看範例程式碼。

範例程式碼

create table tb_student(  id         NUMBER(10)      not null,  createtime     DATE         not null,  constraint PK_tb_student primary key (id));comment on table "tb_student" is'學生表';comment on column "tb_student"."id" is'主鍵id';comment on column "tb_student"."createtime" is'建立時間';--建立序列create sequence seq_tb_studentminvalue 1nomaxvaluestart with 1increment by 1nocycle  --一直累加,不迴圈nocache; --建立觸發器,如果insert語句不指定ID自動插入增長值CREATE OR REPLACE TRIGGER tr_tb_student BEFORE INSERT ON tb_student FOR EACH ROW WHEN (new.id is null)beginselect seq_tb_student.nextval into:new.id from dual;end;

注意:觸發器是非必須的,可以從業務上嚴格要求指定插入值。

注意oracle限制對象名的字元長度不能超過30個字元,所以表名要控制在一定的長度否則後面建立序列可能會超過限制,建議表名控制在27個字元以下。

總結

以上就是Oracle建立主鍵自增表的全部內容,希望本文的內容對大家的學習或者使用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.