?
Solved

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

Posted on 2004-05-03
6
Medium Priority
?
852 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 2

Expert Comment

by:edk137
ID: 10977139

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
ID: 10979799
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
ID: 10981061
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
Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

 
LVL 2

Expert Comment

by:edk137
ID: 10981135

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 500 total points
ID: 10983472
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
ID: 10985676
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Suggested Courses

764 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