Solved

oracle constraints on tables

Posted on 2013-01-21
10
363 Views
Last Modified: 2013-01-23
Hi,

I have a tricky question about constraints and primary keys.

lets say i have a table which has the following structure.

Table name : Events

eventid    integer,
type          varchar2(300),
thetime    date,
description varchar2(3000)


so i have a table that stores different types of events that occur at certain times.
Generally speaking the events should be unique by time but there is an exception that one type of event can have multiple entries at the same time with the same description.  Is there a way i can have a unique constraint with exceptions or a check constraint that checks for uniqueness.  I dont really want to do it in a trigger as that will slow down the inserts significantly.  This is a simplified extract of a very large database with a very complex structure and ui so performance is important.
0
Comment
Question by:cesarchavez
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 14

Expert Comment

by:ajexpert
Comment Utility
You just need to handle UNIQUE CONSTRAINT VIOLATION exception in the code.  So with unique constraint violation exception don't take any action

--pseudo code
BEGIN
INSERT INTO EVENTS (....) VALUES (....)

EXCEPTION
  WHEN <unique constraint violation err>
      NULL;
  WHEN OTHERS THEN
       DBMS_OUTPUT.PUT_LINE ('OTHER EXCEPTION ' ||SQLERRM);
END;

Open in new window

0
 

Author Comment

by:cesarchavez
Comment Utility
im not sure how that helps me.  I cant change the UI side.  so the person doing the inserts shouldnt know any different.  All he knows is that when he tries to insert duplicate event type A it works and when he inserts duplicate event type B it doesnt.  So i must allow duplicates into the database...i cant even create the unique constraint at this stage.
0
 
LVL 14

Expert Comment

by:ajexpert
Comment Utility
can you provide example (data) of duplicate event type A that works, and event type B that doesnt? and what error is raised
0
 

Author Comment

by:cesarchavez
Comment Utility
i just want to contrain my data to support this requirement.

so. everything in the table below should be able to be inserted without error

eventid, type, time, description
1, A, 1, ok
2, A, 1, ok
3, A, 2, ok
4, B, 1, ok

but now when i try and inser

5, B, 1, ok

it should fail because my table is allowed duplicate entries of A but not of B.  Hope that helps
0
 
LVL 14

Expert Comment

by:ajexpert
Comment Utility
Don't think Oracle has any constraint supporting your business case. May be other experts can comment
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
If you can't change the UI then you have to add code to the database.

I also don't think Oracle has this type of a constraint out of the box.  I beleive you will need to handle this in code with a trigger.

I'm not sure if this is a faster trigger but wanted to throw it out there:  two tables, a view and an instead of trigger.

Create a table for event A that allows dupes.  Create a table for event B with the unique constraint.  Create a view that is a union all against both tables.

You can then create an instead of trigger that checks the value for the event and inserts into the correct table.  Then the unique constraint on table B should handle the dupes.

It might be faster than a normal trigger.
0
 

Author Comment

by:cesarchavez
Comment Utility
not sure what to do here where no solution is available.... do i close the question or award points?
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
Leave it open for a while.  Some of our Experts are in different time zones and are busy.  Some of them may not have had a chance to see this.

Also,  we provided the answer you asked for.  Just not the one you were looking for.

Did you check the performance of instead of triggers over a normal trigger?

Check out:
http://www.experts-exchange.com/help/viewHelpPage.jsp?helpPageID=23

"You can't do that"
Sometimes, you will get an answer that isn't what you want to read, but it still may be the correct answer, and you should award points to the Expert that gave you that information.
0
 
LVL 36

Accepted Solution

by:
Geert Gruwez earned 500 total points
Comment Utility
is the data parent to other records ?
or part of a bigger transaction ?

if not, set up a job which runs every so often to delete the offending records
 this is in background, no impact on any front end

i admit it will look rather odd to the frontend user seeing his records disappearing after some time.
0
 

Author Closing Comment

by:cesarchavez
Comment Utility
I didn't think of this.  Great idea and is will work in my scenario.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
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 videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

728 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