HSQLDB:替换成
HSQLDB: REPLACE INTO
发件人:
HyperSQL 用户指南
HyperSQL 数据库引擎 2.4.0
Chapter 12. Compatibility With Other DBMS :
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. 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.
但是当我尝试
REPLACE INTO my_table (my_id, my_int) VALUES (1, 2);
我明白了
unexpected token: REPLACE required: INSERT
这是为什么?
我建议您需要启用 MySQL 兼容模式才能使 MySQL 特定命令(如 REPLACE
)正常工作。来自 HSQL 的第 7 章 documentation:
In MySQL syntax compatibility mode, HyperSQL supports INSERT IGNORE, REPLACE and ON DUPLICATE KEY UPDATE variations of the INSERT statement.
这里的重点是MySQL语法兼容模式需要开启。根据您在问题中发布的 link to Chapter 12,我们发现:
Use SET DATABASE SQL SYNTAX MYS TRUE or the equivalent URL property sql.syntax_mys=true to enable support for AUTO_INCREMENT and TEXT data types and several other types. These type definitions are translated into HyperSQL equivalents.
因此文档为我们提供了两种启用 MySQL 兼容模式的方法。一个我们可以直接从 HSQL 控制台执行:
SET DATABASE SQL SYNTAX MYS TRUE
另一个可能用于开发目的的方法是将以下内容添加到连接字符串中:
sql.syntax_mys=true
启用 MySQL 兼容模式后,REPLACE
应该可以正常工作。
发件人:
HyperSQL 用户指南
HyperSQL 数据库引擎 2.4.0
Chapter 12. Compatibility With Other DBMS :
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. 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.
但是当我尝试
REPLACE INTO my_table (my_id, my_int) VALUES (1, 2);
我明白了
unexpected token: REPLACE required: INSERT
这是为什么?
我建议您需要启用 MySQL 兼容模式才能使 MySQL 特定命令(如 REPLACE
)正常工作。来自 HSQL 的第 7 章 documentation:
In MySQL syntax compatibility mode, HyperSQL supports INSERT IGNORE, REPLACE and ON DUPLICATE KEY UPDATE variations of the INSERT statement.
这里的重点是MySQL语法兼容模式需要开启。根据您在问题中发布的 link to Chapter 12,我们发现:
Use SET DATABASE SQL SYNTAX MYS TRUE or the equivalent URL property sql.syntax_mys=true to enable support for AUTO_INCREMENT and TEXT data types and several other types. These type definitions are translated into HyperSQL equivalents.
因此文档为我们提供了两种启用 MySQL 兼容模式的方法。一个我们可以直接从 HSQL 控制台执行:
SET DATABASE SQL SYNTAX MYS TRUE
另一个可能用于开发目的的方法是将以下内容添加到连接字符串中:
sql.syntax_mys=true
启用 MySQL 兼容模式后,REPLACE
应该可以正常工作。