"Insert if not exists" 在 MySQL 和 HSQLDB 中

"Insert if not exists" in both MySQL and HSQLDB

MySQL 和 HSQLDB 是否都支持 SQL 语句(或语句的原子序列)来插入值(如果它们还不存在)?

我正在开发一个使用 MySQL 作为其生产数据库并使用 HSQLDB 进行单元测试的应用程序;我想要一个 "initial data import when the tables are empty" 脚本。

MySQL支持INSERT IGNOREREPLACE INTOINSERT INTO ... ON DUPLICATE KEY UPDATE ...,但HSQLDB不支持;相反,HSQLDB 支持 MERGE 但 MySQL 不支持。

2.3.4 版的 HSQLDb 添加了对 insert ignore.

的支持

http://hsqldb.org/

Version 2.3.4 added the UUID type for columns, SYNONYM for tables and functions, PERIOD predicates, and auto-updated TIMESTAMP columns on row updates. Other new features included the ability to cancel long-running statements from JDBC as well as from admin sessions, and UTF-16 file support for text table sources, in addition to 8-bit text files. MySQL compatibility for REPLACE, INSERT IGNORE and ON DUPLICATE KEY UPDATE statements.

http://hsqldb.org/doc/guide/guide.pdf(第 260 页)。

HyperSQL supports and translates INSERT IGNORE, REPLACE and ON DUPLICATE KEY UPDATE variations of INSERT into predictable and error-free operations. When INSERT IGNORE is used, if any of the inserted rows would violate a PRIMARY KEY or UNIQUE constraint, that row is not inserted. With multi-row inserts, the rest of the rows are then inserted only if there is no other violation such as long strings or type mismatch, otherwise the appropriate error is returned. When REPLACE or ON DUPLICATE KEY UPDATE is used, the rows that need replacing or updating are updated with the given values. This works exactly like an UPDATE statement for those rows. Referential constraints and other integrity checks are enforced and update triggers are activated. The row count returned is simply the total number of rows inserted and updated.

如果有人仍然遇到此问题,您可以通过将以下内容添加到您的脚本

来启用对 MySQl 的语法支持
SET DATABASE SQL SYNTAX MYS TRUE