If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > SQL triggers

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-02-11, 04:08
uzipaz uzipaz is offline
Registered User
 
Join Date: May 2011
Posts: 5
Red face SQL triggers

Hello World,
I have table in my oracle database where store the data regarding property, the primary key is an ID that is an integer itself. So, whenever I want to insert a tuple in this table, I want its ID to be incremented before its inserted. So, I have made a table in my database using TOAD software. After this I wrote the following statements in the S.Q.L Editor.

CREATE SEQUENCE property_id_incrementer start with 0 increment by 1 minvalue 0;

CREATE TRIGGER increment_property_id
ON PROPERTY
BEFORE INSERT
AS
BEGIN
PROPERTY.PROPERTY_ID=property_id_incrementer.nextv al // I am not sure if this statement is correct!
END
GO;

The sequence is created successfully, but the trigger won't, I am sure the assignment is not correct. Should I use the 'INSERT INTO TABLE' statement here?

Another question, suppose I have five tuples in that table with ID's ranging from 1 to 5. Now, I delete the 3rd tuple with ID=3, the result would be four tuples with IDs=1,2,4,5. However, this should'nt happen this way, the IDs should be 1,2,3,4. Right?

How will handle this case?

Thanks.
Reply With Quote
  #2 (permalink)  
Old 05-02-11, 04:36
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
First: you should have posted this in the Oracle forum.

If you want to change a column's value in the trigger you need to use

Code:
:NEW.property_id := property_id_incrementer.nextval;
And please use [code] tags in the future to make your SQL code readable.

Quote:
However, this should'nt happen this way, the IDs should be 1,2,3,4. Right?
No. The value of the primary key does not have any meaning whatsoever.

Those IDs could just as well be 213264,35485,34,68757646
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

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