0
votes

The trigger currently only gets triggered when an existing record is modified.

create or replace TRIGGER library
BEFORE update ON books
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
  WHEN ( new.author != old.author
      ) DECLARE

I am confused whey it's not getting triggered for a new record because old.author would be equal to null so it would new.author != null, which is true. How do I get the trigger to work for new records and existing record modifications?

I have tried modifying it to check for insert and IS NULL

create or replace TRIGGER library
BEFORE update or insert ON books
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
  WHEN ( old.author IS NULL or old.author = '' OR new.author != old.author 
      ) DECLARE

but it's still not working.

It would help to indicate which dialect of SQL this is (Sybase? Oracle?). There may be any number of issues here. Generally triggers should be avoided. Generally speaking NULL's behave differently to othe scalars. You have to use a different operator. new.author != NULL and new.author = NULL are both always false. Instead use this syntax: new.author IS NOT NULL or new.author IS NULLNick.McDermaid
new records do you mean newly inserted and you code doesn't look like oralcle plsql, but you can add aflow control and check first for null and else if it has anvalue independent ob whoch database you are usingnbk