gpt4 book ai didi

mysql - MySQL 或 SQLite 是否支持 'INSERT OR UPDATE' 语句?

转载 作者:行者123 更新时间:2023-11-29 01:24:25 25 4
gpt4 key购买 nike

如果我没记错的话,Oracle 中可以使用“INSERT OR UPDATE”语句。如果主键不存在,则可以创建新行,否则可以更新该行。或者只能使用某些存储过程或存储函数?

最佳答案

MySQL:

INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name [(col_name,...)]
{VALUES | VALUE} ({expr | DEFAULT},...),(...),...
[ ON DUPLICATE KEY UPDATE
col_name=expr
[, col_name=expr] ... ]

对于 SQLite,您可以使用 ON CONFLICT REPLACE

onconflict - 请参阅http://www.sqlite.org/lang_conflict.html

REPLACE When a UNIQUE constraint violation occurs, the REPLACE algorithm deletes pre-existing rows that are causing the constraint violation prior to inserting or updating the current row and the command continues executing normally. If a NOT NULL constraint violation occurs, the REPLACE conflict resolution replaces the NULL value with he default value for that column, or if the column has no default value, then the ABORT algorithm is used. If a CHECK constraint violation occurs, the REPLACE conflict resolution algorithm always works like ABORT.

When the REPLACE conflict resolution strategy deletes rows in order to satisfy a constraint, delete triggers fire if and only if recursive triggers are enabled.

The update hook is not invoked for rows that are deleted by the REPLACE conflict resolution strategy. Nor does REPLACE increment the change counter. The exceptional behaviors defined in this paragraph might change in a future release.

关于mysql - MySQL 或 SQLite 是否支持 'INSERT OR UPDATE' 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6105619/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com