How to rename a column name in maria DB

26,128

Solution 1

Table names, column names, etc, may need quoting with backticks, but not with apostrophes (') or double quotes (").

ALTER TABLE subject
    CHANGE COLUMN `course_number`   -- old name; notice optional backticks
                   course_id        -- new name
                   varchar(255);     -- must include all the datatype info

Solution 2

Starting with MariaDB 10.5.2 you should be able to do

ALTER TABLE subject RENAME COLUMN course_number TO course_id;

see https://mariadb.com/kb/en/alter-table/#rename-column

Share:
26,128

Related videos on Youtube

Kaveen Hyacinth
Author by

Kaveen Hyacinth

"Flow towards; flow freely" is my life motto which always makes me chase my dreams. Challenging the norms and defining problems in different aspects could enhance my life for past years as a creative thinker. Currently, I'm a Full-stack web developer and a digital nomad. I believe that I have been given my life to make the impossibles a possibility.

Updated on January 09, 2022

Comments

  • Kaveen Hyacinth
    Kaveen Hyacinth over 2 years

    I am new to SQL, I was trying to change column name in my database's table. I am using 'xampp' with 'maria DB' (OS - Ubuntu 18.04)

    I tried all of the followings:

    ALTER TABLE subject RENAME COLUMN course_number TO course_id;
    ALTER TABLE subject CHANGE course_number course_id;
    ALTER TABLE subject CHANGE 'course_number' 'course_id';
    ALTER TABLE subject  CHANGE COLUMN 'course_number'  course_id varchar(255);
    ALTER TABLE subject CHANGE 'course_number' 'course_id' varchar(255);
    

    But the only output I got was:

    ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'column course_number to course_id' at line 1

    Could someone please tell me what is the correct answer. I have no idea what to do further.

    • Jake Lee
      Jake Lee over 5 years
      Possible duplicate of Change column name in MariaDB
    • ToujouAya
      ToujouAya over 5 years
      You should change the symbol from ' to this `
    • Kaveen Hyacinth
      Kaveen Hyacinth over 5 years
      thank you @ToujouAya. That was the mistake.
    • Kaveen Hyacinth
      Kaveen Hyacinth over 5 years
      @JakeSteam It is a different from my question. He has messed up ' . ' with table name, in my case I was using the wrong symbol through out all the program.
  • Michael restore Monica Cellio
    Michael restore Monica Cellio about 3 years
    MariaDB [socialnet]> ALTER TABLE users RENAME COLUMN current_lat TO was_lat;<br/> ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'COLUMN current_lat TO was_lat' at line 1
  • Robert Hickman
    Robert Hickman about 3 years
    @Michael, maybe you’re using the wrong version? You can show your version with SELECT VERSION()
  • Michael restore Monica Cellio
    Michael restore Monica Cellio about 3 years
    10.3.22-MariaDB-1:10.3.22+maria~xenial-log
  • Michael restore Monica Cellio
    Michael restore Monica Cellio about 3 years
    I ended up using change column.
  • Robert Hickman
    Robert Hickman about 3 years
    @MichaelrestoreMonicaCellio, yeah your version doesn't support the newer RENAME COLUMN way of doing it.