+ Reply to Thread
Results 1 to 7 of 7

Thread: SQL moving a field within a table

  1. #1
    wjh2303 is offline x10 Sophmore wjh2303 is an unknown quantity at this point
    Join Date
    Mar 2009
    Posts
    139

    SQL moving a field within a table

    i have a table, it has fields, i would like to move one of the fields to the end of the table (its currently in the middleish). Is there an easy way to do this? if not what is the hard way?

    thanks

  2. #2
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: SQL moving a field within a table

    Location of a field within a table is pretty insignificant. I suppose you could add a new field to the end, create an UPDATE query and make new = old then delete the old field.
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  3. #3
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: SQL moving a field within a table

    As garret said, location is unsignificant. If you need your fields in a special order when you fetch data, well then, after the SELECT part, put the name of the fields in the order you want them. For instance:
    Code:
    SELECT field_1, field_6, field_2, field_8 ...
    When inserting data, proceed in the same way:
    Code:
    INSERT ... (field_1, field_6, field_2, field_8) VALUES (value_1, value_6, value_2, value_8)
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

  4. #4
    wjh2303 is offline x10 Sophmore wjh2303 is an unknown quantity at this point
    Join Date
    Mar 2009
    Posts
    139

    Re: SQL moving a field within a table

    i know it doesnt make a differenc to the functionality, but one of the fields is of arbitrary length but all the others are pretty constant, so id like to put the arbitrary length field at the end so that everything lines up for easier editing when i export the table

  5. #5
    Livewire's Avatar
    Livewire is offline Abuse Compliance Officer Livewire is a glorious beacon of lightLivewire is a glorious beacon of light
    Join Date
    Jun 2005
    Location
    Behind a keyboard.
    Posts
    8,998

    Re: SQL moving a field within a table

    Export as a CSV/Comma Separated Value file, edit in excel, re-save as CSV

    CSV just means separated like this:

    value1,value2,value3,value4,value5
    value1,value2,value3,value4,value5


    Excel and OpenOffice Spreadsheet can open a CSV and save it back to CSV for importing, but you -can- still move the column to the end like garrett pointed out earlier.


    TOS breakers will be suspended regardless of race, creed, national origin, hair color, or favorite food. Thanks for your understanding!

  6. #6
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: SQL moving a field within a table

    When you export or import a table, you can specify field order. No need to change the table definition. You should alter tables only if necessary for what you need to store in the table. An alternative is to create a view and export from that.

    If you still really, really feel like you need to reorder columns, with MySQL you can use:
    Code:
    ALTER TABLE table_name MODIFY col_name col_dfn AFTER last_col_name;
    PostgreSQL doesn't support ALTER ... MODIFY (which should tell you something about what you're trying to do). Backup your data before doing this, because it could damage the data stored in the column. Make sure col_dfn is exactly the same as the old definition. You might need to re-create any indexes that include the moved column and perform other cleanup.

    phpMyAdmin might let you move a column. If it doesn't, that should also tell you something about what you're trying to do.

  7. #7
    wjh2303 is offline x10 Sophmore wjh2303 is an unknown quantity at this point
    Join Date
    Mar 2009
    Posts
    139

    Re: SQL moving a field within a table

    ok, i guess aesthetics arnt worth risking it!

+ Reply to Thread

Similar Threads

  1. MysQL Query for Master table & child table
    By phpasks in forum Programming Help
    Replies: 8
    Last Post: 08-07-2008, 08:07 AM
  2. SQL all but one field
    By zester in forum Programming Help
    Replies: 4
    Last Post: 07-15-2008, 02:41 PM
  3. row missing in an auto-increment field of DB table
    By anilson1 in forum Programming Help
    Replies: 3
    Last Post: 06-07-2008, 03:47 PM
  4. sql table
    By Anonymousx1 in forum Free Hosting
    Replies: 1
    Last Post: 02-01-2008, 10:13 PM
  5. Table inside table
    By wizeman in forum Tutorials
    Replies: 4
    Last Post: 07-11-2005, 05:56 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers