Geeks Talk

Prepare for your Next Interview


Welcome to the Geeks Talk forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

creating a trigger

This is a discussion on creating a trigger within the MY SQL forums, part of the Databases category; hai, I have two tables salesorders,dispatch.In sales order there is a qty to dispatch and in dispatch table there is a dispatch qty,and a column called pendingqty.now I have to ...

Go Back   Geeks Talk > Databases > MY SQL
Register Blogs FAQ Tag Cloud Calendar Mark Forums Read
  #1 (permalink)  
Old 02-16-2009
Junior Member
 
Join Date: Jul 2007
Location: hyderabad
Posts: 12
Thanks: 0
Thanked 3 Times in 2 Posts
bashasi16 is on a distinguished road
Question creating a trigger

hai,
I have two tables salesorders,dispatch.In sales order there is a qty to dispatch and in dispatch table there is a dispatch qty,and a column called pendingqty.now I have to update the pendingqty column using trigger like whenever I insert the dispatchqty in the table the trigger should update the pendingqty column with the qty remain to dispatch.
Reply With Quote
The Following User Says Thank You to bashasi16 For This Useful Post:
Sponsored Links
  #2 (permalink)  
Old 02-23-2009
Expert Member
 
Join Date: Nov 2008
Location: Chennai
Posts: 303
Thanks: 1
Thanked 37 Times in 32 Posts
amitpatel66 is on a distinguished road
Re: creating a trigger

To create a trigger or drop a trigger, use the create trigger or drop trigger statement. The syntax for these statements is described in section 12.1.11, “create trigger syntax”, and section 12.1.18, “drop trigger syntax”. Here is a simple example that associates a trigger with a table for insert statements. The trigger acts as an accumulator, summing the values inserted into one of the columns of the table.
Code:
 mysql> create table account (acct_num int, amount decimal(10,2)); query ok, 0 rows affected (0.03 sec) mysql> create trigger ins_sum before insert on account -> for each row set @sum = @sum + new.amount; query ok, 0 rows affected (0.06 sec)
the create trigger statement creates a trigger named ins_sum that is associated with the account table. It also includes clauses that specify the trigger activation time, the triggering event, and what to do with the trigger activates: the keyword before indicates the trigger action time. In this case, the trigger should activate before each row inserted into the table. The other allowable keyword here is after. The keyword insert indicates the event that activates the trigger. In the example, insert statements cause trigger activation. You can also create triggers for delete and update statements. The statement following for each row defines the statement to execute each time the trigger activates, which occurs once for each row affected by the triggering statement in the example, the triggered statement is a simple set that accumulates the values inserted into the amount column. The statement refers to the column as new.amount which means “the value of the amount column to be inserted into the new row.” to use the trigger, set the accumulator variable to zero, execute an insert statement, and then see what value the variable has afterward:
Code:
 mysql> set @sum = 0; mysql> insert into account values(137,14.98),(141,1937.50),(97,-100.00); mysql> select @sum as 'total amount inserted'; +-----------------------+ | total amount inserted | +-----------------------+ | 1852.48 | +-----------------------+
in this case, the value of @sum after the insert statement has executed is 14.98 + 1937.50 - 100, or 1852.48. To destroy the trigger, use a drop trigger statement. You must specify the schema name if the trigger is not in the default schema: mysql> drop trigger test.ins_sum; triggers for a table are also dropped if you drop the table. Trigger names exist in the schema namespace, meaning that all triggers must have unique names within a schema. Triggers in different schemas can have the same name. In addition to the requirement that trigger names be unique for a schema, there are other limitations on the types of triggers you can create. In particular, you cannot have two triggers for a table that have the same activation time and activation event. For example, you cannot define two before insert triggers or two after update triggers for a table. This should rarely be a significant limitation, because it is possible to define a trigger that executes multiple statements by using the begin ... End compound statement construct after for each row. (an example appears later in this section.) the old and new keywords enable you to access columns in the rows affected by a trigger. (old and new are not case sensitive.) in an insert trigger, only new.col_name can be used; there is no old row. In a delete trigger, only old.col_name can be used; there is no new row. In an update trigger, you can use old.col_name to refer to the columns of a row before it is updated and new.col_name to refer to the columns of the row after it is updated. A column named with old is read only. You can refer to it (if you have the select privilege), but not modify it. A column named with new can be referred to if you have the select privilege for it. In a before trigger, you can also change its value with set new.col_name = value if you have the update privilege for it. This means you can use a trigger to modify the values to be inserted into a new row or that are used to update a row. In a before trigger, the new value for an auto_increment column is 0, not the automatically generated sequence number that will be generated when the new record actually is inserted. Old and new are mysql extensions to triggers. By using the begin ... End construct, you can define a trigger that executes multiple statements. Within the begin block, you also can use other syntax that is allowed within stored routines such as conditionals and loops. However, just as for stored routines, if you use the mysql program to define a trigger that executes multiple statements, it is necessary to redefine the mysql statement delimiter so that you can use the ; statement delimiter within the trigger definition. The following example illustrates these points. It defines an update trigger that checks the new value to be used for updating each row, and modifies the value to be within the range from 0 to 100. This must be a before trigger because the value needs to be checked before it is used to update the row:
Code:
 mysql> delimiter // mysql> create trigger upd_check before update on account -> for each row -> begin -> if new.amount < 0 then -> set new.amount = 0; -> elseif new.amount > 100 then -> set new.amount = 100; -> end if; -> end;// mysql> delimiter ;
it can be easier to define a stored procedure separately and then invoke it from the trigger using a simple call statement. This is also advantageous if you want to invoke the same routine from within several triggers. There are some limitations on what can appear in statements that a trigger executes when activated: the trigger cannot use the call statement to invoke stored procedures that return data to the client or that use dynamic sql. (stored procedures are allowed to return data to the trigger through out or inout parameters.) the trigger cannot use statements that explicitly or implicitly begin or end a transaction such as start transaction, commit, or rollback. Prior to mysql 5.0.10, triggers cannot contain direct references to tables by name. Mysql handles errors during trigger execution as follows: if a before trigger fails, the operation on the corresponding row is not performed. A before trigger is activated by the attempt to insert or modify the row, regardless of whether the attempt subsequently succeeds. An after trigger is executed only if the before trigger (if any) and the row operation both execute successfully. An error during either a before or after trigger results in failure of the entire statement that caused trigger invocation. For transactional tables, failure of a statement should cause rollback of all changes performed by the statement. Failure of a trigger causes the statement to fail, so trigger failure also causes rollback. For non-transactional tables, such rollback cannot be done, so although the statement fails, any changes performed prior to the point of the error remain in effect. This should help you create a trigger in mysql
Reply With Quote
Reply

  Geeks Talk > Databases > MY SQL

Thread Tools
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 On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
trigger Thirumurugan.K Oracle 4 11-27-2008 08:30 AM
what is a trigger? srinu.tenali SQL Server 11 11-10-2008 01:57 AM
trigger jayanth511 SQL 7 06-28-2008 02:37 AM
Trigger goodstudent SQL 2 03-14-2008 07:03 AM
what is trigger amaravadi11 SQL 8 01-23-2008 04:10 PM


All times are GMT -4. The time now is 12:21 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.1
Copyright © 2009 GeekInterview.com. All Rights Reserved