37

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.

halfelf
  • 9,737
  • 13
  • 54
  • 63
Kaveen Hyacinth
  • 495
  • 1
  • 4
  • 10

3 Answers3

55

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
Rick James
  • 135,179
  • 13
  • 127
  • 222
30

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

Robert Hickman
  • 997
  • 1
  • 11
  • 22
7
alter table "table_name" change column "old_name" "New_name" "datatype"*;

there is no need to use "TO" between old_name & New_name , datatype of New_name is must

e.g. -

alter table student change column id roll_no int;
Ihor Konovalenko
  • 1,298
  • 2
  • 16
  • 21
kunal kanse
  • 71
  • 1
  • 1
  • Thank you for sharing this. I swore the documentation said to use 'TO' but after quadruple checking it doesn't have the 'TO' keyword. I was losing my mind! lol Thank you :) – waltmagic Dec 15 '22 at 16:16