ORACLE給已有的表加自增字段

/**
* 動態給表mtg_room添加自增字段int_id,自動賦值,根據表名和字段名替換[mtg_room],[int_id]
*/
-- 1.新增數字類型字段,按rownum+10000000形成唯一8位數數據填充
alter table mtg_room add int_id INTEGER;
update mtg_room t set t.int_id = rownum + 10000000;
commit;

-- 2.創建自增序列,起始值11000000,已有數據量限制在百萬之內
drop sequence seq_mtg_room;
create sequence seq_mtg_room
minvalue 11000000
maxvalue 99999999
start with 11000001
increment by 1
nocache;

-- 3.創建觸發器自增
create or replace trigger mtg_room_test_id
  before insert on mtg_room   --mtg_room 是表名
  for each row
begin
    select seq_mtg_room.nextval into :new.int_id from dual;
end mtg_room_test_id;

-- 4.插數據測試
--INSERT INTO mtg_Room(id,Room_Name,Basic_Situation) values('1004','會議室1004','坐席200');
select * from mtg_room;

-- ***如遇異常,撤銷更改****
-- 移除字段 int_id,序列、觸發器
/*
alter table mtg_room drop column int_id;
drop sequence seq_mtg_room;
drop trigger mtg_room_test_id;
*/

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章