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?
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.
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:
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).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With