Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

php - how to update samehost database using trigger from diffrent database in same host

I need to update SAME HOST database using trigger

CREATE TRIGGER `StudentMaster` BEFORE INSERT ON `student_master`
FOR EACH
ROW INSERT INTO xxxxxx`.`xxxxx`
SELECT *
FROM `student_master`
ORDER BY `SM_NO DESC LIMIT 1` 

Above ex is what i have tried ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can try the following:

/*Table structure for table `db1`.`student_master` */

DROP TABLE IF EXISTS `db1`.`student_master`;

CREATE TABLE `db1`.`student_master` (
  `sm_no` INT(10) UNSIGNED NOT NULL,
  `column0` VARCHAR(50) DEFAULT NULL,
  `column1` VARCHAR(50) DEFAULT NULL,
  `column2` VARCHAR(50) DEFAULT NULL,
  PRIMARY KEY (`sm_no`)
) ENGINE=INNODB;

/*Table structure for table `db2`.`student_master` */

DROP TABLE IF EXISTS `db2`.`student_master`;

CREATE TABLE `db2`.`student_master` (
  `sm_no` INT(10) UNSIGNED NOT NULL,
  `column0` VARCHAR(50) DEFAULT NULL,
  `column1` VARCHAR(50) DEFAULT NULL,
  `column2` VARCHAR(50) DEFAULT NULL,
  PRIMARY KEY (`sm_no`)
) ENGINE=INNODB;

/* Trigger structure for table `db1`.`student_master` */

DELIMITER $$

DROP TRIGGER IF EXISTS `db1`.`trg_ai_sm`$$

CREATE TRIGGER `db1`.`trg_ai_sm` AFTER INSERT ON `db1`.`student_master`
FOR EACH ROW
BEGIN
    INSERT INTO `db2`.`student_master` (`sm_no`, `column0`, `column1`, `column2`)
    SELECT NEW.`sm_no`, NEW.`column0`, NEW.`column1`, NEW.`column2`;
END$$

DELIMITER ;

Test:

INSERT INTO `db1`.`student_master` (`sm_no`, `column0`, `column1`, `column2`)
VALUES
(1, 'column0', 'column1', 'column2');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...