search

Home  >  Q&A  >  body text

The title is rewritten as: Successfully solved - the problem of unable to update mySQL column names using the mysql command line

<p>I cannot change the column names in the mysql database I created. </p> <p>I tried the following commands but none of them seem to work. </p> <pre class="brush:php;toolbar:false;">alter table (mytablename) CHANGE COLUMN (oldcolumnname) (newcolumnname) varchar(120);</pre> <pre class="brush:php;toolbar:false;">alter table (mytablename) RENAME COLUMN (oldcolumnname) (newcolumnname) varchar(120);</pre> <pre class="brush:php;toolbar:false;">ALTER TABLE (mytablename) CHANGE (oldcolumnname) (newcolumnname) varchar(120);</pre> <p>Where (mytablename) is the name of the table I created, (oldcolumnname) is the original column name, and (newcolumnname) is the new column name. </p> <p>Here is a simple to-do list I created for learning MySQL with the following items: </p> <table class="s-table"> <thead> <tr> <th>id</th> <th>todo</th> <th>completed</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>Ready to take off</td> <td>is</td> </tr> <tr> <td>2</td> <td>Learn some MySQL</td> <td>is</td> </tr> <tr> <td>3</td> <td>Remember that damn semicolon</td> <td>No</td> </tr> </tbody> </table> <p>In this case I tried changing the column 'todo' to 'To Do' or 'To-Do' but every time I try these commands I get the famous "Check your SQL version manual" ". </p> <p>Any tips on what I might be doing wrong? Thanks! </p> <p>I've looked at multiple tutorial sites and even looked at another StackOverflow question</p> <p><strong>Update</strong> The final solution was a two-step process. First, I need to use the <strong>TO</strong> phrase between the column names. Secondly, column names don't like special characters. </p> <p>The query that finally worked was: </p> <pre class="brush:php;toolbar:false;">ALTER TABLE mytablename CHANGE COLUMN todo TO ToDo</pre> <p><br /></p>
P粉343408929P粉343408929472 days ago556

reply all(1)I'll reply

  • P粉448130258

    P粉4481302582023-08-21 19:44:33

    You should keep the column named todo; you can always change the output when you select, like this:

    select id, todo as 'To Do', completed from ...

    If you really feel you must include spaces or dashes in column names, in MySQL you can use arbitrary identifiers, just enclose them in backticks:

    alter table ... rename column todo to `To Do`

    But every time you reference the column in sql, you need to surround it with backticks:

    select id,`To Do`,completed from ... where `To Do` like '%learn%'

    reply
    0
  • Cancelreply