DB2 Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDB2 Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 7th, 2007, 02:55 AM
Peter_Machner Peter_Machner is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2007
Posts: 4 Peter_Machner User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 m 59 sec
Reputation Power: 0
Creating an advanced constraint

Hi!
I have a table (called TEST) which is a dictionary
NAME (VARCHAR) - VALUE (VARCHAR)

VALUE can be in practice of any type stored as a VARCHAR (I know it's not elegant but it's a kind of a DB equivalent of a system.properties file so I need to keep different types).

For NAME = 'A' VALUE is an SMALLINT.

And here's my problem:

When NAME='B', I want VALUE to be always greater than the VALUE for 'A'.
I tried creating such a constraint:

alter table TEST add constraint TEST_CK
check ( NAME <> 'B' OR (NAME = 'B' AND SMALLINT(VALUE) > (SELECT SMALLINT(VALUE) FROM TEST WHERE NAME='A')) );

but it turns out I cannot have subqueries inside constraints.
How to solve this?
Best regards,

Peter

Reply With Quote
  #2  
Old August 7th, 2007, 07:53 AM
valined's Avatar
valined valined is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Oct 2004
Location: Brussels
Posts: 612 valined User rank is Captain (20000 - 30000 Reputation Level)valined User rank is Captain (20000 - 30000 Reputation Level)valined User rank is Captain (20000 - 30000 Reputation Level)valined User rank is Captain (20000 - 30000 Reputation Level)valined User rank is Captain (20000 - 30000 Reputation Level)valined User rank is Captain (20000 - 30000 Reputation Level)valined User rank is Captain (20000 - 30000 Reputation Level)valined User rank is Captain (20000 - 30000 Reputation Level)valined User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 18 h 46 sec
Reputation Power: 231
Lightbulb

Quote:
Originally Posted by Peter_Machner
Hi!
I have a table (called TEST) which is a dictionary
NAME (VARCHAR) - VALUE (VARCHAR)

VALUE can be in practice of any type stored as a VARCHAR (I know it's not elegant but it's a kind of a DB equivalent of a system.properties file so I need to keep different types).

For NAME = 'A' VALUE is an SMALLINT.

And here's my problem:

When NAME='B', I want VALUE to be always greater than the VALUE for 'A'.
I tried creating such a constraint:

alter table TEST add constraint TEST_CK
check ( NAME <> 'B' OR (NAME = 'B' AND SMALLINT(VALUE) > (SELECT SMALLINT(VALUE) FROM TEST WHERE NAME='A')) );

but it turns out I cannot have subqueries inside constraints.
How to solve this?
Best regards,

Peter


Create a trigger on insert/update on the table that would contain your business rule.
__________________
"Problem" is just a bleak word for challenge. -- Richard Fish
Javalanche

Reply With Quote
  #3  
Old August 8th, 2007, 02:12 AM
Peter_Machner Peter_Machner is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2007
Posts: 4 Peter_Machner User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 m 59 sec
Reputation Power: 0
Quote:
Originally Posted by valined
Create a trigger on insert/update on the table that would contain your business rule.


Hm.. thanks One more question: how do I know whether the trigger has been triggered or not? I would like to provide some info to the user that the value they passed is too big. I suppose I could make the trigger modify something else I would have to check, but since I haven't got much experience in SQL, I'm not sure if this is the right approach.

Reply With Quote
  #4  
Old August 12th, 2007, 02:53 AM
valined's Avatar
valined valined is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Oct 2004
Location: Brussels
Posts: 612 valined User rank is Captain (20000 - 30000 Reputation Level)valined User rank is Captain (20000 - 30000 Reputation Level)valined User rank is Captain (20000 - 30000 Reputation Level)valined User rank is Captain (20000 - 30000 Reputation Level)valined User rank is Captain (20000 - 30000 Reputation Level)valined User rank is Captain (20000 - 30000 Reputation Level)valined User rank is Captain (20000 - 30000 Reputation Level)valined User rank is Captain (20000 - 30000 Reputation Level)valined User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 18 h 46 sec
Reputation Power: 231
Lightbulb

Quote:
Originally Posted by Peter_Machner
Hm.. thanks One more question: how do I know whether the trigger has been triggered or not?

By witnessing its effects?!?

Quote:
Originally Posted by Peter_Machner
I would like to provide some info to the user that the value they passed is too big. I suppose I could make the trigger modify something else I would have to check, but since I haven't got much experience in SQL, I'm not sure if this is the right approach.

Data validation on user's side?

Reply With Quote
  #5  
Old August 13th, 2007, 02:35 AM
Peter_Machner Peter_Machner is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2007
Posts: 4 Peter_Machner User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 m 59 sec
Reputation Power: 0
Quote:
Originally Posted by valined
By witnessing its effects?!?


What I mean is that I would like to have some kind of synchronous communication... I.e. when a trigger is triggered, something changes in the DB, but I need to make an additional query to find this out. I know nothing of the changes, unless I issue a new SELECT statement. Whilst when I have a constraint, the DB explicitly notifies me that what I want to do is not allowed. And this is the kind of behaviour I want. Is it possible to achieve it with a trigger?


Quote:
Originally Posted by valined
Data validation on user's side?


Hmm... this makes sense I just wanted to have the DB check this as well (i.e. in case someone insert a bad value in an installation script).

Reply With Quote
  #6  
Old August 13th, 2007, 08:38 AM
valined's Avatar
valined valined is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Oct 2004
Location: Brussels
Posts: 612 valined User rank is Captain (20000 - 30000 Reputation Level)valined User rank is Captain (20000 - 30000 Reputation Level)valined User rank is Captain (20000 - 30000 Reputation Level)valined User rank is Captain (20000 - 30000 Reputation Level)valined User rank is Captain (20000 - 30000 Reputation Level)valined User rank is Captain (20000 - 30000 Reputation Level)valined User rank is Captain (20000 - 30000 Reputation Level)valined User rank is Captain (20000 - 30000 Reputation Level)valined User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 18 h 46 sec
Reputation Power: 231
Lightbulb

Quote:
Originally Posted by Peter_Machner
What I mean is that I would like to have some kind of synchronous communication... I.e. when a trigger is triggered, something changes in the DB, but I need to make an additional query to find this out. I know nothing of the changes, unless I issue a new SELECT statement. Whilst when I have a constraint, the DB explicitly notifies me that what I want to do is not allowed. And this is the kind of behaviour I want. Is it possible to achieve it with a trigger?

Maybe you're a little bit confused. Your trigger kicks in after or before a row (or several rows..) is inserted/updated/deleted in a table.
If a business rule (checked by the trigger) is not respected, the trigger enforces it -- by raising an error for example. [You can do practically anything inside a trigger: update other tables, call other global/package procedures or functions etc] Then you know that something went wrong: the DML statement failed with a specific error & message.
You don't need to issue a SELECT statement to find out whether a given operation failed or not. Trying to insert into a table an invalid value will fail due to various constraints imposed on the table(s) by the DB designer.
If all went well then... you really don't want to know. Just commit the transaction and be done with it.
Quote:
Originally Posted by Peter_Machner
Hmm... this makes sense I just wanted to have the DB check this as well (i.e. in case someone insert a bad value in an installation script).

As I said above, RDBMS checks all domain/referential integrity constraints before doing anything, then all before triggers are called, then all after triggers are called; if any of these operations fail a specific error is generated.
So a "bad value" could be the culprit in any of these failures.

Reply With Quote
  #7  
Old August 13th, 2007, 10:58 AM
Peter_Machner Peter_Machner is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2007
Posts: 4 Peter_Machner User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 m 59 sec
Reputation Power: 0
Quote:
Originally Posted by valined
If a business rule (checked by the trigger) is not respected, the trigger enforces it -- by raising an error for example.


Oh, ok If it works this way, then I guess it's all I need I will give it a check. Thanks a lot

Peter

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDB2 Development > Creating an advanced constraint


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway