Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Database Server Software > Oracle > Synchronous Trigger

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-01-08, 08:22
scoobymib3 scoobymib3 is offline
Registered User
 
Join Date: Jul 2008
Posts: 3
Synchronous Trigger

Hi all,

Before I start please let me explain I am not a dba, Im a java developer so I have basic database skills but I am no expert so excuse me if this has been covered before or is an obvious question!

We have an application which is php front end and oracle db back end. This application is a simple data capture but it creates a reference number on the fly which is unique to each row entered. This reference number is a sequencial number based on two other columns with the same table.

There are columns area, year and reference number.
The reference number is a sequencial number which is the number of entrys into the database per area per year so at the moment I have a trigger that works out the number and enters it...

Code:
CREATE OR REPLACE TRIGGER TRIGGER1 BEFORE INSERT ON TBLREPORT FOR EACH ROW DECLARE v_max_ref number; v_year number; BEGIN v_year := to_number(to_char(current_date, 'YYYY')); SELECT nvl(max(ref), 0) INTO v_max_ref FROM TBLREPORT WHERE YEAR = v_year AND AREA = :new.AREA; :new.YEAR := v_year; :new.REF := v_max_ref + 1; END;


This works fine but if two people click save at the same time it repeats the reference number so it is not unique.

I need it to be unique but Im not sure about the best way of doing it. Should I implement a lock on the table before the insert??

Thanks in advance for your help, wish I had more time to try and figure it all out for myself!!

Cheers
Martin
Reply With Quote
  #2 (permalink)  
Old 07-01-08, 10:44
anacedent anacedent is offline
Registered User
 
Join Date: Aug 2003
Location: Where the Surf Meets the Turf @Del Mar, CA
Posts: 3,564
use a SEQUENCE
__________________
You can lead some folks to knowledge, but you can not make them think.
The average person thinks he's above average!
Reply With Quote
  #3 (permalink)  
Old 07-01-08, 11:16
scoobymib3 scoobymib3 is offline
Registered User
 
Join Date: Jul 2008
Posts: 3
The best way would be to create a sequence for each year and area?? I thought of that, just imagined there was better ways of doing it...
Reply With Quote
  #4 (permalink)  
Old 07-01-08, 12:06
anacedent anacedent is offline
Registered User
 
Join Date: Aug 2003
Location: Where the Surf Meets the Turf @Del Mar, CA
Posts: 3,564
Alternatively, place a UNIQUE constraint on the column.
If/when a duplicate occurs, trap the error & increment the value (again).
__________________
You can lead some folks to knowledge, but you can not make them think.
The average person thinks he's above average!
Reply With Quote
  #5 (permalink)  
Old 07-02-08, 05:01
scoobymib3 scoobymib3 is offline
Registered User
 
Join Date: Jul 2008
Posts: 3
The thing is none of the columns will be unique, the reference number will have several values the same in it as it would be dependent on the area and year aswell

for instance.....

AREA REF YEAR
AZ 1 08
BZ 1 08
AZ 2 08
FZ 1 08
Reply With Quote
  #6 (permalink)  
Old 07-02-08, 09:54
beilstwh beilstwh is offline
Registered User
 
Join Date: Jun 2004
Location: Liverpool, NY USA
Posts: 1,642
then set the unique index on all three columns.
__________________
Bill
Cream always raises to the top, and so does the scum!!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On