Rename a column in mysql table without having to repeat its type definition

前端 未结 2 1552
春和景丽
春和景丽 2021-02-19 02:44

Is it possible to rename a column in MySQL without having to repeat its type definition?

Please without having to hack into information_schema.

相关标签:
2条回答
  • 2021-02-19 03:15

    The ALTER TABLE syntax does not seem to offer such possibility:

    ALTER [ONLINE | OFFLINE] [IGNORE] TABLE tbl_name
        [alter_specification [, alter_specification] ...]
        [partition_options]
    
    ALTER [ONLINE | OFFLINE] [IGNORE] TABLE tbl_name
        partition_options
    
    alter_specification:
        table_options
    [...]
      | CHANGE [COLUMN] old_col_name new_col_name column_definition
            [FIRST|AFTER col_name]
      | MODIFY [COLUMN] col_name column_definition
            [FIRST | AFTER col_name]
    [...]
    

    More specifically:

    When you use CHANGE or MODIFY, column_definition must include the data type and all attributes that should apply to the new column, other than index attributes such as PRIMARY KEY or UNIQUE. Attributes present in the original definition but not specified for the new definition are not carried forward.

    0 讨论(0)
  • 2021-02-19 03:23

    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;
        }
    
    0 讨论(0)
提交回复
热议问题