[ale] Getting the next index of a table in mysql

Michael B. Trausch fd0man at gmail.com
Thu Jun 9 08:36:42 EDT 2005


-----BEGIN PGP SIGNED MESSAGE-----
Hash: RIPEMD160

James Sumners wrote:
> It would be better to do the INSERT first and then get the number.
> That way you can be sure that it is the correct number. But, "SELECT
> pri_key_num + 1 FROM table ORDER BY pri_key_num DESC LIMIT 1;"
> 

Also, note that this will only work if there are zero deletions from the
table in question.  If you make a deletion in a table, and you don't
re-order the table by pri_key_num, then eventually you will find that
you're performing INSERTs that aren't any longer using the correct row
ids, and you'll have gaps in numbering if you use any other method to
add a number.

Checking the new Row ID after an insert is the only real way to go about it.

	- Mike

- --
Michael B. Trausch                                     fd0man at gmail.com
Web: http://fd0man.theunixplace.com/        Jabber: mtrausch at jabber.com
Phone: +1-(678)-522-7934                  FAX (US Only): 1-866-806-4647
=======================================================================
  Do you have PGP or GPG?  Key at pgp.mit.edu, Please Encrypt E-Mail!


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFCqDVWPXInbkqM7nwRA8CDAJ0VdF6T7s7ll93/wKScKIqoxiXGdwCeKgeH
/eiTjnrIkApxNRBfTsmRGPI=
=xfuC
-----END PGP SIGNATURE-----



More information about the Ale mailing list