Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change mysql field name in a huge table

Tags:

mysql

alter

I have a table with 21 million row.I have to change one of the row name.When I try with the query "alter table company change id new_id int(11)"; query never ends.

Is there a simple way to change big mysql table's field name?

like image 845
demircan Avatar asked Dec 07 '12 21:12

demircan


People also ask

How do I change a column name in MySQL?

To change a column name, enter the following statement in your MySQL shell: Exchange the your_table_name, original_column_name, and new_column_name with your table and column names. Keep in mind that you cannot rename a column to a name that already exists in the table. Note: The word COLUMN is obligatory for the ALTER TABLE RENAME COLUMN command.

How to rename a table in MySQL?

ALTER TABLE RENAME is the existing syntax to rename the entire table. The CHANGE clause offers important additions to the renaming process. It can be used to rename a column and change the data type of that column with the same command. Enter the following command in your MySQL client shell to change the name of the column and its definition:

How to change the name of a field in a table?

The less time consuming method to change name of a field in a huge Mysql table is to : CREATE TABLE new_big_table SELECT id as new_id,field1,field2,field3 FROM big_table. Show activity on this post. It's just an fast idea, but it won't be fast (exporting/importing).

How to add a column to a table in MySQL?

Summary: in this tutorial, you will learn how to use the MySQL ALTER TABLE statement to add a column, alter a column, rename a column, drop a column and rename a table. The ALTER TABLE ADD statement allows you to add one or more columns to a table.


1 Answers

First, before anything, backup the table:

mysqldump -uroot -p db big_table > /tmp/big_table.backup.sql

One thing to keep in mind when doing an ALTER command is to absolutely make sure that you have the same column details for the alter that you would like to not change.

So for example:

ALTER TABLE big_table MODIFY COLUMN id INT(11)

Would skip anything else like AUTO_INCREMENT and NOT NULL. So best to include that also into the alter statement.

ALTER TABLE big_table MODIFY COLUMN id INT(11) NOT NULL AUTO_INCREMENT;

Basically just copy over the create statement area for the ID column and replace what you want and include that into the modify statement.

like image 95
matsko Avatar answered Oct 05 '22 04:10

matsko