473,396 Members | 1,599 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,396 software developers and data experts.

column "generated always as" generated from multiple columns

Hi,

I have a table, something similar to:

create table my_table (
id char(32) not null primary key,
num integer not null,
code varchar(2) not null,
name varchar(60) not null,
[...]
)

Now I need to add another column 'calc_field' of type varchar that is
always automatically
derived from columns: id, num, code (concatenated)
I was trying to do something with concatenation and CAST but it always
fails.
Even if I try to test my expression with simple select to see generated
column,
the same expression fails in the 'generated always as' statement.

Any hint how to do it?
Thanks in advance

Darek

Apr 14 '06 #1
17 4662
In article <11*********************@i40g2000cwc.googlegroups. com>,
dw*****@gmail.com says...
Hi,

I have a table, something similar to:

create table my_table (
id char(32) not null primary key,
num integer not null,
code varchar(2) not null,
name varchar(60) not null,
[...]
)

Now I need to add another column 'calc_field' of type varchar that is
always automatically
derived from columns: id, num, code (concatenated)
I was trying to do something with concatenation and CAST but it always
fails.
Even if I try to test my expression with simple select to see generated
column,
the same expression fails in the 'generated always as' statement.


It might help if you post what you allready tried.
Apr 14 '06 #2
Darek wrote:
Hi,

I have a table, something similar to:

create table my_table (
id char(32) not null primary key,
num integer not null,
code varchar(2) not null,
name varchar(60) not null,
[...]
)

Now I need to add another column 'calc_field' of type varchar that is
always automatically
derived from columns: id, num, code (concatenated)
I was trying to do something with concatenation and CAST but it always
fails.
Even if I try to test my expression with simple select to see generated
column,
the same expression fails in the 'generated always as' statement.

Have you tried?
id || CHAR(num) || code

I take a guess and suspect you tried to cast to VARCHAR which is not
supported directly.

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Apr 14 '06 #3
Darek,

I don't think it should a problem. Here is a ddl that works:

create table my_table (
id char(32) not null primary key,
num integer not null,
code varchar(2) not null,
name varchar(60) not null,
gen generated always as (id concat char(num) concat code)
);

Probably you should modify the code to remove white characters from the
generated value (I suggest to write SQL function to do this).

I think I know what your problem is. If you have data in your table,
you cannot just alter the table to add a generated column. Here is what
you should do:

set integrity for my_table off;
alter table my_table
add column gen generated always as (id concat char(num) concat code)
;
set integrity for my_table immediate checked force generated;

One important remark. If your my_table is in reference with other
tables (primary, foreign keys) the first statement wil also move the
tables into "integrity off" state. So, in the last statement you must
put all the table names, and (this is important) as a one SQL
instruction, eg.

set integrity for my_table, fk_my_table, fk2_my table immediate checked
force generated;

Radosnych Swiat, Alleluja

-- Artur Wronski

Apr 14 '06 #4
I would try to avoid generated always in the design.

It's drifting away from the relational model (it is an expression but
not in a view), programmer's will use the column instead of the
expression, tools will have problems to cope with it.

Bernard Dhooghe

Apr 18 '06 #5
Bernard Dhooghe wrote:
I would try to avoid generated always in the design.

It's drifting away from the relational model (it is an expression but
not in a view), programmer's will use the column instead of the
expression, tools will have problems to cope with it.

Generated columns are a performance feature just like MQTs and
replicated tables.
There are two main reasons for them:
1. Improve performance of where clauses with expressions in them.
2. Improve performance of queries with expensive expressions.

Functionally dependent columns are discouraged because they can become
inconsistent. Generated columns are designed from teh ground up to
remain consistent.
So, all you pay is storage.

So, while they may not belong into the first schema design they have
their rightful place in reality.

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Apr 18 '06 #6
Thanks for all replies.
The hints above helped me a lot.
And yes, I need it to speed up some expensive "where" conditions.
Without doing this I had problems with full scans, now it started to
use indexes on generated columns. Just a few such columns in a big data
model should not be a problem.

Darek

Apr 20 '06 #7
Seeing colums in the physical data model that are there for query
purposes is to far away from the relation model. That "just ..." could
be more than ennoying in the long term.

Company's management could ask IBM to implement standards. In this case
of SQL 92...

Point n'est besoin d'espérer ...

Bernard Dhooghe

Darek wrote:
Thanks for all replies.
The hints above helped me a lot.
And yes, I need it to speed up some expensive "where" conditions.
Without doing this I had problems with full scans, now it started to
use indexes on generated columns. Just a few such columns in a big data
model should not be a problem.

Darek


Apr 25 '06 #8
Bernard Dhooghe wrote:
Company's management could ask IBM to implement standards. In this case
of SQL 92...


Why use that old and deprecated standard?

The SQL standard doesn't give any specifications regarding performance
things. Not even regular indexes are defined there. So we're talking
product-specific anyway...

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Apr 25 '06 #9
Knut Stolze wrote:
Bernard Dhooghe wrote:
Company's management could ask IBM to implement standards. In this case
of SQL 92...


Why use that old and deprecated standard?

The SQL standard doesn't give any specifications regarding performance
things. Not even regular indexes are defined there. So we're talking
product-specific anyway...

Matter of fact GENERATED COLUMNS ARE SQL Standard.
(and so are sequences and identity columns).
Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Apr 25 '06 #10
Being part of standard (2003, IBM presentation on
http://www.wiscorp.com/SQL2003Features.pdf) does not mean that in this
case it's usage will not bite.

SQL92 is the standard to use (and to have in DB2 UDB) to solve the
problem. Nicely relational, short SQL notation, no data overhead, no
sqlda problems

Bernard Dhooghe

Apr 25 '06 #11
Serge Rielau wrote:
Knut Stolze wrote:
Bernard Dhooghe wrote:
Company's management could ask IBM to implement standards. In this case
of SQL 92...


Why use that old and deprecated standard?

The SQL standard doesn't give any specifications regarding performance
things. Not even regular indexes are defined there. So we're talking
product-specific anyway...

Matter of fact GENERATED COLUMNS ARE SQL Standard.
(and so are sequences and identity columns).


True. But this makes the discussion here even more strange. I still don't
get what standards have to do with the use of generated columns in this
particular case and what the company's management of the OP is supposed to
do.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Apr 25 '06 #12
Bernard Dhooghe wrote:
Being part of standard (2003, IBM presentation on
http://www.wiscorp.com/SQL2003Features.pdf) does not mean that in this
case it's usage will not bite.
The national standardization bodies (i.e. ANSI, DIN, ...) participate in
ISO. Companies are only so much involved as they might sponsor a member of
these national bodies.
SQL92 is the standard to use (and to have in DB2 UDB) to solve the
problem.


No, its not. SQL:2003 is the one.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Apr 25 '06 #13
In this case, the answer is in SQL92, with row value constructors.

Select .. where (id ,num, code) > >= = < <= ...

Optimizer can just pick up the index, no table scans, easy semantic
mapping.

Would you not ask your management to ask the vendor to implement
standards that can help, this being here part of ... SQL92?

For those who want more complex things, index definition based on an
expression, working behind the scenes.

Bernard Dhooghe

Apr 25 '06 #14
Bernard Dhooghe wrote:
In this case, the answer is in SQL92, with row value constructors.

Select .. where (id ,num, code) > >= = < <= ...
You can do a "(id, num, code) = (val1, val2, val3)" in DB2. The other
comparison operators are not handled that way, though. Semantically, this
is identical to "(id = val1 AND num = val2 AND code = val3)". So the
combined variation is just syntactic sugar.
Optimizer can just pick up the index, no table scans, easy semantic
mapping.
The DB2 optimizer will do just that if it estimates that this would lead to
better execution times.
Would you not ask your management to ask the vendor to implement
standards that can help, this being here part of ... SQL92?


Sure, I would. However, I wouldn't ask for minor syntactical things but
rather for real important features like the MERGE statement or range
partitioning.

p.s: I still don't understand why you're referring to a deprecated standard.
SQL92 is superseeded by SQL:1999, which in turn is out-of-date since
December 2003 wher SQL:2003 was published by ISO.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Apr 25 '06 #15
I refer to SQL92 as in this standard, the row-value constructor clause
was introduced to solve the problem of this posting (and a number of
others in this newsgroup).

So it is part of a standard that is 13-14 years old. Let us respect
what was crafted by the committee then.

Is is not minor, there is no fully semantical equivalent way to express
what is contained in the clause. When (c1,c2) >= (v1,v2) is needed,
written as c1 > v1 or c1 = v1 and c2 >= v2 can the optimizer detect
what is meant exactly? This is certainly much more difficult than
implementing row value constructors.
Bernard Dhooghe
Knut Stolze wrote:
Bernard Dhooghe wrote:
In this case, the answer is in SQL92, with row value constructors.

Select .. where (id ,num, code) > >= = < <= ...


You can do a "(id, num, code) = (val1, val2, val3)" in DB2. The other
comparison operators are not handled that way, though. Semantically, this
is identical to "(id = val1 AND num = val2 AND code = val3)". So the
combined variation is just syntactic sugar.
Optimizer can just pick up the index, no table scans, easy semantic
mapping.


The DB2 optimizer will do just that if it estimates that this would lead to
better execution times.
Would you not ask your management to ask the vendor to implement
standards that can help, this being here part of ... SQL92?


Sure, I would. However, I wouldn't ask for minor syntactical things but
rather for real important features like the MERGE statement or range
partitioning.

p.s: I still don't understand why you're referring to a deprecated standard.
SQL92 is superseeded by SQL:1999, which in turn is out-of-date since
December 2003 wher SQL:2003 was published by ISO.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany2


Apr 25 '06 #16
Put this in a VIEW. The coce will be Standard, portable SQL and always
up to date. Do not make it hard and proprietary.

Apr 26 '06 #17
>> I still don't understand why you're referring to a deprecated standard.
SQL92 is superseeded by SQL:1999, which in turn is out-of-date since
December 2003 wher SQL:2003 was published by ISO. <<

But then you get into the problems of porting code to products that are
just getting to SQL-92 and the way the US Government (largest user of
computers on Earth) only wants SQL-92 code. Then local syntax for this
feature varies between SQL:2003 and SQL Server, etc.

I'd go with the VIEW fopr practical reasons right now and then switch
over when more products have it.

Apr 26 '06 #18

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

Similar topics

0
by: L Mehl | last post by:
Hello -- Our user wants a column in the DataGrid formatted as "%", so that he sees "nn.n%" instead of ".nnn". I have seen mention of StdDataFormat, as in Dim fmtPct1 As New StdDataFormat ...
0
by: marlene | last post by:
Hi There Does anyone know how to create a query or do something in a stored procedure to output one column as the other columns name? The output I want is something like this: ...
3
by: Prince Kumar | last post by:
Is there any way I can define an Unique constraint or unique index which allows more than one null values for the same column combination in DB2? ie, If my index is defined on (col3, col4) where...
8
by: Brian | last post by:
This is causing me to not be able to create a relation in my dataset. Here's my code: dc1 = ds.Tables .Columns ; dc2 = ds.Tables .Columns ; dr1 = new System.Data.DataRelation...
11
by: Jan | last post by:
I'm using the CSharpCodeProvider to buils some assemblies @ runtime which are never saved as files (cp.GenerateInMemory = true;). The generated assemblies are hierachically dependent on each other...
2
by: sherifffruitfly | last post by:
Hi, I'm using an adaptation of excel-reading code that's all over the internet - I don't much like or understand it, but it has worked for me in the past.... beggars can't be choosers... : ...
2
by: Fir5tSight | last post by:
Hi, I have a stored procedure that looks like the follows: ------------------------------------------------------------------------------------- SELECT ClientName AS 'Client Name', Location,...
3
by: NewlytoSQL | last post by:
Hi all, im fairly new to SQL and im stuck half way through a query, im using DB2 here is what im tryng to do. i have a query that brings back an item number , shelf req, sum of all orders columns,...
12
by: micarl | last post by:
How would i print a report based on criteria selected from several Combo Boxes as well as multiple Multi Select List Boxes, that are located on the same form? I can get one Multi List Box, just...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.