Changing multiple column names in a mysql table - mysql

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};

Related

select column from multiple unknown tables

I have a lot of dynamically created nearly similar looking tables according to the scheme "prefix + number", eg "t1", "t2", "t343" etc. All those tables have a cross-table unique row named identifier that I like to select within one query:
SELECT
`identifier`
FROM
(
SELECT
`TABLE_NAME`
FROM
information_schema.TABLES
WHERE
`TABLE_NAME` LIKE 't%'
);
But this returns: ERROR 1248 (42000): Every derived table must have its own alias
EDIT: according to the comments I modified my query like this:
SELECT
A.identifier
FROM
(
SELECT
`TABLE_NAME` AS identifier
FROM
information_schema.TABLES
WHERE
`TABLE_NAME` LIKE 't%'
) A;
But this selects only the table names from the subquery but not the column identifier from these tables.
When you create the table dynamically, and you want to query all of them, you can create an SQL statement dynamically like:
select
group_concat(
concat(
'SELECT ',
'''',TABLE_SCHEMA,'.',TABLE_NAME,''',',
TABLE_SCHEMA,'.',TABLE_NAME,'.', COLUMN_NAME,
' FROM ',
TABLE_SCHEMA,'.',TABLE_NAME)
separator ' union all ')
from information_schema.`COLUMNS` c
where table_schema='test' -- the schema name where your tables are
and table_name regexp '^t[0-9]+$' -- table name starts with t and ends with number
and COLUMN_NAME = 'i' -- i choose `i` as the column to be selected
;
This will produce a SQL statement like:
select
'test.t1',
test.t1.i
from
test.t1
union all
select
'test.t2',
test.t2.i
from
test.t2
union all
select
'test.t3',
test.t3.i
from
test.t3
When putting all of this in a stored procedure, you can use PREPARE and EXECUTE to execute this created statement.
Above is just an example of an SQL statement, you should change it to your needs.

Rename all columns in a table removing whitespace

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'

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)