Change the name of the MySQL column Alter table select data type

Sometimes you need to change the column of the database to MySQL, but in comparison with MSSQL and other DB syntax does not allow you to immediately rename the column to the one we need. The problem is to specify the type that you want to know for renaming from the beginning. There may be other options for changing the name, but in this example, we offer your option to perform this task. We needed to rename the end of columns of tables to a new language ie with wordua for example on wordru, in our system of language control are asked exactly this way. Here is an example of how this is done:

PHP code

$ type = mysql_result (mysql_query ("SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = ''. $ table. '' AND COLUMN_NAME = ''. $ table_column_name. ''), 0); if ($ type == 'varchar ') {$ type. =' (255) ';} mysql_query ("ALTER TABLE". $ table. "CHANGE". $ table_column_name. "". $ new_table_column_name. "". $ type. "");}

SQL syntax

To find out the type of column, perform the following SQL

SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'table' AND COLUMN_NAME = 'column_name'

The change of the name of the column of the database itself

ALTER TABLE `table` CHANGE` table_column_name` `new_table_column_name` type '

We know that reasonable on the Internet is enough, do not judge our variant of changing the names of columns MySQL DB, do not judge the code will be useful and useful for your development.

Webmarker logo small
Ukraine, Kiev