|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
| Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
Easiest way to create an auto incrementing column?
Using FB - is there a built in way to create an auto incrementing column? If not, what is the easiest way to do this? I am guessing using a Generator - is this correct?
Thanks
__________________
~ Joe Penn |
|
#2
|
||||
|
||||
|
Yes you use a Generator.
For example: Code:
INSERT INTO SALES (PO_NUMBER) VALUES (GEN_ID(generator_name, 1)); Code:
/* This stored procedure returns a generated integer */ CREATE PROCEDURE GEN_BATCH_ID returns (ID_VALUE Integer) AS BEGIN ID_VALUE = gen_id(BATCH_ID_GEN, 1); SUSPEND; END Code:
/* Send this procedure an Employee's name. Get back the Employee's ID number. */
CREATE PROCEDURE EMPLOYEE_INSERT (LAST_NAME VARCHAR(25), FIRST_NAME VARCHAR(25)) returns (EMPLOYEE_ID Integer)
AS
BEGIN
EMPLOYEE_ID = GEN_ID(EMPLOYEE_ID_GEN, 1);
INSERT INTO EMPLOYEE (
EMPLOYEE_ID,
LAST_NAME,
FIRST_NAME)
VALUES (
:EMPLOYEE_ID,
:LAST_NAME,
:FIRST_NAME);
END
Last edited by dcaillouet : July 11th, 2003 at 02:19 PM. |
|
#3
|
||||
|
||||
|
Great - thanks dman- i thought it would be by means of the generator - just wasn't sure if there was already something built in to the package or not...
PS - appreciate the great examples ![]() |
|
#4
|
|||
|
|||
|
??
Quote:
so how do we get the generator_name?? Please help.. |
|
#5
|
||||
|
||||
|
You create it, so you know that name.
Note that you can put a trigger to do it for you, i.e. you insert nulls and it adds the value taken from the generator for you. Find more infos in this document
__________________
My blog Tutorials about OSS databases, DBMonster ... Contribute to OSS Development, fill bug reports! Developer Shed eSupport Commented my.ini/my.cnf (ADD YOUR OWN CONFIG TRICK) An introduction to database normalization Natural or Surrogate key Custom ordering for your results Correlated and uncorrelated subqueries Don't turn your outer joins into inner joins Random data (with a bias) |
|
#6
|
|||||
|
|||||
|
Posting here as a reference for others:
Say you have a table like this sql Code:
You create a generator (you could set more options). Now you want it to kick in automatically, just like an autoincrement, no need to explictly call it, you can achieve this through a trigger: |
![]() |
| Viewing: Dev Shed Forums > Databases > Firebird SQL Development > Easiest way to create an auto incrementing column? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|