Can i insert something to the top of a table?

Discussion in 'MySQL' started by redhits, Mar 5, 2010.

  1. #1
    If i am having a table like

    row1,
    row2,
    row3,

    then when i will add rowX, it will look like

    row1
    row2
    row3
    rowX

    can i do something so the rowX to be inserted on the top of the table not on the bottom?

    like

    rowX
    row1
    row2
    row3
     
    redhits, Mar 5, 2010 IP
  2. redhits

    redhits Notable Member

    Messages:
    3,023
    Likes Received:
    277
    Best Answers:
    0
    Trophy Points:
    255
    #2
    no responses?! lol
     
    redhits, Mar 6, 2010 IP
  3. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #3
    Why you want to insert on the top of the table? If you want to display the data in that sequence then use ORDER BY clause to order the results.

    It is always not true that new rows are inserted at the end of the table. Try the following and you'll see a different behavior. Insert 3 rows as you inserted before, now delete 2nd row and insert rowX. Now check the data in the table. You'll find that the rows are in this format

    row1
    rowX
    row3
     
    mwasif, Mar 6, 2010 IP
  4. redhits

    redhits Notable Member

    Messages:
    3,023
    Likes Received:
    277
    Best Answers:
    0
    Trophy Points:
    255
    #4
    Because i want to insert it their, not to modify lot's of codes of the already made script

    + can i also do something to insert the data in the middle of that table? is there a function to change row(X) data with row(Y) data?
     
    redhits, Mar 6, 2010 IP
  5. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #5
    You can not insert the data according to your will but you can display it as you want. You can do one thing after inserting data i.e. sort the whole table on a specific field e.g.
    ALTER TABLE table_name  ORDER BY id;
    Code (markup):
     
    mwasif, Mar 6, 2010 IP
  6. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #6
    you can export the current rows to .sql, delete all rows, move the row you want on top to the top, import the .sql
     
    killaklown, Mar 7, 2010 IP
  7. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #7
    Tables have no order--therefore they have no top, bottom, first, thirty-second, nor last.

    They aren't spreadsheets nor word documents. Think of tables as this huge pile of rows of data all thrown together somewhere in the bowels of a computer that you can't even see. When you add new data it just creates a new record and throws it on that pile without care how it bounces around and lands.

    Order only exists when you tell your computer to retrieve that data and use the 'ORDER BY' clause of SQL. So, to make your data come out like you want it, you must define what top, bottom means. What field determines order? From there, to manipulate how each record comes out, insert values into that field such that when you use the 'ORDER BY' clause on that field it comes out properly.
     
    plog, Mar 8, 2010 IP