oracle数据表设置自增ID

1. 创建一张表
create table t_user(
user_id number not null primary key,
user_name varchar2(30),
credits number,
user_password varchar2(32),
last_visit date,
last_ip varchar2(23)
)

2. 为这个表创建序列
create sequence T_USER_SEQ
minvalue 1
maxvalue 999999999999999999999999999
start with 21
increment by 1
cache 20;

3. 创建一个触发器
create or replace trigger t_user_tr
before insert on t_user
for each row
begin
select t_user_seq.nextval into :new.user_id from dual;
end t_user_tr;

4. 测试
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’);
————————————————
版权声明:本文为CSDN博主「may_walkaway」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/sinat_27535209/article/details/52768534

 

你可能感兴趣的:(oracle数据表设置自增ID)