ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    MySQL Assistance - Renaming Multiple Columns That Match A Specific Criteria

    IT Discussion
    3
    7
    1.2k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • BrainsB
      Brains
      last edited by Brains

      I can do this in php, but I would like to do this only in MySQL

      We need to rename ~155 something columns in one table, and I was trying to find the best way to accomplish this. My thoughts at the moment are to query the information_schema to obtain a list of column names matching a specific criteria. My research has pointed me to using a cursor holding the results of the information_schema, and attempting to loop through the cursor. I am by no means a MySQL expert, and the syntax is too confusing for me too follow. Does anyone know of a way to do this? Any guidance will be very appreciated. Looking at you DBAs out there O_o

      I am matching on "comments" and will be replacing it with "assessments"

      Format of column names
      day(1-5)_comments_a(0-31)

      There are also some columns named day(1-5)_comments_a(25,29,30,31)_s0. I can perform these changes manually, but I would to make 1 SQL file to perform all of these changes (To coincide with code deployment)

      1 Reply Last reply Reply Quote 0
      • scottalanmillerS
        scottalanmiller
        last edited by

        You'll want to use standard SQL syntax for this. A command similar to this:

        ALTER TABLE mytable CHANGE columnold columnnew INT

        JaredBuschJ 1 Reply Last reply Reply Quote 0
        • JaredBuschJ
          JaredBusch @scottalanmiller
          last edited by

          @scottalanmiller said:

          You'll want to use standard SQL syntax for this. A command similar to this:

          ALTER TABLE mytable CHANGE columnold columnnew INT

          This is basically it. Just make a script and save it. Really not enough columns to fret over..

          scottalanmillerS 1 Reply Last reply Reply Quote 0
          • scottalanmillerS
            scottalanmiller @JaredBusch
            last edited by

            @JaredBusch Right. For 37 columns, I would do them by hand. In the MySQL client you can "up arrow" to recall the last line typed in. It is as easy to type the column pairs that way as into any other form of into. Whole thing should only take five minutes.

            1 Reply Last reply Reply Quote 0
            • BrainsB
              Brains
              last edited by

              Sorry that should say 155 columns.

              scottalanmillerS 1 Reply Last reply Reply Quote 0
              • scottalanmillerS
                scottalanmiller @Brains
                last edited by

                @Brains said:

                Sorry that should say 155 columns.

                Do you have the list in a text file or something?

                1 Reply Last reply Reply Quote 0
                • BrainsB
                  Brains
                  last edited by

                  I exported a list by querying the information_schema. I know there are other ways I could do this:

                  • Export the table and use replace function in the file. Then recreate the table
                  • Code the logic in PHP and make the changes via loop

                  Part of this is curiosity/learning, part of it is wanting to be able to make these changes quickly, part is for future use since this comes up sometimes if the DB columns were not named properly initially. Ideally, I would like this to be all contained within one .sql file that can be ran automatically during deployment.

                  1 Reply Last reply Reply Quote 0
                  • 1 / 1
                  • First post
                    Last post