Solved

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

Posted on 2004-05-03
6
842 Views
Last Modified: 2012-06-27
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;
 
0
Comment
Question by:jabootyboy
6 Comments
 
LVL 2

Expert Comment

by:edk137
Comment Utility

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.
0
 

Author Comment

by:jabootyboy
Comment Utility
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.

0
 
LVL 2

Expert Comment

by:edk137
Comment Utility
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
);
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 2

Expert Comment

by:edk137
Comment Utility

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
);
0
 
LVL 8

Accepted Solution

by:
annamalai77 earned 125 total points
Comment Utility
hi my friend

u can create an unique index including both the columns forname and surname

create unique index forsurname on <tablename> (column1, column2) tablespace <tablespace name>

the above will solve ur problem instead of writing a db trigger.

regards
annamalai
0
 
LVL 2

Expert Comment

by:solman66
Comment Utility
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.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now