Defining custom type calculated fields for IBM DB2 database tables

I'd like to define a custom field type, MYTYPE, that is calculated according to a formula (e.g. FIELD1+FIELD2). I'd like to be able to add fields of MYTYPE to DB2 database tables.

Oracle refers to this ability as virtual column. More info can be found here:
http://www.dba-oracle.com/oracle11g/oracle_11g_function_based_columns.htm

I am not sure if DB2 views can achieve the same purpose. Views can define a column for the calculated fields, but may not allow associating the calculation with a custom type. Having a custom type is important because it allows keeping the formula definition in a single place and seemlessly propagate the updates.


cyber-33Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kent OlsenDBACommented:
Hi cyber,

You can create your own data types in DB2.  Generally, these are structure objects similar to C struct objects.


  http://publib.boulder.ibm.com/infocenter/db2luw/v9/index.jsp?topic=/com.ibm.db2.udb.admin.doc/doc/c0005011.htm

The CREATE TYPE statement is used to actually create the type:

  http://publib.boulder.ibm.com/infocenter/db2luw/v9/topic/com.ibm.db2.udb.admin.doc/doc/c0005013.htm



Good Luck,
Kent
cyber-33Author Commented:
Hi Kdo, I saw the UDTs before, but I don't think they can be calculated according to a formula. In other words, the UDT types seem to be static. Please, correct me if I am wrong.
Kent OlsenDBACommented:

Yes.  As far as I know they are static.

You could always create a Materialized View / Materialized Query Table over the existing table.  A ton more flexible with the potential to be a lot more efficient, too.


Kent
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

cyber-33Author Commented:
Yeah, the view is an option, but as stated in the question, I don't think views allow formula-based custom types. Do they?
Kent OlsenDBACommented:
Hi Cyber,

We're mixing terminology here.  :)

A Materialized View / Materialized Query Table is NOT a normal view.  DB2 actually caches the results of a query into a user accessible table.  Subsequent queries of the MV / MQT accesses the cached results instead of operating on the entire base table.  Any legal SELECT statement of the base table will generate the data in the MQT.  I'm not aware of a restriction that a UDT can not be used.

DB2 doesn't support the 'virtual column' that you find in Oracle.  But you can perform almost any calculation that you need to in the view and return a legal result set.  You should be able to cast to a UDT and return it.


Kent

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cyber-33Author Commented:
Kdo - thank you.

I was afraid that virtual columns are not supported in DB2 and you have confirmed it. The question is resolved.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
DB2

From novice to tech pro — start learning today.