各位大虾,请帮我写个oracle的trigger(100分)

K

kalituo

Unregistered / Unconfirmed
GUEST, unregistred user!
首先申明我是oracle的新手,不知道oracle中是否有类似于sql server中identity字段
的数据类型吗?如果没有我想写一个触发器来实现,不知用pl/sql怎么写?
 
ok,look
1)
create sequence stud_seq
start with 1
increment by 1
nomaxvalue;
2)
create table stud(stud_id number,name char(10),sex char(2));
3)
create or replace trigger stud_trigger
before insert on stud
for each row
declare
newstud_id number;
begin

select stud_seq.nextval into newstud_id from dual;
:new.stud_id:=newstud_id;
end;
 
运行结果
SQL> select * from stud;
STUD_ID NAME SEX
---------- ---------- ---
SQL> insert into stud(name,sex) values('ab','cd');
1 row inserted
SQL> select * from stud;
STUD_ID NAME SEX
---------- ---------- ---
1 ab cd
SQL> insert into stud(name,sex) values('cd','ef');
1 row inserted
SQL> select * from stud;
STUD_ID NAME SEX
---------- ---------- ---
1 ab cd
2 cd ef
SQL>
 
楼上回答的很好,我来晚了
 
可是我运行
3)报非法的trigger类型错误,这是为什么??
 
不会啊,我是在sql-plus中运行通过才copy过来的啊,你做了1、2步没有呢
 
我也学学
honghs:有没有什么办法可以让序列号回零(除了删除重建外)吗?
 
删除重建并不能归零,只有用ORACLE提供的命令....
Have a nice day!
 
多谢各位,接受答案了
 

Similar threads

回复
0
查看
853
不得闲
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
顶部