I'm looking to replace all occurrences of characters in all columns of all tables in my database.
I got the name of my column like this:
select table_name, column_name from information_schema.columns;
And I would apply an UPDATE REPLACE like this:
update table_name set column_name = replace (column_name, "a", "A");
PS : The replacement of "a" to "A" is just one example, my problem is rather how to link table_name and column_name between my two queries.
I tried with subselect, like:
update (select table_name from information_schema.tables as tables) set (select column_name from columns as information_schema.columns Where table_name = tables) = replace (columns, "a", "A");
But I still get errors when I try. What is the right way to do this?
Thank you in advance.
What you need is a stored procedure that will use the information_schema to find all tables and columns within your database, and execute an update statement for all these tables.
Have a look at the following question which answers exactly what you wish to do: Find and replace in entire mysql database
It will not work like that.
You have 2 options:
1) Iterate through all tables on code side and lunch the update for each one
2) Use a stored procedure that iterate through all tables and lunch the update for each one
Related
In MySQL, how to change all columns names of all tables to remove the string "_euro" from columns names?
I just could find a way to search tables having some columns containing "_euro" in their names:
SELECT DISTINCT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE "%_euro"
AND TABLE_SCHEMA='my_database'
For example, for the column named price_total_euro I want to rename it as price_total
Create a script with the following SQL:
SELECT CONCAT("ALTER TABLE ",TABLE_SCHEMA,".",TABLE_NAME," RENAME COLUMN ",COLUMN_NAME," TO ",REPLACE(COLUMN_NAME,"_euro",""),"; ")
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE "%_euro"
output will be, multiple lines which look like this:
ALTER TABLE test.t1_euro RENAME COLUMN t1_euro TO t1;
Review the script, and execute it on your database (after making a backup....)
put your select into a stored procedure and make a cursor and loop on all and do the alter operation
you can find example for a procedure here : https://stackoverflow.com/questions/15786240/mysql-create-stored-procedure-syntax-with-delimiter#:~:text=Getting%20started%20with%20stored%20procedure%20syntax%20in%20MySQL%3A,Why%20didn%27t%20this%20work%3F%20...%20More%20items...%20
if your using python with MySQL Connector you could place the column headers into a list and then run it through a for loop?
for name in table_names:
if name.endswith("_euro"):
new_name = name.replace("_euro", "")
I have a MySQl database with more than 200 tables. I want to do following on ALL tables in this database.
Update all table name by adding a constant to the name
Add column (Alter table) to each table
Update each table to set newly added column
Can someone please suggest an efficient way of doing this
Thanks
bhim
You need to write a couple of SQL statements that will generate the rename / add column SQL statements.
Then you can run the SQL Statements.
You haven't provided table names, or schemas, etc. so I can give guidance but not exact results.
So assuming your "adding a constant to the name" is prefixing "const_" to it, you could do something like:
SELECT 'RENAME TABLE ''' || table_name || ''' TO ''const_' || table_name || ''' FROM information_schema.tables WHERE table_catalog = 'YourCatalog' and table_schema = 'YourSchema';
This would give you the rename table command as the output, which you could pick up and put in a text editor to tidy up.
You'll need to execute a few queries against INFORMATION_SCHEMA.tables to figure out the right filter to get the right criteria for the tables list.
And you can do similar for the Add column statement.
Some useful references:
Information schema: https://dev.mysql.com/doc/refman/5.7/en/tables-table.html
Rename a table: https://blog.marceloaltmann.com/en-how-to-rename-table-in-mysql-pt-como-renomear-tabelas-no-mysql/
Add a column: http://www.mysqltutorial.org/mysql-add-column/
I've been using the following query to replace certain data:
UPDATE wx3_t1 SET umo = REPLACE(umo, 'stringbefore', 'string after');
wx3_t1 = Table
umo = Column
I am looking for a way to update across the entire database, without needing to put the table or the column into the query.
Something as simple as just REPLACE('stringbefore', 'string after')
I realize that doing it this way is really aggressive but that's fine.
You can create a simple script that query information_schema.COLUMNS to get list of all your columns by table:
select TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME from information_schema.COLUMNS;
Then you have to iterate on the result to play your UPDATE query.
I have a column 'seq' in every table of my database that I would like to delete easily.
I have to do this on occasion in MySQL and am hoping this can be automated.
There isn't a simple magical expression to just do this. You need to generate a list of SQL statements and then run them, somehow.
(Most database folks don't routinely drop columns from a database in production; it takes a lot of time during which the tables are inaccessible, and it's destructive. A fat-finger error could really mess you up.)
You might start by using the information_schema in MySQL to discover which of your tables have a seq column in them. This query will return that list of tables for the database you're currently using.
SELECT DISTINCT TABLE_NAME
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND COLUMN_NAME = 'seq'
You could then adapt that query to, for example, create a list of statements like this.
SELECT DISTINCT
CONCAT('UPDATE ',TABLE_NAME, ' SET seq = 0;') AS stmt
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND COLUMN_NAME = 'seq'
This will produce a result set like this:
UPDATE table_a SET seq = 0;
UPDATE table_b SET seq = 0;
UPDATE user SET seq = 0;
Then you could run these statements one by one. These statements will zero out your seq columns.
Edit
You can also do
CONCAT('ALTER TABLE ',TABLE_NAME, ' DROP COLUMN seq;') AS stmt
to get a drop column statement for each table.
But, you might consider creating views of your tables that don't contain the seq columns, and then exporting to PostgreSQL using those views. If your tables are significant in size, this will save you a lot of time.
I have a Microsoft stored procedure that queries two MySQL databases using OpenQuery. The two MySQL databases should be have the same schemas, so I can run the same query on both.
However, we will soon alter the MySQL schemas, and add a column to a table. But the two MySQL databases won't happen at the same time, and I don't know the exact date of the releases.
I therefore want to write the query so that if the new column exists, then I use it in my select. If not, then I use a default value.
Is this possible? (That is have a query that handles differences in the table schema?)
(Not to be confused with 'coelesce' where the field definitely exists, but is simply null.)
You can use the following SELECT statement:
SELECT *
FROM information_schema.COLUMNS
WHERE
TABLE_SCHEMA = 'database name' AND TABLE_NAME = 'your table name'
AND COLUMN_NAME = 'the column name you want to check for'
If the above returns a value, your column is there. If not, then run your alternative SELECT statement
Updated statement:
IF EXISTS (SELECT *
FROM OPENQUERY(servername, 'SELECT *
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = ''database name''
AND TABLE_NAME = ''your table name''
AND COLUMN_NAME = ''the column name you want to check for'' ))