Dumb_Shock
Dumb_Shock

Reputation: 1008

Incrementing a field in SQL using PHP

I have a table (T1) with some fields F2, F3 (Columns) with already some values in it .... .

I need to add a new field (F1) to the table (T1) and F1 will be the primary key .

The field type F1 is a integer .

I assigned a random 8 digit number to the first row of F1 and the remaining rows should be incremented by 1 from the previous row. This increment should go in par with the other fields F2 , F3 etc...

This F1 should also be increased if entirely new row is added to the table.

I tried to do it with lastinsertid() in PHP but it only works for rows which are newly created.

Can u ppl help me in writing the code for the above using lastinsertid().

If its not clear plzz let me know..

Upvotes: 0

Views: 147

Answers (1)

ypercubeᵀᴹ
ypercubeᵀᴹ

Reputation: 115650

If you are using MySQL:

ALTER TABLE myTable
AUTO_INCREMENT = 1677216 ,
ADD COLUMN F1 INT PRIMARY KEY AUTO_INCREMENT ;

Existing rows will have the F1 field automatically filled with ids, starting from 1677216.


If you are worried that it is an integer and not a string, pretend it's a string by never using id, only use RIGHT(id,8)

Upvotes: 4

Related Questions