tags:

views:

50

answers:

4

OK, a database of information. A column dedicated to a display order. If I make changes, like move rows or if I delete rows or add rows to the end of the database then move them up or down.

I would prefer to keep the display order in sequential order with no gaps. like 1-2-3-4-5-6-7-8-9 if I delete a row 1-2-3-4-5-7-8-9 I may have problem later if I want to add a row after row 5 (in position 6). I would think to keep these numbers sequential with no gaps. Is that right? I have tried:

ALTER TABLE data DROP id;

ALTER TABLE data ADD id int(10) AUTO_INCREMENT;

I know that's not a good thing. Besides, when testing, it didn't keep my data in order.

Any ideas on managing a display order? Am I going to have to load all data into an array and manage it there? Seems like MySql would have a solution already???

A: 

I'm afraid AUTO_INCREMENT fields doesnt reorder them, you'd be best off adding a sort field.

Kristoffer S Hansen
+1  A: 

SQL has the "ORDER BY" syntax for this very purpose.

No reational database guarentees anything about the order the data is stored in or the order the data is retrieved in unless an " ORDER BY " is specified in the select statement.

If you simply want to preserve the order the data was entered in then a simple timestamp would acheive this.

James Anderson
Do you mean I can call a new order to the column? Like renumber it?
MP123
Maybe I need a column that never changes? When deleting rows, leave that column.
MP123
A: 

For this reason there is the "ORDER BY" command in queries:

SELECT * FROM 'TABLE' WHERE 'TABLE.FIELD' = 'XY' ORDER BY 'TABLE.FIELD';
Thariama
Yes, that is how I get the data. But what about UPDATE,INSERT and DELETE. When preforming those commands the display column is going to need adjustment. I would also use that column to reference desired placement of data. But if there are gaps, that could cause problems with rows that don't exist.
MP123
Well, there is no such thing in MYSQL (because there is no specified order by which results found are to be displayed). Sure, you can use a column for display and order by that column, but this is more or less expensive overhead. You can do it, but when you do UPDATE,INSERT and DELETE you will have to update the display column of possibly all the other entries.
Thariama
Well, I was thinking, what if I had a display column that never changed. If I DELETE a row of data, leave that column and just move everything down. If I INSERT, move everything up and INSERT in that spot. I suppose I could use the AUTO_INCREMENT column for that...
MP123
Which would mean that I won't be using DELETE or INSERT, but rather UPDATE.
MP123
i think if you use auto_increment for that column, then you should never change that column unless you want to alter the display order
Thariama
Here is a situation. I have 30 rows AUTO_INCREMENT, I DELETE 5 rows. I add another row and that row number is 31. Hence ...22-23-24-25-31 That's a gap.So here is what i do. I have another column for display order. When INSERT of new data. I use SELECT MAX(display_order) to find high number of display_order. When removing data, move everything down according to display_order and DELETE the high number of display_order, which would be empty.
MP123
what do you mean by "move everything down"?
Thariama
A: 

The solution I worked out was to have a column for displaying the data in order. I use the AUTO_INCREMENT column to keep track of the specific_id's for each entry.

The columns are: auto_id | display_order | specific_id | category | subject | text

When entering new data. I INSERT a new row to the end of the table. Then using mysql_insert_id() I retrieve the AUTO_INCREMENT number, I use SELECT MAX(display_order) to get the high number of the display_order, then UPDATE the new row with my data and put the AUTO_INCREMENT number in to the specific_id column because I actually need/use that number.

Using the display_order column as reference, I can move the information (specific_id | category | subject | text) to wherever. Instead of just changing the display_order number, I choose to move the information and leave the display_order column in sequence (mostly) with the AUTO_INCREMENT column. Just personal preference.

When deleting a row. Again, using the display_order I move the information down (numerically) and then DELETE the last row of the table. Again, personal preference, keeps things neat.

The benefits are: 1) Everything is in one table. 2) I use the AUTO_INCREMENT number to give each data entry a specific_id, which was a must have. 3) No gaps in the display_order column, makes finding/referencing, calculating positions and shuffling the information error free.

Other notes: When referencing chunks of data I use "ORDER BY display_id" and "ORDER BY display_id DESC" to keep things in order.

MP123