温欣爸比

  • 主页
  • Alfred Workflow
  • 《Vim 练级手册》
  • 常用命令
  • 代码笔记
  • 合辑
  • 在线工具
所有文章 友链 关于我

温欣爸比

  • 主页
  • Alfred Workflow
  • 《Vim 练级手册》
  • 常用命令
  • 代码笔记
  • 合辑
  • 在线工具

2017-08-06 新建video和comment表,以及`insert_commet_trigger`

2017-08-06

今天的目标是新建一个video视频表和comment评论表,并编写trigger,达到当insert一条评论类型为video的数据到comment中时,video中comment_count字段可以自加一

专辑:mysql每天练习

  • 新建video表

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    drop table if exists `video`;
    create table `video`(
    id int(11) not null auto_increment,
    name varchar(32) not null comment '标题',
    url varchar(512) not null comment '视频地址',
    is_del int(1) not null default 0 comment '是否删除',
    create_ts timestamp not null default current_timestamp comment '创建时间',
    update_ts timestamp not null default current_timestamp on update current_timestamp comment '修改时间',
    primary key(`id`)
    )engine=InnoDB default charset=utf8mb4 comment '视频表';
  • 添加comment_count字段(不是忘了,就是想练一下add column语句╭(╯^╰)╮)

    1
    alter table video add column comment_count int(11) not null default 0 comment '评论数量';
  • 新建comment表

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    drop table if exists `comment`;
    create table `comment`(
    id int(11) not null auto_increment,
    resource_id int(11) not null comment '被评论资源id',
    resource_type varchar(16) not null comment '被评论资源类型',
    content varchar(1024) not null comment '评论内容',
    is_del int(1) not null default 0 comment '是否删除',
    create_ts timestamp not null default current_timestamp comment '创建时间',
    update_ts timestamp not null default current_timestamp on update current_timestamp comment '修改时间',
    primary key(`id`)
    )engine=InnoDB default charset=utf8mb4 comment '评论表';
  • 新建trigger

    1
    2
    3
    4
    5
    6
    7
    8
    9
    drop trigger if exists `insert_comment`;
    delimiter $
    create trigger `insert_comment` after insert on comment for each row
    begin
    if (new.resource_type = 'video') then
    update video set comment_count = comment_count +1 where id = new.resource_id;
    end if;
    end $
    delimiter ;
  • 执行相应操作语句

1
2
3
insert into video (name,url) values ('蜘蛛侠:返校季','http://woyemeiyou/wangzhi.mp4');
insert into comment (resource_id,resource_type,content) values (1,'video','找不到资源');
select * from video;

最后结果中video表中的comment_count应该等于1

最近更新
Alfred Workflow 命令行帮助工具
最近热读
Go 判断数组中是否包含某个 item
Vim 高级功能 vimgrep 全局搜索文件
办理北京工作居住证的一些细节
Go 语法错误:Non-declaration statement outside function body
Mac 电脑查看字体文件位置
扫码关注公众号,或搜索公众号“温欣爸比” 及时获取我的最新文章
赏

谢谢你请我喝咖啡

支付宝
微信
  • mysql
2017-08-07 新建user和user_data表,完善`insert_commet_trigger`
Mysql 基础命令
目录,不存在的…
© 2017 - 2022 温欣爸比 京ICP备15062634号 总访问量3251次 访客数3203人次 本文总阅读量1次
Hexo Theme Yilia by Litten
  • 所有文章
  • 友链
  • 关于我

tag:

  • python
  • flask
  • javascript
  • docker
  • 工具
  • openresty
  • 微信
  • java
  • hexo
  • 杂谈
  • vim
  • git
  • mysql
  • http
  • linux
  • mac
  • tmux
  • ssh
  • 算法
  • 开发
  • node
  • 杂文
  • jinja2
  • maven
  • spring
  • 北京
  • 生活
  • springboot
  • react
  • shell
  • graphql
  • iterm
  • expect
  • nginx
  • sqlalchemy
  • html
  • electron
  • vagrant
  • elastic
  • 宝贝
  • ansible
  • css
  • jquery
  • go
  • markdown
  • awk
  • redis
  • leetcode
  • zsh
  • 漫威
  • ssr
  • android
  • ffmpeg
  • chrome
  • vmware
  • youtube
  • windows
  • jupyter
  • excel
  • jq
  • Mac
  • Homebrew
  • mongo
  • py2
  • HomeBrew
  • movie
  • nodejs

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

  • Guru99
每天看书
每天背单词
每天一篇
写写代码
听听周杰伦
爱爱老婆