Is it possible to rename a column in MySQL without having to repeat its type definition?
Please without having to hack into information_schema.
In my case I wanted to change all the columnNames from "id_something" to "idSomething" then to be able to load data to my C# object faster and easily. I was looking for a script to do that and it looks impossible to find :( Finally I did it by myself in C# Here you have the main functions that I used:
I wish it will be usefull for everybody!
public bool columnesRename(string oldName, string newName, string dataBase, string dataTable)
{
bool res = true;
string definicio = columnaDefinicioGet(oldName, dataBase, dataTable);
cmd.CommandText = String.Format("alter table {0} change {1} {2} {3} ", dataTable, oldName, newName, definicio);
Debug.WriteLine(String.Format("canviant taula {0}: {1} per {2}", dataTable, oldName, newName));
Debug.WriteLine(cmd.CommandText);
Debug.WriteLine(cmd.ExecuteNonQuery());
return res;
}
public string columnaDefinicioGet(string columna, string dataBase, string dataTable)
{
string definicio = "";
cmd.CommandText = String.Format
(
@"select column_type, is_nullable, extra, column_default from information_schema.columns where table_schema = '{0}' and table_name = '{1}' and column_name = '{2}'"
, dataBase, dataTable, columna
);
MySqlDataReader dr = cmd.ExecuteReader();
dr.Read();
string nulONo = (dr[1].ToString() == "NO") ? "NOT NULL" : "NULL";
string valorDefault = (dr[3].ToString() == "NULL" || dr[3].ToString() == "") ? dr[3].ToString() : "DEFAULT '" + dr[3].ToString() + "'";
definicio = String.Format("{0} {1} {2} {3}", dr[0], nulONo, dr[2], valorDefault);
dr.Close();
return definicio;
}