MYSQL Trigger Simple instance

Source: Internet
Author: User

For some time without writing and forgetting.

/*Cancel foreign KEY constraint*/SETForeign_key_checks=0;/*create C1 table primary key ID field name*/DROP TABLE IF EXISTS' C1 ';CREATE TABLE' C1 ' (' ID ')int( One) not NULLauto_increment, ' name 'varchar( -)CHARACTER SETUtf8mb4DEFAULT NULL,  PRIMARY KEY(' id ')) ENGINE=MEMORYDEFAULTCHARSET=latin1;/*C2 the same as the C1 structure removed the ID self-increment*/DROP TABLE IF EXISTS' C2 ';CREATE TABLE' C2 ' (' ID ')int( One) not NULL, ' name 'varchar( -)CHARACTER SETUtf8mb4DEFAULT NULL,  PRIMARY KEY(' id ')) ENGINE=MEMORYDEFAULTCHARSET=latin1;/*Create a trigger name for C1 called C1-insert*//*the trigger condition is after INSERT*/DROP TRIGGER IF EXISTS' C1-Insert';D elimiter;;CREATE TRIGGER' C1-Insert' AfterINSERT  on' C1 ' forEach ROWBEGIN /*the ID after insertion*/SET @id =new.id;/*the name after insertion*/SET @name =New.name;INSERT  into' C2 ' (' id ', ' name ')VALUES(@id,@name);/*inserting into the C2 table*/END;;/*Create a trigger name for C1 called C1-update*//*the trigger condition is after UPDATE*/DELIMITER;DROP TRIGGER IF EXISTS' C1-Update';D elimiter;;CREATE TRIGGER' C1-Update' AfterUPDATE  on' C1 ' forEach ROWBEGIN./*the ID before the modification*/SET @id =old.id;/*the modified name*/SET @name =New.name;UPDATE' C2 'SET' Name '=@name WHEREId= @id;/*Update to C2 table*/END;;

MYSQL Trigger Simple instance

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.