温欣爸比

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

温欣爸比

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

2017-08-09 新建collection合集表,新建trigger完成更新用户文章数

2017-08-09

今天练习新建collection合集表,并在article中添加user_id字段,在user_data中添加article_count字段,并新建trigger

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

专辑:[mysql每天练习](/?p=/doc/mysql/mysql_daily.md)


- 新建collection
```mysql
drop table if exists `collection`;
create table `collection`(
id int(11) not null auto_increment,
name varchar(256) not null default '' comment '合集名称',
is_del tinyint(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 '合集表';

  • article添加user_id

    1
    alter table article add column user_id int(11) not null default 0 comment '用户id';
  • 中添加```article_count```字段
    1
    2
    ```mysql
    alter table user_data add column article_count int(11) not null default 0 comment '发布文章数量';
  • 新建trigger

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    drop trigger if exists `insert_article`;
    delimiter $
    create trigger `insert_article` after insert on article for each row
    begin
    declare count int;
    if (new.user_id > 0) then
    select count(0) into count from user_data where user_id = new.user_id;
    if (count > 0) then
    update user_data set article_count = article_count +1 where user_id = new.user_id;
    else
    insert into user_data (user_id,article_count) values (new.user_id,1);
    end if;
    end if;
    end $
    delimiter ;
  • 运行

    1
    2
    3
    insert into article (name,url,user_id) values ('title','http://baidu.com',3);
    insert into article (name,url,user_id) values ('title','http://baidu.com',3);
    select * from user_data where user_id = 3;

运行后,article_count应该等于2

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

谢谢你请我喝咖啡

支付宝
微信
  • mysql
pyenv 常用命令
Mysql 索引的增删查
目录,不存在的…
© 2017 - 2022 温欣爸比 京ICP备15062634号 总访问量3632次 访客数3583人次 本文总阅读量2次
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
每天看书
每天背单词
每天一篇
写写代码
听听周杰伦
爱爱老婆