Rename a column in MySQL
Use the following query:
ALTER TABLE tableName CHANGE oldcolname newcolname datatype(length);
The RENAME
function is used in Oracle databases.
ALTER TABLE tableName RENAME COLUMN oldcolname TO newcolname datatype(length);
@lad2025 mentions it below, but I thought it'd be nice to add what he said. Thank you @lad2025!
You can use the RENAME COLUMN
in MySQL 8.0 to rename any column you need renamed.
ALTER TABLE table_name RENAME COLUMN old_col_name TO new_col_name;
ALTER TABLE Syntax:
RENAME COLUMN:
- Can change a column name but not its definition.
- More convenient than CHANGE to rename a column without changing its definition.
How to rename a column in MySQL
Try using this syntax:
ALTER TABLE TableName CHANGE OldColumnName NewColumnName varchar(10) ;
Rename column in mysql (failed)
Some of the statements in your list are valid MySQL syntax. The problem is how to properly quote the original column name, that contains special characters: you need backticks rather than single quotes (which are meant for litteral strings, not for identifiers).
For example:
ALTER TABLE exp RENAME COLUMN `Nr._CRT` TO id;
How to rename a column name in maria DB
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
Can I rename a MySQL Column without knowing the existing column name?
I've found a workaround that works for me. I don't know how applicable it is in general, but for my application, it works like a charm.
Since I know the order of the columns in the table from the external source, I simply use a UNION ALL, as follows:
Select 'Id', 'Name', 'Date', 'Title', 0.00 AS 'Value' LIMIT 0
UNION ALL
Select * from original_table
MySQL uses the values as column names except where explicitly aliased.
Note that the LIMIT 0
is required so that you don't have a dummy row.
Renaming the column MySQL Workbench MariaDB
I resolved put the schema together like autoparanaiba.TABLE_NAME
.
How to change column name in VIEW table?
You can't directly rename columns in a view, but you can recreate it with the new names you want:
CREATE OR REPLACE cView AS
SELECT col1,
col2,
cName AS collageName,
enrollment AS seat,
someOtherColumn
-- etc...
FROM myTable
Rename a column in AWS Aurora DB
I've managed to rename it after all, using this weird CHANGE
statement. Let me leave the query here in case anyone faces the same issue in the future:
ALTER TABLE a CHANGE b c INT NOT NULL
As you can see, it's not perfect, cause one has to specify column type, not just name. So, make sure you replace INT NOT NULL
with your own type. And in case you don't add NOT NULL
in there, it might create a nullable column, so beware.
Related Topics
How to Query a Value in SQL Server Xml Column
Using 'Case Expression Column' in Where Clause
Split Column into Multiple Rows in Postgres
Bulk Delete on SQL Server 2008 (Is There Anything Like Bulk Copy (Bcp) for Delete Data)
Where Clause VS on When Using Join
Is There Something Like a Zip() Function in Postgresql That Combines Two Arrays
SQL Server:Dynamic Pivot Over 5 Columns
How to Store a List in a Column of a Database Table
Search for a String in All Tables, Rows and Columns of a Db
How to Create a Unique Index on a Null Column
How to Determine the Number of Days in a Month in SQL Server
The Maximum Recursion 100 Has Been Exhausted Before Statement Completion