Create and delete MySQL triggers

Source: Internet
Author: User
The following article describes how to create a MySQL trigger and how to 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: CREATETRIGGERtrigger_nametrigger_timetrigger_eve

The following article describes how to create a MySQL trigger and how to 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: CREATETRIGGERtrigger_nametrigger_timetrigger_eve

The following article describes how to create a MySQL trigger and how to 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:

 
 
  1. CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name
  2. FOR EACH ROW
  3. BEGIN
  4. trigger_stmt
  5. END;
  6. CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name
  7. FOR EACH ROW
  8. BEGIN
  9. trigger_stmt
  10. END;

Example

 
 
  1. CREATE TRIGGER SetUserHome after insert ON users
  2. FOR EACH ROW
  3. BEGIN
  4. update `users` set homeLocationX = 128,
  5. homeLocationY=128, homeLocationZ=30
  6. where uuid = NEW.uuid
  7. 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.

Replace the statement with the following:

 
 
  1. CREATE TRIGGER SetUserHome before insert ON users
  2. FOR EACH ROW
  3. BEGIN
  4. set New.homeLocationX = 128;
  5. set New.homeLocationY = 128;
  6. set New.homeLocationZ=30;
  7. END

Uppercase keywords

Trigger_name: name of the trigger. My commonly used naming rules are t_name_tableName _ (B | a) (I | u | d), t: MySQL trigger identifier, name: English name, tableName: table Name, B (BEFORE): BEFORE the event is triggered, a (AFTER): AFTER the event is triggered, I (insert): identifies the insert event, u (update ): identifies an update event. d (delete): identifies a delete event;

Trigger_time: trigger time (BEFORE or AFTER)

Trigger_event: event name (insert, update, or delete)

Tbl_name: Table Name (must be a permanent table)

Trigger_stmt: Execute the statement (which can be a compound language name). Use the OLD and NEW aliases to reference columns in the table related to the trigger program.

2. Delete the producer

Syntax:

 
 
  1. DROP TRIGGER [schema_name.]trigger_name;

Note: The above operations require the SUPER permission.

Example:

 
 
  1. DROP TRIGGER t_wiley_hotelComment_bu;
  2. delimiter //
  3. CREATE TRIGGER t_wiley_hotelComment_bu BEFORE UPDATE ON hotel_comment
  4. FOR EACH ROW
  5. BEGIN
  6. IF OLD.ispass=0 && NEW.ispass=1 THEN
  7. UPDATE hotel_info SET sumcommentsumcomment=sumcomment+1,

    sumconsumesumconsume=sumconsume+NEW.consume,sumservicesumservice=sumservice+NEW.service,

    sumroomsumroom=sumroom+NEW.room,sumentironsumentiron=sumentiron+NEW.entironment,

    totaltotal=total+(NEW.service+NEW.room+NEW.entironment) WHERE hotel_id=NEW.hotel_id;
  8. ELSEIF OLD.ispass=1 && NEW.ispass=0 THEN
  9. UPDATE hotel_info SET sumcommentsumcomment=sumcomment-1,

    sumconsumesumconsume=sumconsume-NEW.consume,sumservicesumservice=sumservice-NEW.service,

    sumroomsumroom=sumroom-NEW.room,sumentironsumentiron=sumentiron-NEW.entironment,

    totaltotal=total-(NEW.service+NEW.room+NEW.entironment) WHERE hotel_id=NEW.hotel_id;
  10. END IF;
  11. END;//
  12. delimiter ;

The above content is an introduction to the use of MySQL triggers. I hope you can 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.