Rename all columns in a table removing whitespace - mysql

I have a table which I imported from a very large CSV which has over 100 columns. I've just noticed they've imported with spaces in the column names.
Is there a way to rename all columns and remove the spaces?

The following query will remove all whitespace from column names containing any white space in the table your_table in the database your_database. You can replace with the values you need.
SELECT
CONCAT(
'ALTER TABLE ', C.TABLE_NAME, ' CHANGE `',
C.COLUMN_NAME, '` `', REPLACE(C.COLUMN_NAME, ' ', ''), '` ',
C.DATA_TYPE, ';'
)
FROM
INFORMATION_SCHEMA.COLUMNS C
WHERE
TABLE_SCHEMA = 'your_database' AND TABLE_NAME = 'your_table'
AND C.COLUMN_NAME LIKE '% %';
Pay very close attention to the backticks which surround the column name (including the new column name). This will output a set of ALTER TABLE statements which look like the following:
ALTER TABLE your_table CHANGE `Old Column Name` `OldColumnName` VARCHAR;

Try this system procedure:
EXEC sp_RENAME 'TableName.OldName' , 'NewName', 'COLUMN'

Related

Replacing All Column and Table Names in MySQL Database

I have about 20 tables in my database where I use the word 'car' in many of the table names and the column names.
Is there a script or tool that will allow me to replace every instance of 'car' with 'vehicle'? I do not want to replace any of the table data itself.
You could generate the RENAME TABLE statements with a query, then simply copy and execute them.
select concat('rename table ', table_name, ' to ', substring_index(table_name, 'car', 1), 'vehicle', substring_index(table_name, 'car', -1), ';')
from information_schema.tables
where table_schema = 'your_schema'
and table_name like '%car%';
The substring-index will return what is before the first occurence of 'car' when given 1.
It will return what is after when given -1.

Delete column where name contains '_nd', rename column where name contains '_er'?

I have a large group of columns in a table (50+) which contain some statistics about a game.
We are simplifying our statistics, so now we want to do two things.
Drop any columns where the name contains '%_nd%'
Remove the string '_er' from any column names.
Is there an easy way to do this automatically, or do I need to enter all the column names manually?
Well you can do the first by running the following and using the SQL it produces:
select distinct concat('alter table ',table_name,' drop column ',column_name) someSql
from information_schema.columns
where column_name like '%_nd%'
You could do the second with a similar query:
select distinct concat('alter table ',table_name,' change ',column_name, ' ', replace(column_name, '_er', ''), ' ', data_type, case when CHARACTER_MAXIMUM_LENGTH is not null then concat('(', CHARACTER_MAXIMUM_LENGTH, ')') end) someSql
from information_schema.columns
where column_name like '%_er%'

Rename column names in MySQL

I have a bunch of columns in MySQL Table T1 with names like
A/B
C/D
E/F etc
I want to remove the '/' so that the names are AB, CD, EF (etc)
Is there a way(query) to rename all of them together rather than doing it one by one through alter table
Thanks
The easy way is to write a query that outputs the SQL, then copy-paste the output back into the command line.
Something like this:
select concat('alter table ', table_name, ' change ', column_name, ' ', replace(column_name, '/', ''), ' ', column_type, ';')
from information_schema.columns
where table_schema = 'mydbname'
and column_name like '%/%';
I tested this SQL and it works
alter table table_name
change A/B AB varchar(20),
change C/D CD varchar(20),
change E/F EF varchar(20)

MySQL modify all field lengths

Is there any way you can update the length of all fields in MySQL? I tried:
ALTER TABLE mytable MODIFY * VARCHAR(150);
But no joy. Any suggestions?
You can use info from information_schema database to generate query for you.
If you need to change all varchar(100) columns to varchar(150) in your table myTable use query:
SELECT CONCAT( 'ALTER TABLE `myTable`',
GROUP_CONCAT(
CONCAT( '\nMODIFY ', COLUMN_NAME, ' VARCHAR(150) ' ),
'' )
) AS query
FROM information_schema.COLUMNS
WHERE DATA_TYPE = 'varchar'
AND CHARACTER_MAXIMUM_LENGTH = 100
AND TABLE_SCHEMA = 'myDatabase'
GROUP BY TABLE_NAME
and then copy/paste output as another query to do the actual job.
You have to modify each column specifically.
ALTER TABLE mytable
MODIFY col_1 VARCHAR(150),
MODIFY col_2 VARCHAR(150),
MODIFY col_3 VARCHAR(150);
Note that all modifications can be done in a single "pass", so this is more efficient than running n individual ALTER TABLE calls.

Changing multiple column names in a mysql table

I have many columns in a table with a name starting with field_t and I have to change that to field_c
For example, here is the ALTER TABLE statement for changing the name of one of the columns:
ALTER TABLE my_table CHANGE field_t_class field_c_class longtext;
How can I change all the columns that follow this pattern instead of doing it in a one by one basis?
You can generated the ALTERs like this
SELECT
CONCAT(
'ALTER TABLE ', C.TABLE_NAME, ' CHANGE ',
C.COLUMN_NAME, ' ', REPLACE(C.COLUMN_NAME, 'field_t', 'field_c')
)
FROM
INFORMATION_SCHEMA.COLUMNS C
WHERE
C.COLUMN_NAME LIKE 'field[_]t[_]%';
You'll also need to append DATA_TYPE etc and based on this CHARACTER_MAXIMUM_LENGTH,
NUMERIC_PRECISION, NUMERIC_SCALE, CHARACTER_SET_NAME and COLLATION_NAME...
Get 1st column names having name like 'field_t%';
select C.COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS C
WHERE
C.TABLE_NAME='YourTableName' AND C.COLUMN_NAME like 'field_t%';
Then make string of renaming column names like:
Make rename_string as:
rename_string= "RENAME COL1 to ReCol1
RENAME COL2 to ReCol2"
Then do :
ALTER Table YourTableName {rename_string};