If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Trigger Help !

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-10-07, 11:49
stevoo stevoo is offline
Registered User
 
Join Date: Dec 2007
Posts: 3
Trigger Help !

Hey everyone , i am relatively new in triigers and i was hoping for some help on this one.

I have succefully created a triger to check for the email correctness.

And i want one more to check that the same product is not entered twice on a day.

I have 3 tables. the products, the catalog and the entry.

So i want to add a produnt into a given catalog but i do not want a product to be entered twich on the same date

How can i do that with triggers ?
the date is on the catalog table and whenever i enter an entry ill have to check that the day is not the same.

Some help would be greatly appreciated !
Reply With Quote
  #2 (permalink)  
Old 12-10-07, 12:03
stevoo stevoo is offline
Registered User
 
Join Date: Dec 2007
Posts: 3
CREATE TRIGGER check_date
BEFORE INSERT ON entry
REFERENCING NEW AS n
FOR EACH ROW MODE
BEGIN ATOMIC
FROM product s, catalog x, entry e
WHERE s.sno = e.esno and
e.encode = x.excode and
n.x.date = x.date
SIGNAL SQLSTATE '75001'
(' There cannot be two entries on the same day');
END


This is something that i am trying to do, but it is wrong, i didnt run it, cause i now it is wrong , so some help
Reply With Quote
  #3 (permalink)  
Old 12-10-07, 13:05
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Why don't you just create a unique index on the columns?

Andy
Reply With Quote
  #4 (permalink)  
Old 12-10-07, 13:12
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Code:
 FROM product s, catalog x, entry e
WHERE s.sno = e.esno and
e.encode = x.excode and
n.x.date = x.date
SIGNAL SQLSTATE '75001'
(' There cannot be two entries on the same day');
does not look like a complete SQL statement.
Reply With Quote
  #5 (permalink)  
Old 12-10-07, 13:21
stevoo stevoo is offline
Registered User
 
Join Date: Dec 2007
Posts: 3
well a unique is one way to solve that one, as also doing that programmatically ( java )

but i am looking for a trigger solution.


So what else should i include in order to make it a complete sql ?
Reply With Quote
  #6 (permalink)  
Old 12-10-07, 13:34
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
CREATE TRIGGER NyTrig NO CASCADE BEFORE INSERT ON MyTable
REFERENCING NEW as newdata FOR EACH ROW MODE DB2SQL
BEGIN ATOMIC
DECLARE num_rows int;

SET num_rows = (select count(*) from MyTable as mt where (mt.col1 = newdata.col1 and mt.col2 = newdata.col2) );

if (num_rows > 0)
THEN SIGNAL SQLSTATE '75001' set message_text = ' There cannot be two entries on the same day';
END IF;

END


Although, in may opinion, a unique constraint / index is the better way to go.

Andy
Reply With Quote
  #7 (permalink)  
Old 12-10-07, 14:06
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
A trigger is pretty much the worst choice. A unique constraint is supported by an index. With the index, a check for duplicates is trivial and done with a few page access only. With a trigger, you potentially have to read all data pages.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #8 (permalink)  
Old 12-10-07, 14:14
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
As the others have sad, index is the best choice for performance reasons.

In case you have to go the trigger route, a variation of the trigger stmt: ( sorry, didn't test it)

Code:
CREATE TRIGGER NyTrig NO CASCADE BEFORE INSERT ON MyTable
REFERENCING NEW as newdata  FOR EACH ROW  MODE DB2SQL
WHEN EXISTS (select 1 from MyTable as mt where (mt.col1 = newdata.col1 and mt.col2 = newdata.col2) )
 
     SIGNAL  SQLSTATE '75001' set message_text = ' There cannot be two entries on the same day'
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On