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 > MySQL > MySQL trigger

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-10-11, 15:49
Eric the Red Eric the Red is offline
Registered User
 
Join Date: May 2008
Posts: 68
MySQL trigger

Hi,

I would like to implement a trigger on my tables. My structure is simple. You have a leaf and Branch:

-Table Leafs
id (PK)
branchid (FK)

============

-Table Branch
id (PK)
leafcount (needs to be triggered)


Simple, a branch has many leafs. But when I insert a new leaf, I need the Branch(leafcount) to increment.

Any ideas?
Reply With Quote
  #2 (permalink)  
Old 06-11-11, 10:29
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
You will need 3 triggers, one during insert to increment the leafcount, one during insert to decrement the leaf count and one during the update to verify that the branch is not changing from one to another in which case you will need to decrement one and increment another leafcount. To verify the update use the old.branchid and new.branchid while determining what to do. Of course if they are the same then no new calculation is required.
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
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