Delphi32.com - Home!
| Home/News | Downloads | Forums | D32 Magazine | Resources | Info and Facts |  
 
 Auto-Increment fields in InterBase


    I have an InterBase table, but I can’t figure out how to make an Auto-Increment field for it. How do I do this?   

        InterBase doesn’t have a convenient AutoInc datatype like Paradox, so we have to do a little bit of work to get this functionality. Start out by creating an Integer field that is appropriate for the size of the values you want in your AutoInc field. From there, we need a trigger to calculate the value for the field:

    CREATE TRIGGER autoinc FOR tablename BEFORE INSERT AS BEGIN NEW.autoinc_field_nane = GEN_ID(generator_name, ); END

        We aren’t done yet. We need a persistent location to store the current AutoInc value. InterBase’s way to deal with this is called a generator:

    CREATE GENERATOR generator_name; SET GENERATOR generator_name TO x;

        GEN_ID() is a built-in InterBase function specially designed to increment the generator specified by the first parameter by the value in the second parameter. The SET GENERATOR statement initializes the generator to the value provided by x. This is useful if your database is based on something like a customer number, and you want this customer number to start at a specific value rather than zero.

        And that is all there is to it. From here on, you can insert away and this trigger will stick the correct value into autoinc_field_name. For further information on how to work with Auto-Increment fields in your application, read the TI "Working With Auto-increment Field Types" at http://community.borland.com/article/0, 4 0, 6 23,00.html.

     



  << Previous Faq     Complete List     Next Faq >>  



 
 Hits/month  2,500,000+ 
 Downloads
 (Since May 2000)
 7,393,709 
 Total Files  6,023 
 Forum msgs  7,670 
 Articles/FAQs  70+/900+ 
Kylix
Tips n Tricks
FAQs
Knowledge Base
Bug Listings
Articles
Books
Newsgroups
Links
Submissions
Testimonials
Advertising
Contact Us
About Us
Search Amazon:
Top Selling Software at Amazon

| Home/News | Downloads | Forums | Resources | Info and Facts | Testimonials |
  Site Search:
 


Comments/Problems: Webmaster@delphi32.com
Copyright © 1998-2006, Delphi32.com. All rights reserved.
Terms of Use