ORACLE没有象SQL SERVER中一样的自增加字段,
要实现只能通过SEQUENCE来实现1.创建序列:
create sequence your_seq
nocycle
maxvalue 9999999999
start with 1;2.使用触发器实现自增:
create or replace trigger your_seq_tri
before insert on your_table1 for each row
declare
next_id number;
begin
select your_seq.nextval into next_id from dual;
:new.id := next_id;
end;
在oracle中,为了方便我常常用触发器及序列结合起来实现,下面就是一个示例,希望对兄弟们有帮助
先建表、再建序列、然后是触发器,最后测试=============================================
--为主键指定序列编号
--2003-10-8 15:53
============================================= conn scott/tiger@powermis drop table foo; create table foo(
id number primary key,
data varchar2(100)); create sequence foo_seq; create or replace trigger bifer_foo_id_pk
before insert
on foo
for each row
begin
select foo_seq.nextval into :new.id from dual;
end;
/ insert into foo(data)
values('Chirstopher'); insert into foo(id,data)
values(5,'Sean'); select * from foo;
要实现只能通过SEQUENCE来实现1.创建序列:
create sequence your_seq
nocycle
maxvalue 9999999999
start with 1;2.使用触发器实现自增:
create or replace trigger your_seq_tri
before insert on your_table1 for each row
declare
next_id number;
begin
select your_seq.nextval into next_id from dual;
:new.id := next_id;
end;
在oracle中,为了方便我常常用触发器及序列结合起来实现,下面就是一个示例,希望对兄弟们有帮助
先建表、再建序列、然后是触发器,最后测试=============================================
--为主键指定序列编号
--2003-10-8 15:53
============================================= conn scott/tiger@powermis drop table foo; create table foo(
id number primary key,
data varchar2(100)); create sequence foo_seq; create or replace trigger bifer_foo_id_pk
before insert
on foo
for each row
begin
select foo_seq.nextval into :new.id from dual;
end;
/ insert into foo(data)
values('Chirstopher'); insert into foo(id,data)
values(5,'Sean'); select * from foo;
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货