473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Calculating default values on insert?

I'm trying to calculate the default values of a column when I insert a
row, based on the values of other columns. It's something I thought
should be simple enough, but I can't seem to figure out how to do it.
Here's a basic example of what I'm trying to do:

CREATE TABLE money (
amount numeric NOT NULL,
currency text NOT NULL DEFAULT 'USD',
currency_per_usd numeric NOT NULL DEFAULT 1.00,
usd_amount NOT NULL DEFAULT (amount / currency_per_usd) -- This fails.
);

INSERT INTO money (amount) VALUES (50.00); -- 50 U.S. dollars

INSERT INTO money (amount, currency, currency_per_usd)
VALUES (
50.00,
'EUR',
1.25
); -- 50 Euros at an exchange rate of 1.25 Euros per U.S. dollar

SELECT amount, usd_amount FROM money;

amount | usd_amount
--------+------------
50.00 | 50.00
50.00 | 40.00
I realize that I could calculate the usd_amount when I do a SELECT:

SELECT amount, (amount / currency_per_usd) AS usd_amount FROM money;
But it would be nice to be able to calculate the default value when I
INSERT the values.

Is there a way of doing this, or should I just create a VIEW that will
do the calculation for me?

--
Robin Munn
rm***@pobox.com
Nov 12 '05 #1
1 3225
On Wed, Nov 05, 2003 at 03:51:36 +0000,
Robin Munn <rm***@pobox.com> wrote:
I'm trying to calculate the default values of a column when I insert a
row, based on the values of other columns. It's something I thought
should be simple enough, but I can't seem to figure out how to do it.
Here's a basic example of what I'm trying to do:
You don't know what order the defaults are going to be done in, so this
approach couldn't work in general. You probably want to do this in a before
trigger anyway in order to maintain integrity between the columns.

CREATE TABLE money (
amount numeric NOT NULL,
currency text NOT NULL DEFAULT 'USD',
currency_per_usd numeric NOT NULL DEFAULT 1.00,
usd_amount NOT NULL DEFAULT (amount / currency_per_usd) -- This fails.
);

INSERT INTO money (amount) VALUES (50.00); -- 50 U.S. dollars

INSERT INTO money (amount, currency, currency_per_usd)
VALUES (
50.00,
'EUR',
1.25
); -- 50 Euros at an exchange rate of 1.25 Euros per U.S. dollar

SELECT amount, usd_amount FROM money;

amount | usd_amount
--------+------------
50.00 | 50.00
50.00 | 40.00
I realize that I could calculate the usd_amount when I do a SELECT:

SELECT amount, (amount / currency_per_usd) AS usd_amount FROM money;
But it would be nice to be able to calculate the default value when I
INSERT the values.

Is there a way of doing this, or should I just create a VIEW that will
do the calculation for me?

--
Robin Munn
rm***@pobox.com

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend


---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: was | last post by:
Hi All, I 'm using Oracle 8i and i have a table table1 with two columns: COL1 not null COL2 DEFAULT 'N' if i make an insert : insert into table1(COL1,COL2) values (1,null)
2
by: Shaun | last post by:
Hi, I have a table called Bookings which has two important columns; Booking_Start_Time and Booking_End_Time. These columns are both of type DATETIME. Given any day how can I calculate how many...
3
by: Phil Sandler | last post by:
All, I have a table with start and end dates/times in it, and would like to be able to calculate the number of hours represented, accounting for overlapping records. Note that I am looking...
5
by: Mike Nolan | last post by:
I notice that 7.4 doesn't do default ordering on a 'group by', so you have to throw in an 'order by' clause to get the output in ascending group order. Is this something that most RDB's have...
12
by: Anthony Robinson | last post by:
Is anyone aware of a function (system or user defined) that will calculate business days? For instance: I have a column in as table called DATE. I want to be able to add five business days to that...
2
by: Viorel | last post by:
Adding new row with default values. In order to insert programmatically a new row into a database table, without direct "INSERT INTO" SQL statement, I use the well-known DataTable.NewRow,...
3
by: Barbara Lindsey | last post by:
I have a case where I am collecting a "Start Date" and an "End Date". I would like to default the "End Date" to the "Start Date" value if only the "Start Date" is entered. I tried setting this as...
2
by: syntego | last post by:
We commonly use triggers to log changes to our main tables to historical log tables. In the trigger, we create a concatenated string of the old values by casting them as follows: ...
0
by: freefony | last post by:
Am building a web site that deals with student registration and using asp.net But am finding it difficult getting the right codes to both calculating the values Of a column in my table, and how...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.