I'm using MySQL as database for online examination system. here Question No. is primary key so, when a question in middle is deleted that number is wasted,(just like in queue data structure).I want the next question numbers to be automatically decremented. is it possible by using PHP and MySQL. If yes, then please write the solution.
Asked
Active
Viewed 1,074 times
0
-
3do not update primary key. bad idea when you have other records referencing to it. – John Woo Aug 17 '13 at 18:12
-
1You should not do that. Also, duplicate of [how to keep a table field value in consecutive growing order throughout delete operations?](http://stackoverflow.com/questions/7625903/how-to-keep-a-table-field-value-in-consecutive-growing-order-throughout-delete-o) – trogdor Aug 17 '13 at 18:15
-
1possible duplicate of [how to reuse deleted primary keys in mysql?](http://stackoverflow.com/questions/2285906/how-to-reuse-deleted-primary-keys-in-mysql) – tereško Aug 17 '13 at 18:20
1 Answers
4
Please do not do this!
The number of the primary key has only one function: Uniquely identifying a record. It does not matter what number it is and if there are gaps in between.
If you want to sort your data use another column like a datetime column or an extra ranking index.
-
this does not answer the question and technically is an opinion. probably better keep that to a comment. And only one should not do it does not mean that you can't show how it can be done. Questioning the motivation of the person who asks does not qualify as an answer :D – hakre Aug 17 '13 at 18:36