Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, A.

Asked: May 01, 2012 - 9:30 pm UTC

Last updated: June 15, 2018 - 1:14 am UTC

Version: 11gR1

Viewed 1000+ times

You Asked

Hi Tom,
Thanks for your time.

We have the scenario like this...

DML on the table t should not populate the table with duplicate entries.

So if we have table with data in it as :

create table t ( x number);

insert into t values (1);

insert into t values (2);


commit;


then
I cannot insert the values 1 or 2 - because these already exists.
I cannot update these values 1 to 2 or 2 to 1 - this will create duplicate entries.


Actually , if we enable unique constraint then job is done.. but application is not allowing the same... i.e. due to unique constraint error .. application is stopping as execption is not handled.. it would take some time to change the code but immediate requirement is to avoid the duplicate entries in the table t.

so something like trigger deleting INSERTED row if the inserted data is violating the uniqueness or trigger updating the new updated values with the old values if new updated data is violating the uniqueness.

we used the trigger to achieve this it is resulting into table mutation.

I know the best way is to enforce the unique constraint, but that is not the need of the hour.

many thanks for your help.

regards




and Tom said...

Actually , if we enable unique constraint then job is done.. but application is not allowing the same... i.e. due to unique constraint error .. application is stopping as execption is not handled.. it would take some time to change the code but immediate requirement is to avoid the duplicate entries in the table t. ,

what they heck????

You *cannot* be serious. This has to be a troll. This cannot be for real.

You have a program, a piece of software, that has a definite and serious bug. A huge bug.

And you want to try to make this bug disappear by magic? Really? Are you serious?

What would the application do if it THINKS it inserted something - but it really didn't? How would that work? Seriously? How would that be better than what you have now - a program that fails when it should fail because it is utterly broken?


I know the best way is to enforce the unique constraint, but that is not the need of the hour.


it is the need of the hour, your approach to fixing this is beyond scary, beyond comprehension, beyond anything I've ever heard of. This one takes "bad idea" to a brand new level.


rename the table
put the constraint on the table
create a view of the table using the old table name
create an instead of trigger on the view that handles the duplicate issue

and sit back and watch the fireworks really go off now - watch the bug evolve into an even worse bug.

Rating

  (3 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Table might already have duplicates

A reader, May 03, 2012 - 6:08 am UTC

It seems table is already have duplicates, so not allowing unique constraint.
Tom Kyte
May 03, 2012 - 9:05 am UTC

so, umm, fix it?

You sound like a jerk.

Tony, June 12, 2018 - 7:26 pm UTC

I don't think it was constructive to assume that you know the business impact of him modifying his application. He didn't ask you for your opinion on if his architecture was sound. He asked how to solve a problem and all you did was try to make him feel dumb. Your response was weak and didn't answer his question. I remember when this forum was invaluable... not anymore.
Connor McDonald
June 13, 2018 - 2:01 am UTC

If you have similar critique on anything made in the past couple of year, then sure, we'll cop it on the chin and do better.

But it seems a little unfair to judge the current team on a post made 6 years ago?


You are correct...

Tony, June 14, 2018 - 6:15 pm UTC

Your point is valid. I used to live on this site and just didn't like a question not being answered and what seemed to be a remark that was unnecessary. But I do agree with your reaction. My bad. And finally... I guess it was dumb to respond at all after that much time had passed. I should have paid more attention to the date.
Connor McDonald
June 15, 2018 - 1:14 am UTC

Hey, no drama. We appreciate all feedback - good and bad.

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library