SQL Recipes
A FREE cookbook for SQL queries and examples
Register FAQ Search Today's Posts Mark Forums Read

SQL - Questions and Answers Have a SQL question? Post it here. First do a search to see if someone hasn't already answered it.

Go Back   SQL Recipes a FREE cookbook of SQL queries and examples > SQL queries and examples > SQL - Questions and Answers

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread
  #1 (permalink)  
Old 11-19-2007, 04:41 PM
Unregistered
 
Posts: n/a
Lightbulb ANY dialect question:

creating table where 3rd column should be sum of first two columns


Hi all,
I have a problem on creating a table.
Table: sss(n1 number,n2 number, and sum=n1+n2)
please send ur suggetions soon
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote

ANSWER(S):

  #2 (permalink)  
Old 11-26-2007, 06:59 PM
Dimitar
 
Posts: n/a
Default ANY answer. Re: creating table where 3rd column should be sum of first two columns

Hi,
You could create a trigger such that each time a row is inserted (containing only data for the first two columns), the third column is updated with the calculated sum.

Dimitar
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
  #3 (permalink)  
Old 11-27-2007, 06:32 AM
anna_2k2 anna_2k2 is offline
Junior Member
 
Join Date: Nov 2007
Posts: 2
anna_2k2 is on a distinguished road
Default ANY answer. Re: creating table where 3rd column should be sum of first two columns

hai,
create a view on u r base table and try to update the view in the after insert trigger.This should automatically update table as well.
other wise you will face the problem of mutating trigger.and u will definetly hit either of these problems
ORA-4091 in an INSERT trigger or an UPDATE trigger where you only need access to the :new values
Hitting the ORA-4091 in a DELETE trigger or an UPDATE trigger where you need to access the ld values .

so better take a view and try to update the base table using an after insert trigger with trigger body containing the update statement on the view.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 12-28-2007, 04:19 AM
SnippetyJoe
 
Posts: n/a
Default ANY answer. Re: creating table where 3rd column should be sum of first two columns

Beginning with Oracle 11g it's quite easy.

Code:
SQL> set null "(null)"
SQL> 
SQL> create table t
  2  ( c1 number
  3  , c2 number
  4  , c1_plus_c2 as ( c1 + c2 )
  5  ) ;

Table created.

SQL> 
SQL> 
SQL> insert into t ( c1, c2 ) values ( 1, 2    ) ;

1 row created.

SQL> insert into t ( c1, c2 ) values ( 3, null ) ;

1 row created.

SQL> 
SQL> commit;

Commit complete.

SQL> 
SQL> select * from t ;

        C1         C2 C1_PLUS_C2
---------- ---------- ----------
         1          2          3
         3 (null)     (null)

SQL> 
SQL> update t set c2 = 4 where c2 is null ;

1 row updated.

SQL> commit;

Commit complete.

SQL> 
SQL> select * from t ;

        C1         C2 C1_PLUS_C2
---------- ---------- ----------
         1          2          3
         3          4          7
--
SnippetyJoe
SQL Snippets: Tutorials for Oracle Developers
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Moderation Tools:



All times are GMT. The time now is 05:32 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0
Copyright (c) 2006-2008 SQL Recipes

1 2 3 4 5 6 7 8