Pero un registro nuevo igualmente puede tener un c贸digo libre, eso lo permite MySql.
Espero haber sido claro.
English:
But a new record can still have a free code, that is allowed by MySql. I hope I was clear.
Yes.
But this is a very unsafe practice for regular insertion of records.
We can use this feature in rate occasions like,
1) fill gaps in auto-inc values due to deletions
2) reseed the auto-increment value to a higher value. Even for this, it is more appropriate to use ALTER TABLE command than inserting a higher value.
El GetAutoIncrement(cTable) lo reemplace por una funcion propia simple:
FUNCTION GetAutoIncrement(cTable,cField)
RETURN oApp:oServer:QueryResult('SELECT MAX('+cField+') FROM ' + cTable) + 1
It is not as simple as that. Even MySql server does not know the next autoinc value, till the time it actually inserts the record in the table.
Between the moment server answers the query "SELECT MAX(fld) FROM table" and the time the new record is actually inserted, several other users might have already inserted several records and this insertion will result in an error.
Note: We are not discussing single user applications here.
Let us see this example. There is a small table with these records.
ID FLD1 FLD2
1 AA AAAA
2 BB BBBB
ID is the auto-increment primary field.
User-A and User-B (and may be more users) are running our application inserting new transactions.
Both users A and B (and also other users) will assume the next autoinc id will be 3, using the above function.
Mr. A is about to commit his new record with this sql statement
INSERT INTO table (ID,FLD1,FLD2) VALUES ( 3, 'CC','CCCC' )
Just a split second before, User-B inserts his new data with
INSERT INTO table (ID,FLD1,FLD2) VALUES ( 3, 'DD','DDDD' )
User-B's insertion will be successful.
But User-A's insertion fails with an error.
Note: The problem remains the same even if we read auto_increment value from the table_schema.
So, the safe practice as well as widely adopted practice is to insert the record without any value to auto-inc field and then get the LAST_INSERT_ID to inform the user.
By default, the RowSet class treats auto-increment field as read-only. TDatarow inherts the readonly attribute from the RowSet object.
In my next post I will explain how to over-ride this default behavior.