Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
how to use InterBase generators for AutoIncrement fields Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
Using InterBase generators for AutoIncrement fields 25-Aug-02
Category
Database Others
Language
Delphi 2.x
Views
104
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas 

Using InterBase generators for AutoIncrement fields

Answer:

InterBase doesn't offer the convenient AutoIncrement datatype as some desktop 
database systems (MS-Access, Parados) do. In a project I simulated this for a 
unique index field by using a trigger combined with a generator.

The example below assumes that there is a table CUSTOMER with a uniquely indexed 
field CUST_HASH.
The generators' name is GEN_CUSTOMER.

The traditional technique would be to detect the current maximum number max and 
then insert a value of [max+1]:

SELECT MAX(cust_hash) + 1 FROM customer
INSERT INTO customer(...)values(...)

The risk with this approach is that a parallel user could theoretically do the same 
thing before you write the determined value and end the transaction. The parallel 
user would try to post the same number and either cause a unique-index violation or 
post a duplicated value!

The trick with the generator is also faster since you don't have to do the max() 
query for each insert.
1   
2   CREATE GENERATOR gen_customer;
3   
4   set GENERATOR gen_customer to 100;
5   
6   CREATE TRIGGER customer_autoinc for customer
7     BEFORE INSERT as
8   begin
9     if (NEW.cust_hash is NULL) then
10      NEW.cust_hash = GEN_ID(gen_customer, 1);
11  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC