views:

160

answers:

2

Hi i want to know that how the auto increment id can be get from the mysql db for two fields in a table

+4  A: 

In MySQL you're only allowed 1 auto_increment column per table.

If you try and create two you'll get:

Incorrect table definition; there can be only one auto column and it must be defined as a key

Greg
+1  A: 

Well, there are two options, but I am not sure how useful it would be (I would just use the primary key auto-increment to achieve my needs).

  1. Using the Database: Use a trigger on the insert to increment a field value.

  2. Using PHP: Two ways, both not so pretty:
    a. grab the previous row before the insert, and increment the field in the insert.
    b. If you are basing it off of the created auto-increment, you can do the insert, get the insert_id, and then update the second field.

Once again, still not sure why you would need it, but those are the options.

Cryophallion