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) ) create sequence T_USER_SEQ minvalue 1 maxvalue 999999999999999999999999999 start with 21 increment by 1 cache 20; 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; insert into t_user values(null,'xiaoming',123,'1114',sysdate,'192.168.37.132'); insert into t_user values(null,'xiaoming1',123,'1114',sysdate,'192.168.37.132'); insert into t_user values(null,'xiaoming1',123,'1114',sysdate,'192.168.37.132'); insert into t_user values(null,'xiaoming1',123,'1114',sysdate,'192.168.37.132'); insert into t_user values(null,'xiaoming2',123,'1114',sysdate,'192.168.37.132'); insert into t_user values(null,'xiaoming',123,'1114',sysdate,'192.168.37.132'); insert into t_user values(null,'xiaoming',123,'1114',sysdate,'192.168.37.132'); insert into t_user values(null,'xiaoming',123,'1114',sysdate,'192.168.37.132'); insert into t_user values(null,'xiaoming',123,'1114',sysdate,'192.168.37.132'); insert into t_user values(null,'xiaoming',123,'1114',sysdate,'192.168.37.132'); select * from t_user
第一步:
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 -- 从21开始 increment by 1 -- 每次递增1 cache 20; --制定存入缓存(也就是内存)序列值的个数 默认20
3.创建一个触发器
t_user_tr:随意的名字,不要重复就行
t_user:表名
user_id :自增的id
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,'1114',sysdate,'192.168.37.132'); insert into t_user values(null,'xiaoming1',123,'1114',sysdate,'192.168.37.132'); insert into t_user values(null,'xiaoming1',123,'1114',sysdate,'192.168.37.132'); insert into t_user values(null,'xiaoming1',123,'1114',sysdate,'192.168.37.132'); insert into t_user values(null,'xiaoming2',123,'1114',sysdate,'192.168.37.132'); insert into t_user values(null,'xiaoming',123,'1114',sysdate,'192.168.37.132'); insert into t_user values(null,'xiaoming',123,'1114',sysdate,'192.168.37.132'); insert into t_user values(null,'xiaoming',123,'1114',sysdate,'192.168.37.132'); insert into t_user values(null,'xiaoming',123,'1114',sysdate,'192.168.37.132'); insert into t_user values(null,'xiaoming',123,'1114',sysdate,'192.168.37.132');
5.查看结果
select * from t_user
这样就可以 本人亲试过;
oralce的自增id