Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jul 2011
    Location
    Toronto, ON
    Posts
    102
    Thanks
    12
    Thanked 1 Time in 1 Post

    Remove column data without removing column

    Hi guys,

    I know about the drop column mysql command, but I understand that removed the column from the database.

    Is there a way to delete all the fields in 1 column, but do not delete the column itself?
    Last edited by inchecksolution; 07-21-2011 at 04:27 PM.

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,316
    Thanks
    23
    Thanked 613 Times in 612 Posts
    Yes update the column to be '' (empty).

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,014
    Thanks
    79
    Thanked 4,435 Times in 4,400 Posts
    Ummm...if the column is not a text column of some kind (that is, if it is a number or datetime, say) then you can *NOT* update the column to be ''

    A more general answer is to update the column to NULL.

    That is:
    Code:
    UPDATE tablename SET columnname = NULL
    BUT...

    But even that can fail if the column was specified as NOT NULL.

    So there's no 100% general answer. It depends on the type of column and on whether the column allows NULLs or not.

    If you do
    Code:
    DESCRIBE tablename
    and show us what it shows you and tell us what column you wish to remove the data from, we can tell you what will (and won't) work.

  • #4
    Regular Coder
    Join Date
    Jul 2011
    Location
    Toronto, ON
    Posts
    102
    Thanks
    12
    Thanked 1 Time in 1 Post
    Thanks for the help guys. My original idea was to erase the whole column and then populate it again with the updated information. Instead I just loop through each row and run an UPDATE query.

    Thanks for your help!

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,014
    Thanks
    79
    Thanked 4,435 Times in 4,400 Posts
    You do *NOT* need or want to "loop through each row"!!!

    That is horribly slow and clumsy!

    If you do what I showed you
    Code:
        UPDATE tablename SET columnname = NULL
    that will do *ALL ROWS* without any need for an ugly loop!!!!

  • #6
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    While I agree a loop is probably not necessary (unless you have exotic conditions you need to evaluate), I take issue with the idea that loops are ugly.

    Loops.... are.... beautiful.




  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,014
    Thanks
    79
    Thanked 4,435 Times in 4,400 Posts
    Now wait a minute!

    Your location says "Utah", but that sure as heck isn't Utah!

    And how many times did you stop at the Alpine Inn when you rode that loop?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •