oracle建表id自动增长及触发器无效解答

来源:互联网 发布:js打开新窗口post 编辑:程序博客网 时间:2024/05/21 04:21

oracle建表id自动增长

首先我想说,oracle中没有字段自动增长的功能,那怎么来实现id自动增长呢?


第一种方法:序列

--》1.创建表team

--drop table team;--如果之前有team表先删除
create table team(id integer,name varchar2(20));


--》2.创建自动增长序列

--drop sequence seq_team;--如果序列存在先删除
create sequence seq_team--创建序列sequence
increment by 1   --增长速度为1
start with 1    --开始值为1
nomaxvalue    --没有最大值
nocycle   --不循环
nocache   --不缓存


--》3.添加数据

insert into team value (seq_team.nextval,'  序列 ');


--》4.查询表

select * from team;



2.序列+触发器

--》1.创建表team

--drop table team;--如果之前有team表先删除
create table team(id integer,name varchar2(20));


--》2.创建自动增长序列

--drop sequence seq_team;--如果序列存在先删除
create sequence seq_team--创建序列sequence
increment by 1   --增长速度为1
start with 1    --开始值为1
nomaxvalue    --没有最大值
nocycle   --不循环
nocache   --不缓存



--》3.创建触发器trg_team

--DROP TRIGGER trg_team;--如果存在触发器先删除
create trigger trg_team before insert on team for each row --在创建触发器前对每一行检测是否触发(team你建的表)
begin
select seq_team.nextval into :new.id from dual;--(new:关键字;id指的是表中的id)
end;

--》3.添加数据

insert into team(name)  value ('  序列 ');

--切记team后面的(name)必须要加上,因为表的列有两个;

--不添加系统就不明白,系统就会提示,没有足够的值!


--》4.查询表

select * from team;

--》5.data时间格式

insert into team values(date'2017-1-1');


触发器无效且未通过重新验证





假如你遇到这种情况,

可以看看这里的


begin
select seq_team.nextval into :new.id from dual;
end;


into :new.id    

中的id指的是team表中的id


然后就可以插入数据了











1 0