在oracle中,当我们要让主键或者序号自增,然而oracle中没有像mysql那样有自增简单语句。但是我们可以通过序列与触发器来实现主键或者序列自增。
创建表
create table bank(
userId varchar2(10),
userName varchar2(10),
userPassword varchar2(20),
userMoney number(10,2),
constraint pk_userId PRIMARY KEY(userId)
);
序列
create sequence seq_bank
increment by 1
start with 1
nomaxvalue
nocycle
nocache;
触发器
create or replace trigger tri_bank
before insert on bank
for each row
begin
select seq_bank.nextval into:new.userId from dual;
end;
commit;(别忘记提交事务)
添加数据后查询
insert into bank (userName,userpassword,usermoney) values ('张三','123456','1230.45');
insert into bank (userName,userpassword,usermoney) values ('张四','123456','1230.45');
select * from bank;
网友评论