I have seen many questions on SO regarding this question but none is relevant to my scenario. I am nowhere near an SQL guy apart from doing basic CRUD operations. Hence I am quite stuck with this.
I have a table.
myTable [rID, newsID, OrderPosition] where;
rID is primaryKey int column,
newsID int is the ID of an item from another table and,
OrderPosition int to hold the position of the rows.
myTable will always have a total of 10 rows.
So, initially, lets assume myTable has the following data:
rID newsID OrderPosition
100 4000 1
101 4100 2
102 4200 3
103 4300 4
104 4400 5
105 4500 6
106 4600 7
107 4700 8
108 4800 9
109 4900 10
The expected functionality should be as follows;
INSERT NEW
when inserting a new item, user should be able to insert it into any position he/she desires. Right now, I only managed to insert the new record to the first position by deleting the OrderPosition = 10 row, assigning the new record OrderPosition of 0, and reorder the table. But client wants to select which position the item should go. In which case I assume, the OrderPosition = 10 will be deleted again?
DELETE
When a record from this table is deleted, since there will always be a total of 10 records, I need to get the latest entered record from another table [tblNews] and insert it to the 10th position (I can get the last record from tblNews by ordering descending by the date it was entered.) Since I don't know which record they will delete, I don't know how to re-order the table after a record has been deleted.
Any help, code, direction to an article would be very much appreciated.
=========== EDIT ====================
The UPDATE method mentioned in the answers will not work for me since; e.g. user wants to insert a new record into the 5th order position. This would mean, the order position 10 would be deleted and the current records with order postions 5,6,7,8 and 9 is to be incremented by one
ORDER BYclause on the outermostSELECTthat generates the result set. Is that compatible with what you're trying to do?