Oracle 中 INSERT 重复主键的更新?
我有一个简单的 INSERT 查询,当主键重复时,我需要使用 UPDATE 来代替。在 MySQL 中这似乎更容易,在 Oracle 中似乎我需要使用 MERGE。
我能找到的 MERGE 的所有示例都有某种“源”和“目标”表,在我的例子中,源和目标是同一个表。我无法理解这些示例来创建我自己的查询。
MERGE 是唯一的方法还是也许有更好的解决方案?
INSERT INTO movie_ratings
VALUES (1, 3, 5)
基本上就是这样,主键是前 2 个值,所以更新会是这样的:
UPDATE movie_ratings
SET rating = 8
WHERE mid = 1 AND aid = 3
我想到使用一个触发器,它会在调用 INSERT 时自动执行 UPDATE 语句,但前提是主键是重复的。这样做有什么问题吗?我需要一些有关触发器的帮助,因为我在尝试理解它们并自己做事时遇到一些困难。
I have a simple INSERT query where I need to use UPDATE instead when the primary key is a duplicate. In MySQL this seems easier, in Oracle it seems I need to use MERGE.
All examples I could find of MERGE had some sort of "source" and "target" tables, in my case, the source and target is the same table. I was not able to make sense of the examples to create my own query.
Is MERGE the only way or maybe there's a better solution?
INSERT INTO movie_ratings
VALUES (1, 3, 5)
It's basically this and the primary key is the first 2 values, so an update would be like this:
UPDATE movie_ratings
SET rating = 8
WHERE mid = 1 AND aid = 3
I thought of using a trigger that would automatically execute the UPDATE statement when the INSERT was called but only if the primary key is a duplicate. Is there any problem doing it this way? I need some help with triggers though as I'm having some difficulty trying to understand them and doing my own.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
MERGE 是标准 SQL 中的“根据需要执行 INSERT 或 UPDATE”语句,因此在 Oracle SQL 中也可能如此。
是的,您需要一个“表”来合并,但您几乎肯定可以动态创建该表:(
语法未验证。)
MERGE is the 'do INSERT or UPDATE as appropriate' statement in Standard SQL, and probably therefore in Oracle SQL too.
Yes, you need a 'table' to merge from, but you can almost certainly create that table on the fly:
(Syntax not verified.)
执行此操作的典型方法是
您不能在表上编写触发器来对表执行其他操作同一张桌子。这会导致 Oracle 错误(表变化)。
A typical way of doing this is
You can't write a trigger on a table that does another operation on the same table. That's causing an Oracle error (mutating tables).
我是一个 T-SQL 专家,但在这种情况下触发器并不是一个好的解决方案。大多数触发器都不是好的解决方案。在 T-SQL 中,我只需执行 IF EXISTS (SELECT * FROM dbo.Table WHERE ...),但在 Oracle 中,您必须选择计数...
在这种情况下,您需要 MERGE:
就像我说的,我是一个 T-SQL 人员,但这里的基本思想是“加入” movie_ rating 表本身。如果使用“如果存在”示例不会影响性能,我会使用它来提高可读性。
I'm a T-SQL guy but a trigger in this case is not a good solution. Most triggers are not good solutions. In T-SQL, I would simply perform an IF EXISTS (SELECT * FROM dbo.Table WHERE ...) but in Oracle, you have to select the count...
It would appear that MERGE is what you need in this case:
Like I said, I'm a T-SQL guy but the basic idea here is to "join" the movie_rating table against itself. If there's no performance hit on using the "if exists" example, I'd use it for readability.