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?
Move your if ‘ ‘ check inside id check.