Create and delete MySQL triggers

Source: Internet
Author: User

The following article describes how to create and delete a MySQL trigger. If you create a MySQL trigger correctly, if you are interested in deleting the MySQL trigger, you can click the following article to view it. 1. CREATE a MySQL TRIGGER. Syntax: create trigger trigger_name trigger_time trigger_event ON tbl_name for each row begin trigger_stmt END; create trigger trigger_name trigger_time trigger_event ON tbl_name for each row begin trigger_stmt END; example: create trigger SetUserHome after insert ON users for each row begin update 'users' set homeLocationX = 128, homeLocationY = 128, homeLocationZ = 30 where uuid = NEW. uuid END The preceding example is incorrect. Updating the table when it is triggered will bring the program into an endless loop. The system will report this error: it is already used by statement which invoked this stored function/trigger. you should change it to the following statement: create trigger SetUserHome before insert ON users for each row begin set New. homeLocationX = 128; set New. homeLocationY = 128; set New. homeLocationZ = 30; END is capitalized with the keyword trigger_name: trigger name. My frequently used naming rules are t_name_tableName _ (B | a) (I | u | d), t: mySQL trigger ID, name: English name, tableName: Table name, B (BEFORE): BEFORE the event is triggered, a (AFTER): AFTER the event is identified, I (insert ): Identifies the insert event, u (update): identifies the update event, d (delete): identifies the delete event, trigger_time: trigger_event (BEFORE or AFTER: event name (insert, update, or delete) tbl_name: Table Name (must be a permanent table) trigger_stmt: Statement executed (can be a compound language name), alias OLD and NEW, can reference columns in tables related to the trigger program. 2. Delete the producer Syntax: drop trigger [schema_name.] trigger_name; Note: The preceding operations require SUPER permission. Example: drop trigger t_wiley_comment comment_bu; delimiter // create trigger t_wiley_comment comment_bu before update on comment _comment for each row begin if old. ispass = 0 & NEW. ispass = 1 then update pai_info SET sumcommentsumcomment = sumcomment + 1, sumconsumesumconsume = sumconsume + NEW. consume, sumservicesumservice = sumservice + NEW. service, sum Roomsumroom = sumroom + NEW. room, sumentironsumentiron = sumentiron + NEW. entironment, totaltotal = total + (NEW. service + NEW. room + NEW. entironment) WHERE condition _id = NEW. else _id; elseif old. ispass = 1 & NEW. ispass = 0 then update consumer _info SET sumcommentsumcomment = sumcomment-1, sumconsumesumconsume = sumconsume-NEW.consume, sumservicesumservice = sumservice-NEW.service, sumroomsumroom = sumroom-NEW.room, sumentironsumentir On = sumentiron-NEW.entironment, totaltotal = total-(NEW. service + NEW. room + NEW. entironment) WHERE condition _id = NEW. else _id; end if; END; // delimiter; The above content is an introduction to the use of MySQL triggers. I hope you will gain some benefits.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.