burner Dragiša Rakić BG
Član broj: 898 Poruke: 150 *.183.EUnet.yu
|
InterBase 6 ima autoincrement polja, i to oni interno zovu generatori. Evo ti deo iz originalnog uputstva:
Creating generators
To create a unique number generator in the database, use the CREATE GENERATOR statement. CREATE GENERATOR declares a generator to the database and sets its starting value to zero (the default). If you want to set the starting value for the generator to a number other than zero, use SET GENERATOR to specify the new value.
The syntax for CREATE GENERATOR is:
CREATE GENERATOR name;
The following statement creates the generator, EMPNO_GEN:
CREATE GENERATOR EMPNO_GEN;
Note Once defined, a generator cannot be deleted.
Setting or resetting generator values
SET GENERATOR sets a starting value for a newly created generator, or resets the value of an existing generator. The new value for the generator, int, can be an integer from –264 to 264– 1. When the GEN_ID() function is called, that value is int plus the increment specified in the GEN_ID() step parameter.
The syntax for SET GENERATOR is:
SET GENERATOR NAME TO int;
The following statement sets a generator value to 1,000:
SET GENERATOR CUST_NO_GEN TO 1000;
IMPORTANT Don’t reset a generator unless you are certain that duplicate numbers will not occur. For example, a generators are often used to assign a number to a column that has PRIMARY KEY or UNIQUE integrity constraints. If you reset such a generator so that it generates duplicates of existing column values, all subsequent insertions and updates fail with a “Duplicate key” error message.
USING GENERATORS
Using generators
After creating the generator, the data definition statements that make the specific number generator known to the database have been defined; no numbers have been generated yet. To invoke the number generator, you must call the InterBase GEN_ID() function.
GEN_ID() takes two arguments: the name of the generator to call, which must already be defined for the database, and a step value, indicating the amount by which the current value should be incremented (or decremented, if the value is negative). GEN_ID() can be called from within a trigger, a stored procedure, or an application whenever an INSERT,
UPDATE, or DELETE operation occurs.
The syntax for GEN_ID() is:
GEN_ID( genname, step);
GEN_ID() can be called directly from within an application or stored procedure using INSERT, UPDATE, or DELETE statements. For example, the following statement uses GEN_ID() to call the generator G to increment a purchase order number in the SALES table
by one:
INSERT INTO SALES (PO_NUMBER) VALUES (GEN_ID(G,1));
A number is generated by the following sequence of events:
1. The generator is created and stored in the database.
2. A trigger, stored procedure, or application references the generator with a call
to GEN_ID().
3. A generator returns a value when a trigger fires, or when a stored procedure
or application executes. It is up to the trigger, stored procedure, or
application to use the value. For example, a trigger can insert the value into
a column.
IMPORTANT: Generators return a 64-bit value. You should define the column that holds the generated value as an ISC_INT64 variable (DECIMAL or NUMERIC datatype).
|