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