douji5329 2015-01-04 00:37
浏览 21
已采纳

用MySQL创建触发器

Two tables: users and messages

 CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(10) NOT NULL,
`password` varchar(10) NOT NULL,
`email` varchar(100) NOT NULL,
`verifystring` varchar(20) NOT NULL,
`active` tinyint(4) NOT NULL,
`usertype` tinyint(4) NOT NULL,
`img` varchar(200) DEFAULT NULL,
 PRIMARY KEY (`id`)
 ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;

 CREATE TABLE IF NOT EXISTS `messages` (
`id` tinyint(4) NOT NULL AUTO_INCREMENT,
`date` datetime NOT NULL,
`user_id` int(11) NOT NULL,
`topic_id` int(11) NOT NULL,
`subject` varchar(100) NOT NULL,
`body` text NOT NULL,
`img` varchar(200) DEFAULT NULL,
  PRIMARY KEY (`id`),
 KEY `topic_id` (`topic_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=51 ;

trigger:

CREATE TRIGGER `updateimg` AFTER UPDATE ON `users`
 FOR EACH ROW begin
    update messages set img = new.img where messages.user_id = users.id;
end

img field is keeping the name of the image user has uploaded. How can i copy the value img from table users to table messages when it is updated?

  • 写回答

1条回答 默认 最新

  • douben7493 2015-01-04 01:19
    关注

    Thats should do it:

    CREATE TRIGGER update AFTER UPDATE ON table1
        for each ROW
        begin
            update table2 set y = new.y where table2.user_id = new.id;
        end
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 Keil uVision5创建project没反应
  • ¥15 mmseqs内存报错
  • ¥15 vika文档如何与obsidian同步
  • ¥15 华为手机相册里面的照片能够替换成自己想要的照片吗?
  • ¥15 陆空双模式无人机飞控设置
  • ¥15 sentaurus lithography
  • ¥100 求抖音ck号 或者提ck教程
  • ¥15 关于#linux#的问题:子进程1等待子进程A、B退出后退出(语言-c语言)
  • ¥20 web页面如何打开Outlook 365的全球离线通讯簿功能
  • ¥15 io.jsonwebtoken.security.Keys