Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rename a column name in maria DB

Tags:

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.

like image 494
Kaveen Hyacinth Avatar asked Dec 12 '18 02:12

Kaveen Hyacinth


People also ask

How do you rename a column name?

To change a column name, enter the following statement in your MySQL shell: ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name; Replace table_name , old_column_name , and new_column_name with your table and column names.

Can we rename column name in SQL?

You select the table with ALTER TABLE table_name and then write which column to rename and what to rename it to with RENAME COLUMN old_name TO new_name .

How do I rename a column in MySQL?

To rename a column in MySQL the following syntax is used: ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name; This command is used to change the name of a column to a new column name.

How do I rename a column in PL SQL?

Use the RENAME COLUMN statement to rename a column in a table. The RENAME COLUMN statement allows you to rename an existing column in an existing table in any schema (except the schema SYS). To rename a column, you must either be the database owner or the table owner.


2 Answers

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
like image 107
Rick James Avatar answered Oct 12 '22 16:10

Rick James


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

like image 24
Robert Hickman Avatar answered Oct 12 '22 15:10

Robert Hickman