473,396 Members | 1,706 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.

Sql Trigger or Key Constraint or Check Constraint

new
Sorry to bug you with what is a simple question but if you could point me in
the right direction I can follow up.

I have a sql table field we can call XXX it is the least significant of a
four field primary key (aaa bbb ccc).

When XXX is of the form A99 (i.e. the letter A followed by two digits) I
want to ensure that it is the only aaa bbb ccc of that form.

If I had a function ISof FormA99(XXX) that returned either "T" when it was
or "F" || XXX my key constraint would be that aaa bbb ccc ISof FormA99(XXX)
is unique.

My question is: how do I do this? If there was such a thing (is there?) as
a derived field then I would create the function and use a unique
constraint. I am sure there are many ways to do this. This table will be
small (1000 rows at most) and change infrequently.

Thanks
Oct 4 '06 #1
5 2458
new wrote:
Sorry to bug you with what is a simple question but if you could point me
in the right direction I can follow up.

I have a sql table field we can call XXX it is the least significant of a
four field primary key (aaa bbb ccc).

When XXX is of the form A99 (i.e. the letter A followed by two digits) I
want to ensure that it is the only aaa bbb ccc of that form.

Can XXX contain values that are _not_ in the form A\d\d (perl regexp
syntax)? If so, what's supposed to happen then? If not, then I believe
that XXX is your only primary/unique key. Otherwise, you will need a
trigger because check constraints operate on a single row only.
If I had a function ISof FormA99(XXX) that returned either "T" when it was
or "F" || XXX my key constraint would be that aaa bbb ccc ISof
FormA99(XXX) is unique.
You treat the values A12 and A78 as the same in that case? But 'A76'
and 'CDE' are different while 'ABC' and 'XYZ' are treated as equal again?
If so, you can simply create this function of yours (make it DETERMINISTIC
and NO EXTERNAL ACTION), add a GENERATED ALWAYS column (based on the
function) to your table, and use that column in your primary/unique key.

p.s: If possible, you should think about your database design. It seems to
be rather strange to have something in a primary/unique key that doesn't
really belong to the primary/unique key at all - but rather some derived
value.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Oct 4 '06 #2
new
Knut, thanks for your response!

Your last comment first: I entirely agree - now that I realize that one can
create a generated column with db2 I entirely agree that it is the generated
column that should be in the unique primary key.

I am having a problem with using "generated always" with a char column - am
I missing something? (V5r2)

I believe your second reading of my code was correct: A\d\d would mean that
A12 and A78 would be the same, 'A76'
and 'CDE' are different but (NOTE DIFFERENCE FROM YOUR RESPONSE) 'ABC' and
'XYZ' are NOT treated as equal

So can I use a generated function with a char column (in V5r2)?
"Knut Stolze" <st****@de.ibm.comwrote in message
news:eg**********@lc03.rz.uni-jena.de...
new wrote:
>Sorry to bug you with what is a simple question but if you could point me
in the right direction I can follow up.

I have a sql table field we can call XXX it is the least significant of
a
four field primary key (aaa bbb ccc).

When XXX is of the form A99 (i.e. the letter A followed by two digits) I
want to ensure that it is the only aaa bbb ccc of that form.


Can XXX contain values that are _not_ in the form A\d\d (perl regexp
syntax)? If so, what's supposed to happen then? If not, then I believe
that XXX is your only primary/unique key. Otherwise, you will need a
trigger because check constraints operate on a single row only.
>If I had a function ISof FormA99(XXX) that returned either "T" when it
was
or "F" || XXX my key constraint would be that aaa bbb ccc ISof
FormA99(XXX) is unique.

You treat the values A12 and A78 as the same in that case? But 'A76'
and 'CDE' are different while 'ABC' and 'XYZ' are treated as equal again?
If so, you can simply create this function of yours (make it DETERMINISTIC
and NO EXTERNAL ACTION), add a GENERATED ALWAYS column (based on the
function) to your table, and use that column in your primary/unique key.

p.s: If possible, you should think about your database design. It seems
to
be rather strange to have something in a primary/unique key that doesn't
really belong to the primary/unique key at all - but rather some derived
value.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany

Oct 5 '06 #3
new wrote:
Knut, thanks for your response!

Your last comment first: I entirely agree - now that I realize that one
can create a generated column with db2 I entirely agree that it is the
generated column that should be in the unique primary key.

I am having a problem with using "generated always" with a char column -
am
I missing something? (V5r2)

I believe your second reading of my code was correct: A\d\d would mean
that A12 and A78 would be the same, 'A76'
and 'CDE' are different but (NOTE DIFFERENCE FROM YOUR RESPONSE) 'ABC' and
'XYZ' are NOT treated as equal
Then your description wasn't complete. You were only talking about A
followed by two digits. What's supposed to happen in all other cases?
So can I use a generated function with a char column (in V5r2)?
I don't know enough about DB2 for iSeries to comment. You didn't state your
platform, so I assumed DB2 for LUW.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Oct 5 '06 #4
new
ok the function in my case would transform:
if arg of form A\d\d then '---' else arg

"Knut Stolze" <st****@de.ibm.comwrote in message
news:eg**********@lc03.rz.uni-jena.de...
new wrote:
>Knut, thanks for your response!

Your last comment first: I entirely agree - now that I realize that one
can create a generated column with db2 I entirely agree that it is the
generated column that should be in the unique primary key.

I am having a problem with using "generated always" with a char column -
am
I missing something? (V5r2)

I believe your second reading of my code was correct: A\d\d would mean
that A12 and A78 would be the same, 'A76'
and 'CDE' are different but (NOTE DIFFERENCE FROM YOUR RESPONSE) 'ABC'
and
'XYZ' are NOT treated as equal

Then your description wasn't complete. You were only talking about A
followed by two digits. What's supposed to happen in all other cases?
>So can I use a generated function with a char column (in V5r2)?

I don't know enough about DB2 for iSeries to comment. You didn't state
your
platform, so I assumed DB2 for LUW.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany

Oct 5 '06 #5
DB2 for i5/OS only supports the GENERATED ALWAYS AS IDENTITY clause for numeric
columns - that support was added initially in V5R2.

new wrote:
ok the function in my case would transform:
if arg of form A\d\d then '---' else arg

"Knut Stolze" <st****@de.ibm.comwrote in message
news:eg**********@lc03.rz.uni-jena.de...
>new wrote:
>>Knut, thanks for your response!

Your last comment first: I entirely agree - now that I realize that one
can create a generated column with db2 I entirely agree that it is the
generated column that should be in the unique primary key.

I am having a problem with using "generated always" with a char column -
am
I missing something? (V5r2)

I believe your second reading of my code was correct: A\d\d would mean
that A12 and A78 would be the same, 'A76'
and 'CDE' are different but (NOTE DIFFERENCE FROM YOUR RESPONSE) 'ABC'
and
'XYZ' are NOT treated as equal
Then your description wasn't complete. You were only talking about A
followed by two digits. What's supposed to happen in all other cases?
>>So can I use a generated function with a char column (in V5r2)?
I don't know enough about DB2 for iSeries to comment. You didn't state
your
platform, so I assumed DB2 for LUW.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany

--
Kent Milligan
ISV Enablement - System i
km***@us.eye-bee-m.com (spam trick) GO HAWKEYES!!
>>ibm.com/iseries/db2
(opinions stated are not necessarily those of my employer)
Oct 5 '06 #6

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

Similar topics

7
by: Justin | last post by:
I am extremely new at SQL Server2000 and t-sql and I'm looking to create a simple trigger. For explanation sake, let's say I have 3 columns in one table ... Col_1, Col_2 and Col_3. The data type...
8
by: Stuart McGraw | last post by:
Is Microsoft full of #*$#*% (again) or am I badly misunderstanding something? Quote from Microsoft's T-SQL doc: > INSTEAD OF triggers are executed instead of the triggering action. > These...
3
by: Fernand St-Georges | last post by:
How can I create a trigger that obliges UPPERCASE of a field in the database? thanks
2
by: ezra epstein | last post by:
Hi, I've got a table: <code language="SQL"> CREATE TABLE "common"."dynamic_enum" ( "pk_id" integer DEFAULT nextval('"common"."pw_seq"') , "enum_type" ...
4
by: Alexander Pope | last post by:
I am using db2 udb v8.2 AIX I have created trigger, however I am not confident it meets industry standards. If I make it fail, I cant tell from the message where it is failing. what can I add to...
5
by: Peter Erickson | last post by:
I am running postgresql 7.4.2 and having problems creating a trigger function properly. I keep getting the following error: ERROR: OLD used in query that is not in rule I have a table called...
3
by: Wojto | last post by:
Hi there! I need to write a trigger that will check referential integrity of my data. I have few FOREIGN KEY constraints but, as You probably konow, the cannot be deferred (in the meaning of SQL...
7
by: Wojto | last post by:
Hello! Another day, another problem... :-) I've got something like this: CREATE TABLE A ( pk_A INT CONSTRAINT primarykey_A PRIMARY KEY ); CREATE TABLE B (
8
by: lenygold via DBMonster.com | last post by:
What about using delete/insert instead of update? Lennart wrote: -- Message posted via DBMonster.com http://www.dbmonster.com/Uwe/Forums.aspx/ibm-db2/200807/1
7
by: lenygold via DBMonster.com | last post by:
Hi everybody. I wrote a trigger for the follwing requirement: In a given table T with columns A,B,C are up to 5 entries allowed for a given combination of (A,B). create table t (a varchar2(10),...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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.