如何编写允许空值同时仍然检查引用完整性的 mySQL 触发器?

发布于 2024-11-16 01:27:31 字数 1381 浏览 2 评论 0原文

我正在 phpMyAdmin 3.3.9 和 mySQL 5.5.8 中工作。我有一个包含 myISAM 表的数据库,因此我必须编写一个触发器来检查两个表字段之间的引用完整性。我有一个表project_key,其中主键列为process ID 字段。第二个表是 hvi,它还包含进程 ID 字段。 hvi 表中的进程 ID 也必须位于 project_key 表中。因此,我编写了一个触发器来显示错误(如果情况并非如此),如下所示:

create trigger t1
before insert   
on hvi
for each row
begin
declare dummy int;
. . .
if new.`process id` not in(select `process id` from `project_key`) then 
select `Process ID not in project_key`
into dummy
from hvi
where `KEY`= new.`KEY`;
end if;
end;

如果进程 ID 不为空,则效果很好。当它捕获尝试导入 hviproject_key 表中尚不存在的进程 ID 时,会显示错误“未知” “字段列表”中的“进程 ID 不在 project_key”中”就像编码那样。

当我没有一行数据的进程 ID 时(这种情况很常见),就会出现问题。我的数据记录将有一个空格,因此我希望触发器将该空格转换为空值,然后接受该空值。我尝试像这样编写代码,但它不起作用:

create trigger nullt1
before insert   
on hvi
for each row
begin
declare dummy int;
if new.`Process ID` = ' ' then
set new.`Process ID` = null;
end if;
. . .
if new.`process id` not in(select `process id` from `project_key`) or null then 
select `Process ID not in project_key`
into dummy
from hvi
where `KEY`= new.`KEY`;
end if;
end;

我在黑暗中拍摄了允许空部分的照片,因为我以前从未尝试过类似的事情(我已经将空格转换为之前的空值并且有效)。这个触发器显然完全不起作用,因为当我尝试导入带有空格的行时,它仍然给出“‘字段列表’中的未知列‘进程ID不在project_key’”错误。

有谁知道我怎样才能做到这一点?

I'm working in phpMyAdmin 3.3.9 with mySQL 5.5.8. I have a database with myISAM tables so I had to write a trigger to check referential integrity between two table fields. I have a table project_key with the primary key listed as the process ID field. The second table is hvi and it also contains the process ID field. The process IDs in the hvi table must also be in the project_key table. So I wrote a trigger to display an error if this is not the case, which looks like this:

create trigger t1
before insert   
on hvi
for each row
begin
declare dummy int;
. . .
if new.`process id` not in(select `process id` from `project_key`) then 
select `Process ID not in project_key`
into dummy
from hvi
where `KEY`= new.`KEY`;
end if;
end;

If the process ID is not null this works just fine. When it catches a process ID that is trying to be imported into hvi that isn't already in the project_key table is displays the error "Unknown column 'Process ID not in project_key' in 'field list'" like it's coded to do.

The problem comes in when I do not have a process ID for a row of data (which is a common occurrence). My record of data will have a blank space, so I want the trigger to convert that blank space to a null value and then accept this null value. I tried to code it like this, but it didn't work:

create trigger nullt1
before insert   
on hvi
for each row
begin
declare dummy int;
if new.`Process ID` = ' ' then
set new.`Process ID` = null;
end if;
. . .
if new.`process id` not in(select `process id` from `project_key`) or null then 
select `Process ID not in project_key`
into dummy
from hvi
where `KEY`= new.`KEY`;
end if;
end;

I was taking a shot in the dark with the allowing the null part, because I've never tried anything like that before (I've done the converting blank spaces to null values before and that works). This trigger in it's entirety obviously didn't work though becasue when I tried to import a row with the blank space it still gives me my "Unknown column 'Process ID not in project_key' in 'field list'" error.

Does anyone know how I can make this work?

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

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

发布评论

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

评论(1

戏蝶舞 2024-11-23 01:27:31

将 if ' ' 检查移至 id 检查内。

if new.`process id` not in(select `process id` from `project_key`) then 
  if new.`process id` = ' ' then
    set new.`Process ID` = null;
  else
    select `Process ID not in project_key`
    into dummy
    from hvi
    where `KEY`= new.`KEY`;
  end if;
end if;

Move your if ' ' check inside id check.

if new.`process id` not in(select `process id` from `project_key`) then 
  if new.`process id` = ' ' then
    set new.`Process ID` = null;
  else
    select `Process ID not in project_key`
    into dummy
    from hvi
    where `KEY`= new.`KEY`;
  end if;
end if;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文