Quote:
The MAX(itemno) is to show the newest record. (The item is stored in a sequential fashion, thus the highest in the database will also be the newest entry and the one that needs to be compared with the rest of the database)
|
This is a very tricky assumption you make. Did you think about updates ? Suppose your table contains two rows :
ItemNo Title
1 First_title
2 Second_title
Now suppose somebody updates row identified by ItemNo 1, and sets the title colum to "Second_Title". Does your trigger still do the job ? What about bulk inserts ? Did you notice that your trigger only fires after each statement. I can do a million inserts in your table and only have the trigger fired once...
You also probably will run into the notorious "ORA-04091 table is mutating".
The only proper way to enforce uniqueness is to use a constraint. But, if you still want to use a trigger, you might want to try (you need Oracle version 8.1.5 or higher):
create or replace trigger UPDATEITEM
AFTER INSERT OR UPDATE OF title ON ITEM
for each row -- verifies all inserted or updated rows !!
declare
pragma autonomous_transaction; -- circumvent ORA-04091
v_count number;
begin
select count(*) into v_count from item where title = :new_title;
if v_count > 0 then raise_application_error(-20000,'ITEM ALREADY EXISTS IN DATABASE'); end if;
end;