温欣爸比

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

温欣爸比

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

Mysql insert ... on duplicate key update 的用法及在 SQLAlchemy 中的使用

2019-02-24

在项目高并发时,很容易出现数据库插入相同数据的情况,虽然可以使用唯一索引避免插入相同数据,但是不断的程序报错也是我们也避免的。



  • 语法
  • SQLAlchemy 中的使用

语法

使用 insert ... on duplicate key update .. 语法可以避免这种情况,举个例子

1
2
3
4
5
6
7
drop table if exists `test`;
create table `test` (
`id` int(11) not null AUTO_INCREMENT,
`name` varchar(32) not null default '',
`update_ts` timestamp not null default current_timestamp(),
primary key (`id`)
) engine=InnoDB default charset=utf8mb4;

主键 id 是天然的唯一索引,我们插入重复数据时会报错

1
2
3
> INSERT INTO test (id, name) VALUES (1, 'wxnacy');
> INSERT INTO test (id, name) VALUES (1, 'wxnacy');
Error 1062: Duplicate entry '1' for key 'PRIMARY'

查看数据

1
2
3
4
> SELECT * FROM `test`;
| id | name | update_ts |
|------------------------------------
| 1 | wxnacy | 2019-02-24 12:26:58 |

下面我们来换个语句

1
2
3
4
5
6
7
8
> insert into test (id, name) values (1, 'wxnacy') on duplicate key update update_ts = current_timestamp();
> SELECT * FROM `test`;

+----+--------+---------------------+
| id | name | update_ts |
+----+--------+---------------------+
| 1 | wxnacy | 2019-02-24 12:31:49 |
+----+--------+---------------------+

on duplicate key update 前面是正常的插入语句,其后跟着的是当唯一索引冲突时,想要更新的数据。

再换个使用场景,如果我想让数据库中用户名是唯一的,则可以先建立唯一索引,在使用该语法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
> alter table test add unique index_name (name);
> insert into test (name) values ('wenn') on duplicate key update update_ts = current_timestamp();
> SELECT * FROM `test`;
+----+--------+---------------------+
| id | name | update_ts |
+----+--------+---------------------+
| 1 | wxnacy | 2019-02-24 12:31:49 |
| 2 | wenn | 2019-02-24 12:39:25 |
+----+--------+---------------------+

> insert into test (name) values ('wenn') on duplicate key update update_ts = current_timestamp();
> SELECT * FROM `test`;

+----+--------+---------------------+
| id | name | update_ts |
+----+--------+---------------------+
| 1 | wxnacy | 2019-02-24 12:31:49 |
| 2 | wenn | 2019-02-24 12:40:15 |
+----+--------+---------------------+

这样及保证了避免插入重复数据,同时程序也没有报错,我还可以根据 update 的数据来分析问题的根源。

SQLAlchemy 中的使用

这个功能需要 SQLAlchemy 1.2 版本以上才支持,官方的例子

1
2
3
4
5
6
7
8
9
10
11
from sqlalchemy.dialects.mysql import insert

insert_stmt = insert(my_table). \
values(id='some_id', data='some data to insert')

on_conflict_stmt = insert_stmt.on_duplicate_key_update(
data=insert_stmt.inserted.data,
status='U'
)

conn.execute(on_conflict_stmt)

析出的 sql 语句为

1
2
3
INSERT INTO my_table (id, data)
VALUES (:id, :data)
ON DUPLICATE KEY UPDATE data=VALUES(data), status=:status_1

参考资料

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

谢谢你请我喝咖啡

支付宝
微信
  • mysql
  • sqlalchemy
Vim8 内置插件管理方案
Vimscript 判断文件是否存在
  1. 1. 语法
  2. 2. SQLAlchemy 中的使用
© 2017 - 2022 温欣爸比 京ICP备15062634号 总访问量3247次 访客数3199人次 本文总阅读量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
每天看书
每天背单词
每天一篇
写写代码
听听周杰伦
爱爱老婆