I am trying to create a trigger that only allows the user to insert the same value into a column after the first insert has been made. For example in the table below the user has entered Name = Jake and age = 29.
Name | Age |
---|---|
Jake | 29 |
From this point onwards, the trigger on the name column should now only allow the user to enter the Name = Jake.
Name | Age |
---|---|
Jake | 29 |
Jake | 36 |
Code below is what I tried but I am getting a ORA-04091 error but I imagine this is because I am referencing the table itself in the trigger? Although I’m not sure
CREATE TRIGGER my_table
BEFORE INSERT OR UPDATE OF name ON my_table
FOR EACH ROW
DECLARE
a_CurrName VARCHAR2(20BYTE);
BEGIN
SELECT name INTO a_CurrName FROM my_table WHERE ROWNUM=1;
IF a_CurrName IS NOT NULL THEN
IF :new.name != a_CurrName
THEN
RAISE_APPLICATION_ERROR( -20001, 'This table can only hold one unique name at any point' );
END IF;
END IF;
END;