2013-02-09 57 views

回答

1
create or replace trigger calc_total 
before insert on your_table 
for each row 
begin 
    :new.total := :new.marks1 + :new.marks2 + :new.marks3; 
end; 
2

如果碰巧你使用的是Oracle 11g,以达到预期的效果,你可以在虚拟列添加到表:

SQL> create table your_table(
    2 marks1 number, 
    3 marks2 number, 
    4 marks3 number 
    5 ) 
    6 ; 

Table created 

SQL> 
SQL> alter table your_table 
    2 add total number generated always as (nvl(marks1, 0)+ 
    3           nvl(marks2, 0)+ 
    4           nvl(marks3, 0) 
    5           ) 
    6 ; 

Table altered 

SQL> insert into your_table(marks1,marks2,marks3) 
    2 values(1,2,3); 

1 row inserted 

SQL> commit; 

Commit complete 

SQL> select * from your_table; 

    MARKS1  MARKS2  MARKS3  TOTAL 
---------- ---------- ---------- ---------- 
     1   2   3   6 
+0

只(干净)的方式来真正去。或低于使用11G的视图,可以做同样的 – Plouf 2013-02-10 18:01:26

相关问题