SQLite“插入或替换"与“更新......在哪里" [英] SQLite "INSERT OR REPLACE INTO" vs. "UPDATE ... WHERE"

查看:36
本文介绍了SQLite“插入或替换"与“更新......在哪里"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前从未见过 SQL 中使用的语法 INSERT OR REPLACE INTO names (id, name) VALUES (1, "John"),我想知道为什么它比 更新名称 SET name = "John" WHERE id = 1.是否有充分的理由使用一个而不是另一个.此语法是否特定于 SQLite?

I've never seen the syntax INSERT OR REPLACE INTO names (id, name) VALUES (1, "John") used in SQL before, and I was wondering why it's better than UPDATE names SET name = "John" WHERE id = 1. Is there any good reason to use one over the other. Is this syntax specific to SQLite?

推荐答案

如果该行不存在,UPDATE 不会做任何事情.

UPDATE will not do anything if the row does not exist.

如果行不存在,INSERT OR REPLACE 将插入,如果存在则替换值.

Where as the INSERT OR REPLACE would insert if the row does not exist, or replace the values if it does.

这篇关于SQLite“插入或替换"与“更新......在哪里"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆