转自:http://edu.codepub.com/2010/0821/25258.php
如何让Oracle表字段自动增长的学习例子:
1,订单表order1:
2, --创建订单表
create table order1(
order_id number(30) constraint pk_order_id primary key,
user_id number(30) not null,
pro_id number(30) not null,
p_amount number(10),
pay number(10),
c_number number(30),
pay_id number(5) not null,
state_id number(5) not null
);
--建立外键
alter table order1
add constraint order1_fk1 foreign key (pay_id)references pay(pay_id);
alter table order1
add constraint order1_fk2 foreign key (state_id) references state(state_id);
alter table order1
add constraint order1_fk3 foreign key (pro_id) references product(pro_id);
alter table order1
add constraint order1_fk4 foreign key (user_id) references use(user_id);
3,--自动生成订单序列
create sequence t_order_id_seq
increment by 1
start with 8100001
nomaxvalue
nocycle;
4,--创建触发器
create or replace trigger order_id_trigger
before insert
on order1
for each row
declare
newid number(18,0);
begin
select t_order_id_seq.nextval into newid from dual;
:new.order_id:=newid;
end;
本篇文章来源于:开发学院 http://edu.codepub.com/ 原文链接:http://edu.codepub.com/2010/0821/25258.php