Oracle新手: 触发器的问题

[code="sql"]
create table userInfo
(
user_name varchar2(20) not null, --用户名称
sex number(1) , --性别[code="java"][/code]
identity_card varchar2(20) primary key, --身份证 ,主键(PK)
phone varchar2(50) not null, --联系方式
address varchar2(50) not null, --地址
remark varchar2(255), --备注
account_id varchar2(20) --账户编号,外键(FK)
)

create table accountInfo
(
account_id varchar2(20) primary key, --账户 , 主键(PK)
account_password varchar2(10) not null, --密码
account_balance number not null, --账户余额
open_date date not null --开户日期
)

--建立userInfo用户信息表和accountInfo账户信息表之间的关系
alter table userInfo
add constraint fk_user_account
foreign key(account_id) references accountInfo(account_id);

--创建一个序列用来辅助生成账户信息id
create sequence seq_exacid
start with 1001
increment by 1

--查询信息
select * from userInfo;
select * from accountInfo;

/*

  • 为用户信息表(userInfo)创建触发器,当创建用户时自动为用户开户(accountInfo),
  • 初始金额为0元,默认密码为123456
  • 同时将账户id绑定给用户表 */ create or replace trigger tri_userinfo_insert before insert on userInfo for each row begin insert into accountInfo values('hsbc'||seq_exacid.nextval,'123456',0,sysdate); update userInfo set account_id = 'hsbc'||seq_exacid.currval where identity_card=:NEW.identity_card; end;

--插入数据
insert into userInfo values('Big Brother',0,'421126198805056666','15856568998','China Wuhan','Nothing','')
[/code]
大家帮忙看下,现在这个触发器有点问题,触发器的作用在注释里写了
我向userInfo插入数据后,accountInfo里确实也插入了数据,但是userInfo里的account_id字段值依然为空,也就是说update语句没有执行,是吗?是不是用的before insert 此时userInfo是空的没有数据可更新呢?
然后我改为after insert 再插入数据就报错了,看图
[img]http://dl.iteye.com/upload/attachment/182836/7fcf8495-ae5c-38d9-89ad-ce731a3a9563.png[/img]
如此一来问题该如何解决呢?
我是oracle新手,谢谢指点。

修改你的trigger,直接NEW.account_id赋值而不是用update语句.

[code="java"]
create or replace trigger tri_userinfo_insert
before insert on userInfo
for each row
declare
var_account_id accountInfo.account_Id%type;
begin
--// get valid account id
select 'hsbc' || seq_exacid.nextval into var_account_id from dual;
--// insert into subassembly table
insert into accountInfo values (var_account_id, '123456', 0, sysdate);
--// associate FK
:NEW.account_id := var_account_id;
end;
[/code]

变异表的问题
以前的解决方法是在触发器里面加入
commit;
EXCEPTION
WHEN OTHERS THEN
NULL;
强制提交,就是在触发器的end前面加上这句话,你可以试试

我觉得是锁的问题,建议改成存储过程吧.

Oracle行级触发器是不能同时修改触发表的。这种问题道理上是不该写在触发器里的。而是应该在事务中。纯Oracle的话,用存储过程写。
你一定要使用触发器的话,将
可以使用语句级触发器。