是否可以在 HSQLDB 中丢弃特定条件下的插入?

发布于 2024-10-05 07:02:44 字数 294 浏览 0 评论 0原文

我需要通过某些条件限制新行的插入。如果可插入值与特定条件不匹配,数据库必须抛出异常。 首先我尝试使用 CONSTRAINT 对象,但出现以下错误: “SQLException:不支持功能:语句中检查约束中的子查询”。

然后我尝试使用触发器:

CREATE TRIGGER tg_val BEFORE INSERT ON valueta 将新行引用为新行 对于每一行,当(new.simvol 为空) SIGNAL SQL_STATE '45000'

并收到另一个错误:SQLException:意外标记:SIGNAL。

I need to restrict insertion of new rows by certain conditions. The database have to throw an exception if insertable values mismatch certain condition.
First I've tried to use CONSTRAINT object, but got the following error:
"SQLException: feature not supported: subquery in check constraint in statement".

Then I tried to use the trigger:

CREATE TRIGGER tg_val BEFORE INSERT ON valuta
REFERENCING NEW ROW AS new
FOR EACH ROW WHEN (new.simvol IS NULL)
SIGNAL SQL_STATE '45000'

And got another error: SQLException: unexpected token: SIGNAL.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红ご颜醉 2024-10-12 07:02:44

该错误可能表明所使用的 HSQLDB 版本不支持此上下文中的 SIGNAL 语句。 HSQLDB 2.0.1(目前 RC2)通过 SQLSTATE 支持这一点(不是指南中当前提到的 SQL_STATE):

CREATE TRIGGER tg_val BEFORE INSERT ON valuta REFERENCING NEW ROW AS new FOR EACH ROW WHEN (new.simvol IS NULL) SIGNAL SQLSTATE '45000'

从您尝试对子查询使用检查约束可以清楚地看出,您希望使用比以下更复杂的检查条件NOT NULL 约束。在这种情况下,请使用触发器的 BEGIN ... END 块,以便从不同表(包括目标表)执行任何 SELECT 语句来验证您的条件。

The error probably indicates the version of HSQLDB used does not support the SIGNAL statement in this context. HSQLDB 2.0.1 (at the moment RC2) supports this, with SQLSTATE (not SQL_STATE as currently mentioned in the Guide) :

CREATE TRIGGER tg_val BEFORE INSERT ON valuta REFERENCING NEW ROW AS new FOR EACH ROW WHEN (new.simvol IS NULL) SIGNAL SQLSTATE '45000'

It is clear from your attempt to use a check constraint with subquery that you want to use a more complex check condition than a NOT NULL constraint. In this case, use a BEGIN ... END block for the trigger in order to perform any SELECT statement from different tables (including the target table) to verify your condition.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文