2013-02-09 56 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

相關問題