gargoylemusic Posted July 15, 2006 Share Posted July 15, 2006 My question is fairly simple. There seem to be two ways of incrementing the 'ID' field of a new record. One is to find the maximum ID, add one, and use that new value as the ID. Or one can use an auto-incrementing field. Why would someone use one method versus another? When is auto-incrementing not appropriate? Link to comment https://forums.phpfreaks.com/topic/14678-new-user-ids-use-max-or-auto-increment/ Share on other sites More sharing options...
fenway Posted July 15, 2006 Share Posted July 15, 2006 UID fields should always be auto-increment.. MAX() is unreliable, because you don't have consistent reads in MySQL with MyISAM table without transactions, so you can't be sure that you're adding one to the "last" record. Auto-incrementing is always appropriate for uids; sometime count=count+1 makes sense for other fields, but the same warning applies. Link to comment https://forums.phpfreaks.com/topic/14678-new-user-ids-use-max-or-auto-increment/#findComment-58539 Share on other sites More sharing options...
pixy Posted July 15, 2006 Share Posted July 15, 2006 AUTO_INCREMENT is the best, as fenway said, for ids. Link to comment https://forums.phpfreaks.com/topic/14678-new-user-ids-use-max-or-auto-increment/#findComment-58619 Share on other sites More sharing options...
gargoylemusic Posted July 16, 2006 Author Share Posted July 16, 2006 [quote author=fenway link=topic=100678.msg397727#msg397727 date=1152978386]UID fields should always be auto-increment.. MAX() is unreliable, because you don't have consistent reads in MySQL with MyISAM table without transactions, so you can't be sure that you're adding one to the "last" record. Auto-incrementing is always appropriate for uids; sometime count=count+1 makes sense for other fields, but the same warning applies.[/quote]Then should I use an InnoDB table (and then use transactions?) I mean... is there any advantage to that (why does't everybody use auto_increment?) Link to comment https://forums.phpfreaks.com/topic/14678-new-user-ids-use-max-or-auto-increment/#findComment-58856 Share on other sites More sharing options...
fenway Posted July 16, 2006 Share Posted July 16, 2006 Everyone does use auto-increment... MAX() is meant for other things, and transactions come with a huge overhead. Link to comment https://forums.phpfreaks.com/topic/14678-new-user-ids-use-max-or-auto-increment/#findComment-58893 Share on other sites More sharing options...
gargoylemusic Posted July 17, 2006 Author Share Posted July 17, 2006 When I do that, the only thing unique about the record would be the auto-incremented ID; does that mean I don't have to wrap it in a transaction? (If two people create accounts at the same time... MySQL won't assign the same auto-id?)(pardon my ignorance... if you have any decent tutorials on this kind of thing, please point me to the url) Link to comment https://forums.phpfreaks.com/topic/14678-new-user-ids-use-max-or-auto-increment/#findComment-59261 Share on other sites More sharing options...
fenway Posted July 17, 2006 Share Posted July 17, 2006 That's correct... auto-increment is per table, and is connection & thread safe, as is LAST_INSERT_ID(). Link to comment https://forums.phpfreaks.com/topic/14678-new-user-ids-use-max-or-auto-increment/#findComment-59540 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.