How to rename a column name in maria DB

江枫思渺然 提交于 2020-05-14 18:22:09

问题


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.


回答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



回答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



来源:https://stackoverflow.com/questions/53735305/how-to-rename-a-column-name-in-maria-db

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!