您的位置:首页 > 数据库 > Oracle

oracle创建主键自增字段

2013-04-15 21:33 393 查看
创建表
create table LoginUser
(
Pid number(6) primary key,
username varchar(20) not null,
userpwd varchar(20) not null,
usertype varchar(12) not null
);
创建序列
create sequence LoginUser_seq
increment by 1
start with 1
nomaxvalue
nominvalue
nocache
创建触发器其中一定要注意new.pid这个地方
create or replace trigger tr_user
before insert on loginuser
for each row
begin
select LoginUser_seq.nextval into :new.pid from dual;
end;

执行sql插入语句
insert into LoginUser(username,Userpwd,Usertype) values('jack','123','管理员');
insert into LoginUser(username,Userpwd,Usertype) values('jney','123','普通用户');
查看执行结果
select * from loginuser
  
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: