Link to home
Start Free TrialLog in
Avatar of jabootyboy
jabootyboy

asked on

How to create an oracle trigger to stop repition in a table.

I am trying to write an oracle trigger that when fired stops an insert into a table; it is fired if that insert is a repeation of an already inserted row, what I have is:

CREATE TRIGGER no_repition_lecturer
BEFORE INSERT OR UPDATE ON lecturer
FOR EACH ROW WHEN(new.forname=old.forname AND new.surname=old.surname)
BEGIN
      :new.forname=NULL AND new.surname=NULL AND lid#=NULL

END;
 
Avatar of edk137
edk137


It has no sense to spent effort for something ORACLE can do for you much better. Create a unique index on columns you are concerned with (probably you want forname and surname) to be unique and ORACLE will always work for you.

If you still want to do it by itself (may be the existing data are not unique already, or whatever), take into account:

If you will try to check the existence of similar row directly in the trigger you will probably meet the "mutating table" problem that you better should avoid.

Instead of using triggers it could be better to create a stored procedure that will first check if a row exists and only then insert it.

If you still looking for answer to your origianl question be adviced that any exception inside the trigger will cause failure of your operations.

You can use RAISE <exception_name> in your code when you want. <exception_name> is a variable in your trigger of type EXCEPTION.
Avatar of jabootyboy

ASKER

Cheers ed,

Unfortunatly it is not going to be possible to create a unique index as there will be repetions in both columns. As to the stored procedure this is sounds like our best chance;

The table contains 3 fields,    forname,  surname,  lid  want i am trying to achieve is that if on insert  forname && surname are repeated in an already stored row it will be disaloud.

I am wondering if something as following can work in your case:

INSERT INTO lecturer
SELECT forname_value,  surname_value,  lid_value
FROM DUAL
WHERE NOT EXISTS
(SELECT NULL
  FROM   lecturer
 WHERE forname = forname_value
   AND surname = surname_value
   AND lid = lid_value
);

Actually, it should be:

INSERT INTO lecturer
SELECT forname_value,  surname_value,  lid_value
FROM DUAL
WHERE NOT EXISTS
(SELECT NULL
  FROM   lecturer
 WHERE forname = forname_value
   AND surname = surname_value
);
ASKER CERTIFIED SOLUTION
Avatar of annamalai77
annamalai77

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
annamalai is right,

use both columns together as a unique index.

this way forname can have repeated values and surname can have repeated values, but the combination of forname and surname must be unique.

doing this kind of thing in a trigger is not highly suggested, you will most likely run into more trouble than its worth.

the alternative is the stored procedure as stated above, but by creating a unique index it saves a lot of work.