oracle數據庫中為已經存在表的主鍵ID設置自增
阿新 • • 發佈:2018-03-14
oracle數據庫中為已經存在表的主鍵I創建一張表
create table t_(
user_id number not null primary key,
user_name varchar2(30),
credits number,
user_password varchar2(32),
last_visit date,
last_ip varchar2(23)
)
- 為這個表創建序列
create sequence T_SEQ
minvalue 1
maxvalue 999999999999999999999999999
start with 21
increment by 1
cache 20;
- 創建一個觸發器
create or replace trigger ttr before insert on t
for each row
begin
select t_seq.nextval into :new.id from dual;
end t_tr;
- 測試
insert into t_user values(null,’xiaoming’,123,’111’,sysdate,’192.168.37.132’);
insert into t_user values(6,’xiaoming’,124,’111’,sysdate,’192.168.37.132’);
select * from t_user
oracle數據庫中為已經存在表的主鍵ID設置自增