pikemsu28 Posted December 21, 2006 Share Posted December 21, 2006 Is it possible to insert a record into the middle of a table allowing the auto inc id to adjust accordingly? example:Table: Color Schemesid(auto inc) value 1 value 21 Red Blue2 Blue Green3 Orange Black4 Black RedTable: Color Schemes (I need to insert a record between records 2 and 3)id(auto inc) value 1 value 21 Red Blue2 Blue Green3 Green Orange (inserted record and causes ID of following records to change)4 Orange Black5 Black RedI hope this makes sense.Thanks,Jason**Thanks to who all replied. Link to comment https://forums.phpfreaks.com/topic/31492-solved-insert-record-in-middle-of-table/ Share on other sites More sharing options...
artacus Posted December 21, 2006 Share Posted December 21, 2006 I want to say no, just because you shouldn't. Auto inc PK's aren't meant to be used like this. You could do this on the one table but it would mess up every table in your database that uses that id as a foreign key. Use instead a separate field, I usually call mine sort_order for that.Then before the insert:[code]UPDATE color_schemes SET sort_order = sort_order + 1 WHERE sort_order > 2[/code] Link to comment https://forums.phpfreaks.com/topic/31492-solved-insert-record-in-middle-of-table/#findComment-146033 Share on other sites More sharing options...
fenway Posted December 22, 2006 Share Posted December 22, 2006 There's no such thing as the "middle" of a table.. that's the first problem. Link to comment https://forums.phpfreaks.com/topic/31492-solved-insert-record-in-middle-of-table/#findComment-146563 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.