ferpadro Posted September 15, 2007 Share Posted September 15, 2007 I have a table "services" with 8 fields: serviceid (auto_increment) technicianid typeofserviceid clientid date hour hours comments Every time i add, say, 5 fields, the serviceid reaches 5. But if i erase one of them, or all of them, and then insert another row, the value of serviceid won´t replace the serviceid value for the old row. Instead of that, if i had 5 values, next row will be inserted with serviceid = 6. Is there any way to fix this? Another problem i have is difficulty while trying to maintain integral reference. I established a composite key with the first four fields, but because serviceid changes automatically from row to row, its making incongruences with the rows inserted To make me clear, lets say i add this two fields ('','2','1','3','2007-12-26','9:37','58','no comment'). If i add another row with the same values, the DBMS wont throw any error and insert the row. They won´t be identical to the DB engine because they diferentiate in theirs id. But two rows with the same values is not correct in real life. One technician can´t be doing the same type of service to the same client at the same day in the same hour of the day. I dont now how to fix this. Any ideas? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/69470-how-to-restart-id-count-from-a-auto_increment-field-when-deleting-a-row/ Share on other sites More sharing options...
darkfreaks Posted September 15, 2007 Share Posted September 15, 2007 code please? Quote Link to comment https://forums.phpfreaks.com/topic/69470-how-to-restart-id-count-from-a-auto_increment-field-when-deleting-a-row/#findComment-349056 Share on other sites More sharing options...
tomfmason Posted September 15, 2007 Share Posted September 15, 2007 If you are deleting all of records in the table you could do TRUNCATE TABLE `your_table` and that will reset the auto_increment. You could also use something like SET INSERT_ID = 2; and then do your insert. Here is a simple example. $query = "SET INSERT_ID = 2; INSERT INTO `your_table`(`foo`,`bar`) VALUES ('foo', 'bar');" That would insert with an id of 2 Quote Link to comment https://forums.phpfreaks.com/topic/69470-how-to-restart-id-count-from-a-auto_increment-field-when-deleting-a-row/#findComment-349059 Share on other sites More sharing options...
ferpadro Posted September 15, 2007 Author Share Posted September 15, 2007 I could include pieces of the code but i dont think its an issue involving the code. I thought it could be done by activating some options in the phpmyadmin control panel. I cant do what you suggested tomfmanson coz i am not able to predict what id will have the future inserted row Quote Link to comment https://forums.phpfreaks.com/topic/69470-how-to-restart-id-count-from-a-auto_increment-field-when-deleting-a-row/#findComment-349143 Share on other sites More sharing options...
rarebit Posted September 15, 2007 Share Posted September 15, 2007 see http://dev.mysql.com/doc/refman/5.0/en/truncate.html It's just something you have to live with... Quote Link to comment https://forums.phpfreaks.com/topic/69470-how-to-restart-id-count-from-a-auto_increment-field-when-deleting-a-row/#findComment-349145 Share on other sites More sharing options...
rarebit Posted September 17, 2007 Share Posted September 17, 2007 This might help: alter table "table_name" auto_increment=1 http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html Quote Link to comment https://forums.phpfreaks.com/topic/69470-how-to-restart-id-count-from-a-auto_increment-field-when-deleting-a-row/#findComment-349928 Share on other sites More sharing options...
tomfmason Posted September 17, 2007 Share Posted September 17, 2007 This might help: alter table "table_name" auto_increment=1 http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html The code that I posted will do this without having to alter the table $query = "SET INSERT_ID = 2; INSERT INTO `your_table`(`foo`,`bar`) VALUES ('foo', 'bar');" That would insert with an id of 2 Quote Link to comment https://forums.phpfreaks.com/topic/69470-how-to-restart-id-count-from-a-auto_increment-field-when-deleting-a-row/#findComment-350209 Share on other sites More sharing options...
Barand Posted September 17, 2007 Share Posted September 17, 2007 see http://www.phpfreaks.com/forums/index.php/topic,140306.msg596840.html#msg596840 Quote Link to comment https://forums.phpfreaks.com/topic/69470-how-to-restart-id-count-from-a-auto_increment-field-when-deleting-a-row/#findComment-350218 Share on other sites More sharing options...
sasa Posted September 18, 2007 Share Posted September 18, 2007 for prevent duplicate row you can add unique index to your table ALTER TABLE `services` ADD UNIQUE `my_index` ( technicianid, typeofserviceid, clientid, date, hour, hours, comments ) Quote Link to comment https://forums.phpfreaks.com/topic/69470-how-to-restart-id-count-from-a-auto_increment-field-when-deleting-a-row/#findComment-350490 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.