Subject | Re: [firebird-support] Auto Incrementing field |
---|---|
Author | Yves Glodt |
Post date | 2003-12-09T19:30:28Z |
On Tuesday 09 December 2003 15:41, Bahadir ARMAGAN wrote:
create table TEST123 (
ID INTEGER NOT NULL PRIMARY KEY,
NAME VARCHAR(100)
);
and you want ID to be autoincremented.
Follow these steps:
1. Create a generator:
CREATE GENERATOR MY_GEN;
2. Set it's value to the higest ID already used in your DB:
SET GENERATOR MY_GEN TO 1234;
(if you omit this step, ID will start at 1 with the first insert)
3. Use this syntax when you insert a record:
INSERT INTO TEST123 (ID,NAME) VALUES (GEN_ID(MY_GEN,1),'blablabla');
Find a record with ID=1235 and NAME='blablabla' in your DB.
(Of course you could also use a trigger. With a trigger you would not
need to specify the ID column in the insert statement (See message from
Richard Pendered), so your insert would be like this:
INSERT INTO TEST123 (NAME) VALUES ('blablabla');
Youself decide whether this is an advantage for you or not.)
regards,
Yves Glodt
Linux 2.4.22-1-k7 #1 Sat Sep 6 02:13:04 EST 2003 i686
20:14:41 up 46 min, 1 user, load average: 0.05, 0.17, 0.27
> I have an integer field that an application increments it each timeok, assume this table:
> it adds a record. But when I run the intances of that application at
> the same time , I get an error because all the instances reading the
> same last value and trying to insert the same incremented value to
> this unique field.
>
> What is the general methode that is to be followed for this problem?
> Is it possible to create an autoincrement field as in access
> databases? What if I try to make a field (already have unique recodrs
> in it) autoincrement?
create table TEST123 (
ID INTEGER NOT NULL PRIMARY KEY,
NAME VARCHAR(100)
);
and you want ID to be autoincremented.
Follow these steps:
1. Create a generator:
CREATE GENERATOR MY_GEN;
2. Set it's value to the higest ID already used in your DB:
SET GENERATOR MY_GEN TO 1234;
(if you omit this step, ID will start at 1 with the first insert)
3. Use this syntax when you insert a record:
INSERT INTO TEST123 (ID,NAME) VALUES (GEN_ID(MY_GEN,1),'blablabla');
Find a record with ID=1235 and NAME='blablabla' in your DB.
(Of course you could also use a trigger. With a trigger you would not
need to specify the ID column in the insert statement (See message from
Richard Pendered), so your insert would be like this:
INSERT INTO TEST123 (NAME) VALUES ('blablabla');
Youself decide whether this is an advantage for you or not.)
regards,
Yves Glodt
> TRANSVARO Elektron Aletleri San. ve Tic. A.S.--
>
> Address: Fatih Cad. Dereboyu Sok. No:12
> 34660 Halkali - Istanbul - Turkey
> Phone: +90 (212) 473 0100
> Fax: +90 (212) 473 0155
> URL: http://www.transvaro.com.tr
> E-Mail: mail@...
>
> NOT: Bu elektronik posta mesaji gizli olup, ayni zamanda ayricalikli
> olabilir; sartlar ne olursa olsun, gondericinin onceden onayini
> almaksizin, baskalarina iletmemeniz, icerigini kopyalamamaniz veya
> baskalarina aciklamamaniz gerekmektedir. Bu mesajin muhatabi
> degilseniz, bu durumu derhal tarafimiza bildirmenizi rica ederiz.
> Internet üzerinden yapilan iletisim guvenli olmayip, verilerin kasten
> veya tesadufen bozulmasi ve virus icermesi olasiligi vardir. Ayrica,
> elektronik posta, resmi olmayan ve cogunlukla kisaltilmis bir
> iletisim yontemidir. Dolayisiyla, burada yer alan bilgi veya
> tavsiyelere, gonderici ile ayrica gorusmeden guvenmeniz normal
> sartlar altinda uygun olmayabilir.
>
> NOTE: This e-mail is confidential and may also be privileged; under
> no circumstances should you forward it, or copy or disclose its
> contents, to any other person without the prior consent of the
> sender. If you are not an intended recipient of this e-mail, please
> notify us immediately. Internet communications are not secure and
> subject to possible data corruption, either accidentally or on
> purpose, and may contain viruses. Furthermore e-mail is an informal
> and often abbreviated method of communication. For these reasons, it
> will normally be inappropriate to rely on any nformation or advice
> contained herein without also discussing it with the sender.
>
>
>
> [Non-text portions of this message have been removed]
>
>
>
> ------------------------ Yahoo! Groups Sponsor
> ---------------------~--> Buy Ink Cartridges or Refill Kits for your
> HP, Epson, Canon or Lexmark Printer at MyInks.com. Free s/h on orders
> $50 or more to the US & Canada.
> http://www.c1tracking.com/l.asp?cid=5511
> http://us.click.yahoo.com/mOAaAA/3exGAA/qnsNAA/67folB/TM
> ---------------------------------------------------------------------
>~->
>
> To unsubscribe from this group, send an email to:
> firebird-support-unsubscribe@yahoogroups.com
>
>
>
> Your use of Yahoo! Groups is subject to
> http://docs.yahoo.com/info/terms/
Linux 2.4.22-1-k7 #1 Sat Sep 6 02:13:04 EST 2003 i686
20:14:41 up 46 min, 1 user, load average: 0.05, 0.17, 0.27