?
Solved

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

Posted on 2004-05-03
6
Medium Priority
?
855 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
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
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
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…
Suggested Courses

840 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