且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将自动增量主键添加到oracle中的现有表

更新时间:2023-09-22 22:28:38

假设您的表称为t1,而主键称为id
首先,创建序列:

Say your table is called t1 and your primary-key is called id
First, create the sequence:

create sequence t1_seq start with 1 increment by 1 nomaxvalue; 

然后创建一个触发器,该触发器在插入时递增:

Then create a trigger that increments upon insert:

create trigger t1_trigger
before insert on t1
for each row
   begin
     select t1_seq.nextval into :new.id from dual;
   end;