且构网

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

将自动增量标识添加到不为空的oracle中的现有表中

更新时间:2023-09-24 17:10:22

您不能一步一步完成.相反,

You can not do it in one step. Instead,

  • 更改表并添加列(无主键约束)

  • Alter the table and add the column (without primary key constraint)

ALTER TABLE DEGREE ADD (Ident NUMBER(10));

  • 用满足主键约束(唯一/非空)的数据填充新列,例如喜欢

  • Fill the new column with data which will fulfill the primary key constraint (unique/not null), e.g. like

    UPDATE DEGREE SET Ident=ROWNUM;
    

  • 更改表并将约束添加到列

  • Alter the table and add the constraint to the column

    ALTER TABLE DEGREE MODIFY (Ident PRIMARY KEY);
    

  • 完成此操作后,您可以设置SEQUENCEBEFORE INSERT触发器来自动设置新记录的id值.

    After that is done, you can set up a SEQUENCE and a BEFORE INSERT trigger to automatically set the id value for new records.