way to inserting data into a MYSQL table data in an ordered way

Reply

Join Date: Sep 2006
Posts: 22
Reputation: komrad is an unknown quantity at this point 
Solved Threads: 1
komrad's Avatar
komrad komrad is offline Offline
Newbie Poster

way to inserting data into a MYSQL table data in an ordered way

 
0
  #1
Jan 7th, 2009
Hello all,

I'd like to know if there is a way to insert into a MYSQL table data in an ordered way?
Right now as a workaround I've literally made a table of questions with questionID as primary key, nextQuestion and prevQuestion pointing to other questionIDs. Basically I've made a two way linked list from scratch. Is there anything built into MYSQL that implements this?

Thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 165
Reputation: Fest3er is an unknown quantity at this point 
Solved Threads: 18
Fest3er Fest3er is offline Offline
Junior Poster

Re: way to inserting data into a MYSQL table data in an ordered way

 
0
  #2
Jan 9th, 2009
I don't know if there is anything built into MySQL to do this. It's been a while since I've had to 'track' autoincrement values, but that's where you can look. But you can get close using autoincrement.

Basically, you create a table where the question_id is an auto-increment value. You would insert the first question using, for example, -1 for the previous_id and -1 for the next_id. For the rest of the questions, you would insert the question using LAST_INSERT_ID() for the previous_id and -1 for the next_id, then you would update the record with question_id=LAST_INSERT_ID()-1 and set its next_id to LAST_INSERT_ID().

This will work as long as nothing else is inserting rows into the table. I believe autoincrement is guaranteed to increment by one with each record inserted. You could create many separate lists of questions as long as you don't interrupt the insert process. (I don't know if there's a way to reset the LAST_INSERT_ID value.)

Want pseudo-pseudo code?

  1. LOCK TABLE question_list;
  2. INSERT INTO question_list
  3. SET TEXT='The text of the question',
  4. previous_id='-1',
  5. next_id='-1';
  6.  
  7. INSERT INTO question_list
  8. SET TEXT='Inside question text',
  9. previous_id=LAST_INSERT_ID(),
  10. next_id=-1;
  11. UPDATE question_list
  12. SET next_id=LAST_INSERT_ID()
  13. WHERE question_id=LAST_INSERT_ID()-1;
  14.  
  15. INSERT INTO question_list
  16. SET TEXT='Inside question text',
  17. previous_id=LAST_INSERT_ID(),
  18. next_id=-1;
  19. UPDATE question_list
  20. SET next_id=LAST_INSERT_ID()
  21. WHERE question_id=LAST_INSERT_ID()-1;
  22.  
  23. INSERT INTO question_list
  24. SET TEXT='Last question text',
  25. previous_id=LAST_INSERT_ID(),
  26. next_id=-1;
  27. UPDATE question_list
  28. SET next_id=LAST_INSERT_ID()
  29. WHERE question_id=LAST_INSERT_ID()-1;
  30. UNLOCK TABLES;
So this isn't built into MySQL, but I think it accomplishes what you want to do.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the MySQL Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC