不使用重复键MYSQL进行Upsert(Upsert without using on duplicate key MYSQL)

编程入门 行业动态 更新时间:2024-10-17 00:29:49
不使用重复键MYSQL进行Upsert(Upsert without using on duplicate key MYSQL)

我正在尝试编写一个mysql语句,无需使用主键即可将其插入表中。

我知道on duplicate key命令,但我不能在这里使用它,因为我正在检查两列不是主键的唯一性。 我知道最好只生成这两个主键,但我不能,因为这是给出的模式。

架构如下所示:

tbl_order_detail

key_order_detail key_order key_product some_other_keys

如果key_order,key_product对是唯一的,那么我会进行常规插入。 如果它们不是唯一的,那么我更新行。

有什么建议?

I'm trying to write a mysql statement that upserts into a table without having to use the primary key.

I know of the on duplicate key command but I can't use it here since I'm checking for the uniqueness of two columns that aren't primary keys. I know it would be better to just make these two primary keys, but I can't since this was the schema that was given.

The schema looks like this:

tbl_order_detail

key_order_detail key_order key_product some_other_keys

If the key_order,key_product pair is unique then I do a regular insert. If they aren't unique then I update the row.

Any suggestions?

最满意答案

ON DUPLICATE KEY UPDATE不仅适用于主键,还适用于任何唯一约束。

因此,只需为(key_order, key_product)列创建复合唯一索引并使用它。

ON DUPLICATE KEY UPDATE works not only with primary keys, but with any unique constraints.

So just create a composite unique index for (key_order, key_product) columns and use it.

更多推荐

本文发布于:2023-07-22 09:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219118.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Upsert   MYSQL   key   duplicate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!