Skip to Main Content
  • Questions
  • Recommendations for using default and automatic column values

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Terry.

Asked: September 29, 2003 - 11:07 pm UTC

Last updated: September 30, 2003 - 11:46 am UTC

Version: 9.2.0

Viewed 1000+ times

You Asked

Try as I might and after searching this site, checking the Oracle docs, several books (three from Tom Kyte), the sample Oracle schemas, I was unable to find good examples of how to set default values and calculate automatic columns. Here are some of the defaults that I was able to do:

Literal (okay this is easy)
ex: region varchar2 (4) default 'CO'
Current Date (again, pretty easy)
ex: date_entered date default sysdate
Current Day of week (month, year, etc.)
ex: current_day to_char(sysdate, 'Day')
Current Hour
ex: current_hour numeric(1) default to_number(to_char(sysdate,'HH'))
Current Timestamp
ex: rec_timestamp timestamp default systimestamp
Next sequence (uset the following PL/SQL code within a trigger:
ex: select seqno.nextval into :new.seqno from dual;

Are there any more good ones that I'm missing? Expecially ones using DECODE or more complex expressions.

And what's the best practice for calculating the tot_amt from price and qty columns and updating the table via an insert trigger since the following is invalid because defaults can not reference other columns?

column price numeric(10)
column qty numeric (10)
column tot_amt numeric (10) default price*qty /* bad syntax */

And what's the best practice for setting a default column in an insert trigger by using the value of an entered column to lookup a value from another table?

Many thanks.

Terry O'Brien







and Tom said...


Defaults are like "beauty" i guess - all in the eye of the beholder. I don't see them as being "very interesting". I don't have any other examples then you do and in fact, I would find your list "too long" really.

They are as simple as they look to me -- literals, dates, complex values via triggers (like a sequence).


In your example -- tot_amt would never be stored -- it would be ALMOST as bad as storing "age" and "date of birth". AGE must be computed upon retrieval. Here, tot_amt must be computed upon retrieval. If you needed to search on tot_amt, you can certainly index:


create index t_idx on t(price*qty);

and create a view that appears to have tot_amt in it:

create view v as select ..., price, qty, price*qty tot_amt from t;

and then select * from t where tot_amt > 500000;

could use an index if appropriate.


As for the question:

...
And what's the best practice for setting a default column in an insert trigger
by using the value of an entered column to lookup a value from another table?
......

that sounds like a foreign key -- so the best practice would be

a) lose the trigger
b) lose the column
c) join to get the value at query time


Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Correct information but does not address the questions directly

Terry O'Brien, September 30, 2003 - 11:46 am UTC

Thanks for the response. However, I was hoping for list of other column default examples as it gets short attention in the Oracle docs and the general Oracle publications.

I'll take your respones to indicate that you have not come across any other defaults that would be useful to know about.

As to the rest of the response, I used the simplist example possible to illustrate the concept required and I was looking for a trigger example such as:

begin
select seqno.nextval into :new.seqno from dual;
:new.tot_amt := :new.price * :new.qty;
end

Although your answer is correct that derived values can be easily be done via a function based index, there are times when generating the derived value and storing it in the table and indexing as part of a composite index can yield staggering performance gains.

As to the lookup, again there are times for performance reasons where this type of lookup is a much better than the one you describe. In my particular case, the lookup is to categorize records into buckets and then indexed with the tot_amt into a composite index.

I was looking for any better approach than doing a select from the tigger as follows:

declare
Vto number;
begin
select col1 from table1 into Vto where .....;
:new.column1 = Vto;
......
end;

So it doesn't appear there is anything more in Oracle 9 for setting defaults and calculating values automatically on an insert beyond what I've already indicated.

Thanks again for the response and I'm enjoying your latest Oracle by Design book.


More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library