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 > SQL trigger help !!

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-13-11, 13:56
learnerkid learnerkid is offline
Registered User
 
Join Date: Apr 2011
Posts: 17
Exclamation SQL trigger help !!



Following are the tables

order (orderno,custno,totalcost,dateordered,datedispatch ed,datepaid,status)

orderline (orderno,prodno,quantity)

For above tables can anyone show how to write trigger when there is change in quantity in orderline table , totalcost field in order table should gets updated .

further how can then we write trigger to handle situation when product is removed from stock ?
Reply With Quote
  #2 (permalink)  
Old 04-13-11, 14:09
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
what you ask is not possible, because there is no price column

you've got the worst homework assignments evar!

please go back to your teach and complain
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 04-13-11, 15:01
learnerkid learnerkid is offline
Registered User
 
Join Date: Apr 2011
Posts: 17
hi , thanks for looking into query ,

this is not part of any work, though I wanted to update totalcost when quantity id changed in orderline .

In this these are additional tables that might be useful

product (prodno,unitcost,prodname,...)
customer(custno,custaddr,custname,....)
Reply With Quote
  #4 (permalink)  
Old 04-13-11, 16:09
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
Look at UPDATE, INSERT and DELETE triggers. The UPDATE trigger you will need to verify that the quantity and amount have changed and then use the OLD value and subtract from the total and add the NEW value to the total. For DELETE subtract the OLD value from the total and INSERT add NEW value to the total.

If you still have problems post your trigger and we can help investigate further.
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #5 (permalink)  
Old 04-14-11, 15:54
learnerkid learnerkid is offline
Registered User
 
Join Date: Apr 2011
Posts: 17
Hi thanks for the help , i am just new learner if i make mistake please correct it as i am trying whole heartedly to learn triggers


product (prodno,unitcost,prodname,...)

customer(custno,custaddr,custname,....)

order (orderno,custno,totalcost,.......)

orderline (orderno,prodno,quantity)

for this I want to write rule/ trigger which update total cost when we make change to quantity

So following is trigger that I am trying on logical basis, please see

CREATE OR REPLACE TRIGGER UPDATE_TRIGG
AFTER UPDATE OF QUANTITY
ON OREDERLINE
FOR EACH ROW
BEGIN
INSERT INTO ORDER
VALUES
(: old.orderno,
: old.custno ,
: old.totalcost
);
END;
Please correct if something got wrong in query as I am very new learner !!
Reply With Quote
  #6 (permalink)  
Old 04-14-11, 17:52
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
A question for you? Will there not already be an order record for each of these orderlines? If that is the case then INSERTing a new record is not the correct logic to apply.

Before looking at the syntax of the trigger lets make sure that we have the correct logic in place.
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #7 (permalink)  
Old 04-24-11, 20:28
learnerkid learnerkid is offline
Registered User
 
Join Date: Apr 2011
Posts: 17
can you please suggest any document which can give clear idea of logic and syntax for constructing trigger , it would be really a good help !!
Reply With Quote
  #8 (permalink)  
Old 04-24-11, 20:41
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by learnerkid View Post
can you please suggest any document which can give clear idea of logic and syntax for constructing trigger , it would be really a good help !!
here you go -- mysql trigger tutorial

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 04-24-11, 20:48
learnerkid learnerkid is offline
Registered User
 
Join Date: Apr 2011
Posts: 17
thanks a lot !!
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